diff --git a/include/eRum.hrl b/include/eRum.hrl index 53f0910..a49afea 100644 --- a/include/eRum.hrl +++ b/include/eRum.hrl @@ -1,6 +1,10 @@ %% 三元表达式 -define(IIF(Cond, Ret1, Ret2), (case Cond of true -> Ret1; _ -> Ret2 end)). +%% 错误日志宏定义 +-define(ERR(Format), error_logger:error_msg(Format)). +-define(ERR(Format, Args), error_logger:error_msg(Format, Args)). + %% 应用名字 -define(RumAppName, eRum). diff --git a/src/eRum.erl b/src/eRum.erl index da9ec20..51f2dd3 100644 --- a/src/eRum.erl +++ b/src/eRum.erl @@ -1,5 +1,3 @@ -%% @doc The lager logging framework. - -module(eRum). -include("eRum.hrl"). diff --git a/src/eRum_app.erl b/src/eRum_app.erl index c5af16f..645dc44 100644 --- a/src/eRum_app.erl +++ b/src/eRum_app.erl @@ -9,7 +9,7 @@ , start/2 , stop/1 , doStart/0 - , doStartExtraSink/1 + , startSink/1 , startHandler/3 , startSink/2 ]). @@ -22,7 +22,7 @@ start() -> start(_StartType, _StartArgs) -> {ok, Pid} = eRum_sup:start_link(), SavedHandlers = doStart(), - _ = doStartExtraSink(), + doStartExtraSink(), doStartTraces(), {ok, Pid, SavedHandlers}. @@ -59,8 +59,8 @@ startTrace({Handler, Filter, Level}) when is_atom(Level) -> doStartExtraSink() -> doStartExtraSinks(rumUtil:get_env(extra_sinks, [])). -doStartExtraSink(?RumDefSink) -> doStart(); -doStartExtraSink(Sink) -> +startSink(?RumDefSink) -> doStart(); +startSink(Sink) -> AllSinksDef = rumUtil:get_env(extra_sinks, []), boot_sink(Sink, lists:keyfind(Sink, 1, AllSinksDef)). @@ -73,7 +73,7 @@ stop(Handlers) -> [error_logger:add_report_handler(Handler) || Handler <- Handlers], rumConfig:cleanup(). -tryStartAsyncMgr(undefined, _Window, _SinkS) -> +tryStartAsyncMgr(undefined, _Window, _Sink) -> ignore; tryStartAsyncMgr(Threshold, Window, Sink) -> case Window of diff --git a/src/watcher/rumHWatcherSrv.erl b/src/watcher/rumHWatcherSrv.erl index 0c27de2..008a465 100644 --- a/src/watcher/rumHWatcherSrv.erl +++ b/src/watcher/rumHWatcherSrv.erl @@ -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), diff --git a/进度.md b/进度.md new file mode 100644 index 0000000..02e73ea --- /dev/null +++ b/进度.md @@ -0,0 +1,19 @@ +# Done complete + eRum_sup + eRum.app.src + rumHWatcherSup + + +# Done incomplete + app.config done + eRum_app + rumHWatcherSrv 部分还需要修改 删除测试代码 + + +# Doing + + + + +# TODO + \ No newline at end of file