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

80 行
1.8 KiB

-module(rumConfig).
-include("eRum.hrl").
-export([
init/0
, initSink/1
, get/1
, get/2
, set/2
, global_get/1
, global_get/2
, global_set/2
, cleanup/0
]).
-define(TBL, lager_config).
-define(GLOBAL, '_global').
%% For multiple sinks, the key is now the registered event name and the old key as a tuple.
%% {{lager_event, loglevel}, Value} instead of {loglevel, Value}
init() ->
initSink(?RumDefSink),
%% Need to be able to find the `lager_handler_watcher' for all handlers
insert_new({?GLOBAL, handlers}, []),
ok.
initSink(Sink) ->
%% use insert_new here so that if we're in an appup we don't mess anything up until lager is completely started, allow all messages to go through
insert_new({Sink, loglevel}, {element(2, rumUtil:config_to_mask(debug)), []}).
global_get(Key) ->
global_get(Key, undefined).
global_get(Key, Default) ->
get({?GLOBAL, Key}, Default).
global_set(Key, Value) ->
set({?GLOBAL, Key}, Value).
get({_Sink, _Key} = FullKey) ->
get(FullKey, undefined);
get(Key) ->
get({?RumDefSink, Key}, undefined).
get({Sink, Key}, Default) ->
lookup({Sink, Key}, Default);
get(Key, Default) ->
get({?RumDefSink, Key}, Default).
set({Sink, Key}, Value) ->
insert({Sink, Key}, Value);
set(Key, Value) ->
set({?RumDefSink, Key}, Value).
insert(Key, Value) ->
persistent_term:put({?TBL, Key}, Value).
insert_new(Key, Value) ->
try persistent_term:get({?TBL, Key}) of
_Value ->
false
catch error:badarg ->
insert(Key, Value),
true
end.
lookup(Key, Default) ->
try persistent_term:get({?TBL, Key}) of
Value -> Value
catch
error:badarg ->
Default
end.
cleanup() ->
[persistent_term:erase(K) || {{?TBL, _} = K, _} <- persistent_term:get()].