Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

87 рядки
2.5 KiB

10 роки тому
10 роки тому
10 роки тому
10 роки тому
  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. %% @doc Helper functions for working with lager's runtime config
  17. -module(lager_config).
  18. -include("lager.hrl").
  19. -export([new/0, new_sink/1, get/1, get/2, set/2,
  20. global_get/1, global_get/2, global_set/2]).
  21. -define(TBL, lager_config).
  22. -define(GLOBAL, '_global').
  23. %% For multiple sinks, the key is now the registered event name and the old key
  24. %% as a tuple.
  25. %%
  26. %% {{lager_event, loglevel}, Value} instead of {loglevel, Value}
  27. new() ->
  28. %% set up the ETS configuration table
  29. _ = try ets:new(?TBL, [named_table, public, set, {keypos, 1}, {read_concurrency, true}]) of
  30. _Result ->
  31. ok
  32. catch
  33. error:badarg ->
  34. ?INT_LOG(warning, "Table ~p already exists", [?TBL])
  35. end,
  36. new_sink(?DEFAULT_SINK),
  37. %% Need to be able to find the `lager_handler_watcher' for all handlers
  38. ets:insert_new(?TBL, {{?GLOBAL, handlers}, []}),
  39. ok.
  40. new_sink(Sink) ->
  41. %% use insert_new here so that if we're in an appup we don't mess anything up
  42. %%
  43. %% until lager is completely started, allow all messages to go through
  44. ets:insert_new(?TBL, {{Sink, loglevel}, {element(2, lager_util:config_to_mask(debug)), []}}).
  45. global_get(Key) ->
  46. global_get(Key, undefined).
  47. global_get(Key, Default) ->
  48. get({?GLOBAL, Key}, Default).
  49. global_set(Key, Value) ->
  50. set({?GLOBAL, Key}, Value).
  51. get({_Sink, _Key}=FullKey) ->
  52. get(FullKey, undefined);
  53. get(Key) ->
  54. get({?DEFAULT_SINK, Key}, undefined).
  55. get({Sink, Key}, Default) ->
  56. try
  57. case ets:lookup(?TBL, {Sink, Key}) of
  58. [] ->
  59. Default;
  60. [{{Sink, Key}, Res}] ->
  61. Res
  62. end
  63. catch
  64. _:_ ->
  65. Default
  66. end;
  67. get(Key, Default) ->
  68. get({?DEFAULT_SINK, Key}, Default).
  69. set({Sink, Key}, Value) ->
  70. ets:insert(?TBL, {{Sink, Key}, Value});
  71. set(Key, Value) ->
  72. set({?DEFAULT_SINK, Key}, Value).