You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. %% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
  2. %%
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. -define(LEVELS,
  17. [debug, info, notice, warning, error, critical, alert, emergency, none]).
  18. -define(DEBUG, 7).
  19. -define(INFO, 6).
  20. -define(NOTICE, 5).
  21. -define(WARNING, 4).
  22. -define(ERROR, 3).
  23. -define(CRITICAL, 2).
  24. -define(ALERT, 1).
  25. -define(EMERGENCY, 0).
  26. -define(LOG_NONE, -1).
  27. -define(LEVEL2NUM(Level),
  28. case Level of
  29. debug -> ?DEBUG;
  30. info -> ?INFO;
  31. notice -> ?NOTICE;
  32. warning -> ?WARNING;
  33. error -> ?ERROR;
  34. critical -> ?CRITICAL;
  35. alert -> ?ALERT;
  36. emergency -> ?EMERGENCY
  37. end).
  38. -define(NUM2LEVEL(Num),
  39. case Num of
  40. ?DEBUG -> debug;
  41. ?INFO -> info;
  42. ?NOTICE -> notice;
  43. ?WARNING -> warning;
  44. ?ERROR -> error;
  45. ?CRITICAL -> critical;
  46. ?ALERT -> alert;
  47. ?EMERGENCY -> emergency
  48. end).
  49. -define(SHOULD_LOG(Level),
  50. lager_util:level_to_num(Level) =< element(1, lager_mochiglobal:get(loglevel, {?LOG_NONE, []}))).
  51. -define(NOTIFY(Level, Pid, Format, Args),
  52. gen_event:notify(lager_event, {log, lager_util:level_to_num(Level),
  53. lager_util:format_time(), [io_lib:format("[~p] ", [Level]),
  54. io_lib:format("~p ", [Pid]), io_lib:format(Format, Args)]})).
  55. %% FOR INTERNAL USE ONLY
  56. %% internal non-blocking logging call
  57. %% there's some special handing for when we try to log (usually errors) while
  58. %% lager is still starting.
  59. -ifdef(TEST).
  60. -define(INT_LOG(Level, Format, Args),
  61. case ?SHOULD_LOG(Level) of
  62. true ->
  63. ?NOTIFY(Level, self(), Format, Args);
  64. _ ->
  65. ok
  66. end).
  67. -else.
  68. -define(INT_LOG(Level, Format, Args),
  69. Self = self(),
  70. %% do this in a spawn so we don't cause a deadlock calling gen_event:which_handlers
  71. %% from a gen_event handler
  72. spawn(fun() ->
  73. case catch(gen_event:which_handlers(lager_event)) of
  74. X when X == []; X == {'EXIT', noproc} ->
  75. %% there's no handlers yet or lager isn't running, try again
  76. %% in half a second.
  77. timer:sleep(500),
  78. ?NOTIFY(Level, Self, Format, Args);
  79. _ ->
  80. case ?SHOULD_LOG(Level) of
  81. true ->
  82. ?NOTIFY(Level, Self, Format, Args);
  83. _ ->
  84. ok
  85. end
  86. end
  87. end)).
  88. -endif.