From d5b6964edeb014ca0fa90dff459542b52a3cd30c Mon Sep 17 00:00:00 2001 From: Panagiotis Papadomitsos Date: Wed, 28 Sep 2016 18:52:38 -0700 Subject: [PATCH] Support gen_statem in error logger Add a simple case to support gen_statem crashes, since they generate the same message in error_logger but not the same returned state. --- src/error_logger_lager_h.erl | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/error_logger_lager_h.erl b/src/error_logger_lager_h.erl index f0209c7..a1fc4f2 100644 --- a/src/error_logger_lager_h.erl +++ b/src/error_logger_lager_h.erl @@ -33,7 +33,7 @@ -export([format_reason/1, format_mfa/1, format_args/3]). --record(state, { +-record(state, { sink :: atom(), shaper :: lager_shaper(), %% group leader strategy @@ -88,8 +88,8 @@ handle_event(Event, #state{sink=Sink, shaper=Shaper} = State) -> {true, 0, NewShaper} -> eval_gl(Event, State#state{shaper=NewShaper}); {true, Drop, #lager_shaper{hwm=Hwm} = NewShaper} when Drop > 0 -> - ?LOGFMT(Sink, warning, self(), - "lager_error_logger_h dropped ~p messages in the last second that exceeded the limit of ~p messages/sec", + ?LOGFMT(Sink, warning, self(), + "lager_error_logger_h dropped ~p messages in the last second that exceeded the limit of ~p messages/sec", [Drop, Hwm]), eval_gl(Event, State#state{shaper=NewShaper}); {false, _, NewShaper} -> @@ -106,9 +106,9 @@ terminate(_Reason, _State) -> code_change(_OldVsn, {state, Shaper, GLStrategy}, _Extra) -> Raw = lager_app:get_env(lager, error_logger_format_raw, false), {ok, #state{ - sink=configured_sink(), - shaper=Shaper, - groupleader_strategy=GLStrategy, + sink=configured_sink(), + shaper=Shaper, + groupleader_strategy=GLStrategy, raw=Raw }}; code_change(_OldVsn, {state, Sink, Shaper, GLS}, _Extra) -> @@ -152,12 +152,18 @@ log_event(Event, #state{sink=Sink} = State) -> ?LOGFMT(Sink, error, [{pid, Pid}, {name, Name} | Md], "gen_server ~w terminated with reason: ~s", [Name, Formatted]); {false, "** State machine "++_} -> - %% gen_fsm terminate - [Name, _Msg, StateName, _StateData, Reason] = Args, + %% Check if the terminated process is gen_fsm or gen_statem + %% since they generate the same exit message + {Type, Name, StateName, Reason} = case Args of + [TName, _Msg, TStateName, _StateData, TReason] -> + {gen_fsm, TName, TStateName, TReason}; + [TName, _Msg, {TStateName, _StateData}, _ExitType, TReason, _FsmType, Stacktrace] -> + {gen_statem, TName, TStateName, {TReason, Stacktrace}} + end, {Md, Formatted} = format_reason_md(Reason), ?CRASH_LOG(Event), - ?LOGFMT(Sink, error, [{pid, Pid}, {name, Name} | Md], "gen_fsm ~w in state ~w terminated with reason: ~s", - [Name, StateName, Formatted]); + ?LOGFMT(Sink, error, [{pid, Pid}, {name, Name} | Md], "~s ~w in state ~w terminated with reason: ~s", + [Type, Name, StateName, Formatted]); {false, "** gen_event handler"++_} -> %% gen_event handler terminate [ID, Name, _Msg, _State, Reason] = Args,