您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

120 行
4.0 KiB

  1. %% Copyright (c) 2011-2012 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(DEFAULT_TRUNCATION, 4096).
  17. -define(DEFAULT_TRACER, lager_default_tracer).
  18. -define(DEFAULT_SINK, lager_event).
  19. -define(LEVELS,
  20. [debug, info, notice, warning, error, critical, alert, emergency, none]).
  21. %% Use of these "functions" means that the argument list will not be
  22. %% truncated for safety
  23. -define(LEVELS_UNSAFE,
  24. [{debug_unsafe, debug}, {info_unsafe, info}, {notice_unsafe, notice}, {warning_unsafe, warning}, {error_unsafe, error}, {critical_unsafe, critical}, {alert_unsafe, alert}, {emergency_unsafe, emergency}]).
  25. -define(DEBUG, 128).
  26. -define(INFO, 64).
  27. -define(NOTICE, 32).
  28. -define(WARNING, 16).
  29. -define(ERROR, 8).
  30. -define(CRITICAL, 4).
  31. -define(ALERT, 2).
  32. -define(EMERGENCY, 1).
  33. -define(LOG_NONE, 0).
  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) band element(1, lager_config:get(loglevel, {?LOG_NONE, []}))) /= 0).
  58. -define(NOTIFY(Level, Pid, Format, Args),
  59. gen_event:notify(lager_event, {log, lager_msg:new(io_lib:format(Format, Args),
  60. Level,
  61. [{pid,Pid},{line,?LINE},{file,?FILE},{module,?MODULE}],
  62. [])}
  63. )).
  64. %% FOR INTERNAL USE ONLY
  65. %% internal non-blocking logging call
  66. %% there's some special handing for when we try to log (usually errors) while
  67. %% lager is still starting.
  68. -ifdef(TEST).
  69. -define(INT_LOG(Level, Format, Args),
  70. case ?SHOULD_LOG(Level) of
  71. true ->
  72. ?NOTIFY(Level, self(), Format, Args);
  73. _ ->
  74. ok
  75. end).
  76. -else.
  77. -define(INT_LOG(Level, Format, Args),
  78. Self = self(),
  79. %% do this in a spawn so we don't cause a deadlock calling gen_event:which_handlers
  80. %% from a gen_event handler
  81. spawn(fun() ->
  82. case catch(gen_event:which_handlers(lager_event)) of
  83. X when X == []; X == {'EXIT', noproc}; X == [lager_backend_throttle] ->
  84. %% there's no handlers yet or lager isn't running, try again
  85. %% in half a second.
  86. timer:sleep(500),
  87. ?NOTIFY(Level, Self, Format, Args);
  88. _ ->
  89. case ?SHOULD_LOG(Level) of
  90. true ->
  91. ?NOTIFY(Level, Self, Format, Args);
  92. _ ->
  93. ok
  94. end
  95. end
  96. end)).
  97. -endif.
  98. -record(lager_shaper, {
  99. %% how many messages per second we try to deliver
  100. hwm = undefined :: 'undefined' | pos_integer(),
  101. %% how many messages we've received this second
  102. mps = 0 :: non_neg_integer(),
  103. %% the current second
  104. lasttime = os:timestamp() :: erlang:timestamp(),
  105. %% count of dropped messages this second
  106. dropped = 0 :: non_neg_integer()
  107. }).
  108. -type lager_shaper() :: #lager_shaper{}.