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.

114 lines
3.5 KiB

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