選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

108 行
3.2 KiB

  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. -record(lager_log_message,{
  17. destinations,
  18. metadata,
  19. severity_as_int,
  20. timestamp,
  21. message
  22. }).
  23. -define(LEVELS,
  24. [debug, info, notice, warning, error, critical, alert, emergency, none]).
  25. -define(DEBUG, 7).
  26. -define(INFO, 6).
  27. -define(NOTICE, 5).
  28. -define(WARNING, 4).
  29. -define(ERROR, 3).
  30. -define(CRITICAL, 2).
  31. -define(ALERT, 1).
  32. -define(EMERGENCY, 0).
  33. -define(LOG_NONE, -1).
  34. -define(LEVEL2NUM(Level),
  35. case Level of
  36. debug -> ?DEBUG;
  37. info -> ?INFO;
  38. notice -> ?NOTICE;
  39. warning -> ?WARNING;
  40. error -> ?ERROR;
  41. critical -> ?CRITICAL;
  42. alert -> ?ALERT;
  43. emergency -> ?EMERGENCY
  44. end).
  45. -define(NUM2LEVEL(Num),
  46. case Num of
  47. ?DEBUG -> debug;
  48. ?INFO -> info;
  49. ?NOTICE -> notice;
  50. ?WARNING -> warning;
  51. ?ERROR -> error;
  52. ?CRITICAL -> critical;
  53. ?ALERT -> alert;
  54. ?EMERGENCY -> emergency
  55. end).
  56. -define(SHOULD_LOG(Level),
  57. lager_util:level_to_num(Level) =< element(1, lager_mochiglobal:get(loglevel, {?LOG_NONE, []}))).
  58. -define(NOTIFY(Level, Pid, Format, Args),
  59. gen_event:notify(lager_event,#lager_log_message{destinations=[],
  60. message=io_lib:format(Format,Args),
  61. metadata=[{pid,Pid},{line,?LINE},{file,?FILE},{module,?MODULE}],
  62. timestamp=lager_util:format_time(),
  63. severity_as_int=lager_util:level_to_num(Level)
  64. })).
  65. %% FOR INTERNAL USE ONLY
  66. %% internal non-blocking logging call
  67. %% there's some special handing for when we try to log (usually errors) while
  68. %% lager is still starting.
  69. -ifdef(TEST).
  70. -define(INT_LOG(Level, Format, Args),
  71. case ?SHOULD_LOG(Level) of
  72. true ->
  73. ?NOTIFY(Level, self(), Format, Args);
  74. _ ->
  75. ok
  76. end).
  77. -else.
  78. -define(INT_LOG(Level, Format, Args),
  79. Self = self(),
  80. %% do this in a spawn so we don't cause a deadlock calling gen_event:which_handlers
  81. %% from a gen_event handler
  82. spawn(fun() ->
  83. case catch(gen_event:which_handlers(lager_event)) of
  84. X when X == []; X == {'EXIT', noproc} ->
  85. %% there's no handlers yet or lager isn't running, try again
  86. %% in half a second.
  87. timer:sleep(500),
  88. ?NOTIFY(Level, Self, Format, Args);
  89. _ ->
  90. case ?SHOULD_LOG(Level) of
  91. true ->
  92. ?NOTIFY(Level, Self, Format, Args);
  93. _ ->
  94. ok
  95. end
  96. end
  97. end)).
  98. -endif.