|
|
@ -24,29 +24,30 @@ |
|
|
|
sink :: pid() | atom() |
|
|
|
}). |
|
|
|
|
|
|
|
start_link(Sink, Module, Config) -> |
|
|
|
gen_server:start_link(?MODULE, [Sink, Module, Config], []). |
|
|
|
|
|
|
|
start(Sink, Module, Config) -> |
|
|
|
gen_server:start(?MODULE, [Sink, Module, Config], []). |
|
|
|
gen_srv:start(?MODULE, {Sink, Module, Config}, []). |
|
|
|
|
|
|
|
start_link(Sink, Module, Config) -> |
|
|
|
gen_srv:start_link(?MODULE, {Sink, Module, Config}, []). |
|
|
|
|
|
|
|
init([Sink, Module, Config]) -> |
|
|
|
init({Sink, Module, Config}) -> |
|
|
|
process_flag(trap_exit, true), |
|
|
|
install_handler(Sink, Module, Config), |
|
|
|
installHandler(Module, Config, Sink), |
|
|
|
{ok, #state{sink = Sink, module = Module, config = Config}}. |
|
|
|
|
|
|
|
handleCall(_Call, State, _From) -> |
|
|
|
{reply, ok, State}. |
|
|
|
handleCall(_Msg, _State, _From) -> |
|
|
|
?ERR("~p call receive unexpect msg ~p ~n ", [?MODULE, _Msg]), |
|
|
|
{reply, ok}. |
|
|
|
|
|
|
|
handleCast(_Request, State) -> |
|
|
|
{noreply, State}. |
|
|
|
handleCast(_Msg, _State) -> |
|
|
|
?ERR("~p cast receive unexpect msg ~p ~n ", [?MODULE, _Msg]), |
|
|
|
kpS. |
|
|
|
|
|
|
|
handleInfo({gen_event_EXIT, Module, normal}, #state{module = Module} = State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handleInfo({gen_event_EXIT, Module, shutdown}, #state{module = Module} = State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handleInfo({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReinstallAfter]}}}, |
|
|
|
#state{module = Module, sink = Sink, config = Config} = State) -> |
|
|
|
handleInfo({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReinstallAfter]}}}, #state{module = Module, sink = Sink, config = Config} = State) -> |
|
|
|
%% Brutally kill the manager but stay alive to restore settings. |
|
|
|
%% |
|
|
|
%% SinkPid here means the gen_event process. Handlers *all* live inside the |
|
|
@ -59,30 +60,30 @@ handleInfo({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReinst |
|
|
|
exit(SinkPid, kill), |
|
|
|
_ = timer:apply_after(KillerReinstallAfter, lager_app, start_handler, [Sink, Module, Config]), |
|
|
|
{stop, normal, State}; |
|
|
|
handleInfo({gen_event_EXIT, Module, Reason}, #state{module = Module, |
|
|
|
config = Config, sink = Sink} = State) -> |
|
|
|
handleInfo({gen_event_EXIT, Module, Reason}, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
case eRum:log(error, self(), "Lager event handler ~p exited with reason ~s", [Module, rumErrLoggerH:format_reason(Reason)]) of |
|
|
|
ok -> |
|
|
|
install_handler(Sink, Module, Config); |
|
|
|
installHandler(Module, Config, Sink); |
|
|
|
{error, _} -> |
|
|
|
%% lager is not working, so installing a handler won't work |
|
|
|
ok |
|
|
|
end, |
|
|
|
{noreply, State}; |
|
|
|
handleInfo(reinstall_handler, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
install_handler(Sink, Module, Config), |
|
|
|
{noreply, State}; |
|
|
|
handleInfo({reboot, Sink}, State) -> |
|
|
|
_ = lager_app:boot(Sink), |
|
|
|
{noreply, State}; |
|
|
|
handleInfo(reinstall_handler, #state{module = Module, config = Config, sink = Sink}) -> |
|
|
|
installHandler(Module, Config, Sink), |
|
|
|
kpS; |
|
|
|
handleInfo({reboot, Sink}, _State) -> |
|
|
|
eRum_app:startSink(Sink), |
|
|
|
kpS; |
|
|
|
handleInfo(stop, State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handleInfo({'EXIT', _Pid, killed}, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
Tmr = application:get_env(lager, killerReTime, 5000), |
|
|
|
_ = timer:apply_after(Tmr, lager_app, start_handler, [Sink, Module, Config]), |
|
|
|
Tmr = rumUtil:get_env(killerReTime, 5000), |
|
|
|
_ = timer:apply_after(Tmr, eRum_app, startHandler, [Sink, Module, Config]), |
|
|
|
{stop, normal, State}; |
|
|
|
handleInfo(_Info, State) -> |
|
|
|
{noreply, State}. |
|
|
|
handleInfo(_Msg, _State) -> |
|
|
|
?ERR("~p info receive unexpect msg ~p ~n ", [?MODULE, _Msg]), |
|
|
|
kpS. |
|
|
|
|
|
|
|
terminate(_Reason, _State) -> |
|
|
|
ok. |
|
|
@ -90,20 +91,18 @@ terminate(_Reason, _State) -> |
|
|
|
code_change(_OldVsn, State, _Extra) -> |
|
|
|
{ok, State}. |
|
|
|
|
|
|
|
%% internal |
|
|
|
install_handler(Sink, lager_backend_throttle, Config) -> |
|
|
|
%% The lager_backend_throttle needs to know to which sink it is |
|
|
|
%% attached, hence this admittedly ugly workaround. Handlers are |
|
|
|
%% sensitive to the structure of the configuration sent to `init', |
|
|
|
%% sadly, so it's not trivial to add a configuration item to be |
|
|
|
%% ignored to backends without breaking 3rd party handlers. |
|
|
|
install_handler2(Sink, lager_backend_throttle, [{sink, Sink} | Config]); |
|
|
|
install_handler(Sink, Module, Config) -> |
|
|
|
install_handler2(Sink, Module, Config). |
|
|
|
|
|
|
|
%% private |
|
|
|
install_handler2(Sink, Module, Config) -> |
|
|
|
case gen_event:add_sup_handler(Sink, Module, Config) of |
|
|
|
installHandler(Module, Config, Sink) -> |
|
|
|
Ret = |
|
|
|
case Module of |
|
|
|
rumBackendThrottle -> |
|
|
|
%% lager_backend_throttle需要知道它连接到哪个接收器,因此这个公认的丑陋的解决方案。处理程序对发送到' init'的配置结构是敏感的, |
|
|
|
%% 遗憾的是,在不破坏第三方处理程序的情况下,向后端添加一个要忽略的配置项并不是一件小事。 |
|
|
|
gen_emm:add_sup_epm(Sink, Module, [{sink, Sink} | Config]); |
|
|
|
_ -> |
|
|
|
gen_emm:add_sup_epm(Sink, Module, Config) |
|
|
|
end, |
|
|
|
|
|
|
|
case Ret of |
|
|
|
ok -> |
|
|
|
?INT_LOG(debug, "Lager installed handler ~p into ~p", [Module, Sink]), |
|
|
|
eRum:updateLogevelCfg(Sink), |
|
|
|