Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

130 rader
4.4 KiB

14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
  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(ERROR_LOGGER_SINK, error_logger_lager_event).
  20. -define(LEVELS,
  21. [debug, info, notice, warning, error, critical, alert, emergency, none]).
  22. %% Use of these "functions" means that the argument list will not be
  23. %% truncated for safety
  24. -define(LEVELS_UNSAFE,
  25. [{debug_unsafe, debug}, {info_unsafe, info}, {notice_unsafe, notice}, {warning_unsafe, warning}, {error_unsafe, error}, {critical_unsafe, critical}, {alert_unsafe, alert}, {emergency_unsafe, emergency}]).
  26. -define(DEBUG, 128).
  27. -define(INFO, 64).
  28. -define(NOTICE, 32).
  29. -define(WARNING, 16).
  30. -define(ERROR, 8).
  31. -define(CRITICAL, 4).
  32. -define(ALERT, 2).
  33. -define(EMERGENCY, 1).
  34. -define(LOG_NONE, 0).
  35. -define(LEVEL2NUM(Level),
  36. case Level of
  37. debug -> ?DEBUG;
  38. info -> ?INFO;
  39. notice -> ?NOTICE;
  40. warning -> ?WARNING;
  41. error -> ?ERROR;
  42. critical -> ?CRITICAL;
  43. alert -> ?ALERT;
  44. emergency -> ?EMERGENCY
  45. end).
  46. -define(NUM2LEVEL(Num),
  47. case Num of
  48. ?DEBUG -> debug;
  49. ?INFO -> info;
  50. ?NOTICE -> notice;
  51. ?WARNING -> warning;
  52. ?ERROR -> error;
  53. ?CRITICAL -> critical;
  54. ?ALERT -> alert;
  55. ?EMERGENCY -> emergency
  56. end).
  57. -define(SHOULD_LOG(Sink, Level),
  58. (lager_util:level_to_num(Level) band element(1, lager_config:get({Sink, loglevel}, {?LOG_NONE, []}))) /= 0).
  59. -define(SHOULD_LOG(Level),
  60. (lager_util:level_to_num(Level) band element(1, lager_config:get(loglevel, {?LOG_NONE, []}))) /= 0).
  61. -define(NOTIFY(Level, Pid, Format, Args),
  62. gen_event:notify(lager_event, {log, lager_msg:new(io_lib:format(Format, Args),
  63. Level,
  64. [{pid,Pid},{line,?LINE},{file,?FILE},{module,?MODULE}],
  65. [])}
  66. )).
  67. %% FOR INTERNAL USE ONLY
  68. %% internal non-blocking logging call
  69. %% there's some special handing for when we try to log (usually errors) while
  70. %% lager is still starting.
  71. -ifdef(TEST).
  72. -define(INT_LOG(Level, Format, Args),
  73. case ?SHOULD_LOG(Level) of
  74. true ->
  75. ?NOTIFY(Level, self(), Format, Args);
  76. _ ->
  77. ok
  78. end).
  79. -else.
  80. -define(INT_LOG(Level, Format, Args),
  81. Self = self(),
  82. %% do this in a spawn so we don't cause a deadlock calling gen_event:which_handlers
  83. %% from a gen_event handler
  84. spawn(fun() ->
  85. case catch(gen_event:which_handlers(lager_event)) of
  86. X when X == []; X == {'EXIT', noproc}; X == [lager_backend_throttle] ->
  87. %% there's no handlers yet or lager isn't running, try again
  88. %% in half a second.
  89. timer:sleep(500),
  90. ?NOTIFY(Level, Self, Format, Args);
  91. _ ->
  92. case ?SHOULD_LOG(Level) of
  93. true ->
  94. ?NOTIFY(Level, Self, Format, Args);
  95. _ ->
  96. ok
  97. end
  98. end
  99. end)).
  100. -endif.
  101. -record(lager_shaper, {
  102. id :: any(),
  103. %% how many messages per second we try to deliver
  104. hwm = undefined :: 'undefined' | pos_integer(),
  105. %% how many messages we've received this second
  106. mps = 0 :: non_neg_integer(),
  107. %% the current second
  108. lasttime = os:timestamp() :: erlang:timestamp(),
  109. %% count of dropped messages this second
  110. dropped = 0 :: non_neg_integer(),
  111. %% timer
  112. timer = make_ref() :: reference(),
  113. %% optional filter fun to avoid counting suppressed messages against HWM totals
  114. filter = fun(_) -> false end :: fun()
  115. }).
  116. -type lager_shaper() :: #lager_shaper{}.