From 68c7006eee7c92b8fd7a05638857fc76a71ae01f Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 11 Dec 2020 22:07:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gen_ipc.erl | 72 +++++++++++++++++--------------- src/gen_srv.erl | 107 +++++++++++++++++++++++++----------------------- test/ping1.erl | 2 +- test/ping2.erl | 8 ++-- test/pong1.erl | 2 +- test/pong2.erl | 2 +- 6 files changed, 100 insertions(+), 93 deletions(-) diff --git a/src/gen_ipc.erl b/src/gen_ipc.erl index 0ed70c8..fa63e64 100644 --- a/src/gen_ipc.erl +++ b/src/gen_ipc.erl @@ -1050,11 +1050,11 @@ report_error(_EpmHer, {swapped, _, _}, _, _) -> ok; report_error(#epmHer{epmId = EpmId, epmM = EpmM}, Reason, State, LastIn) -> ?LOG_ERROR( #{ - label=>{gen_ipc, epm_terminate}, + label => {gen_ipc, epm_terminate}, handler => {EpmId, EpmM}, name => undefined, last_message => LastIn, - state=> State, + state => State, reason => Reason }, #{ @@ -2096,15 +2096,17 @@ format_log(Report) -> limit_report(Report, unlimited) -> Report; limit_report( - #{label:={gen_ipc, terminate}, - queue:=Q, - postponed:=Postponed, - module:=Module, - status:=FmtData, - timeouts:=Timeouts, - log:=Log, - reason:={Class, Reason, Stacktrace}, - client_info:=ClientInfo} = Report, + #{ + label := {gen_ipc, terminate}, + queue := Q, + postponed := Postponed, + module := Module, + status := FmtData, + timeouts := Timeouts, + log := Log, + reason := {Class, Reason, Stacktrace}, + client_info := ClientInfo + } = Report, Depth) -> Report#{ queue => @@ -2153,25 +2155,26 @@ format_log(Report, FormatOpts0) -> FormatOpts = maps:merge(Default, FormatOpts0), IoOpts = case FormatOpts of - #{chars_limit:=unlimited} -> []; - #{chars_limit:=Limit} -> [{chars_limit, Limit}] + #{chars_limit := unlimited} -> []; + #{chars_limit := Limit} -> [{chars_limit, Limit}] end, {Format, Args} = format_log_single(Report, FormatOpts), io_lib:format(Format, Args, IoOpts). format_log_single( #{ - label:={gen_ipc, terminate}, - name:=Name, - queue:=Q, + label := {gen_ipc, terminate}, + name := Name, + queue := Q, %% postponed %% isEnter - status:=FmtData, + status := FmtData, %% timeouts - log:=Log, - reason:={Class, Reason, Stacktrace}, - client_info:=ClientInfo}, - #{single_line:=true, depth:=Depth} = FormatOpts) -> + log := Log, + reason := {Class, Reason, Stacktrace}, + client_info := ClientInfo + }, + #{single_line := true, depth := Depth} = FormatOpts) -> P = p(FormatOpts), {FixedReason, FixedStacktrace} = fix_reason(Class, Reason, Stacktrace), {ClientFmt, ClientArgs} = format_client_log_single(ClientInfo, P, Depth), @@ -2221,18 +2224,19 @@ format_log_single(Report, FormatOpts) -> format_log_multi( #{ - label:={gen_ipc, terminate}, - name:=Name, - queue:=Q, - postponed:=Postponed, - module:=Module, - isEnter:=StateEnter, - status:=FmtData, - timeouts:=Timeouts, - log:=Log, - reason:={Class, Reason, Stacktrace}, - client_info:=ClientInfo}, - #{depth:=Depth} = FormatOpts) -> + label := {gen_ipc, terminate}, + name := Name, + queue := Q, + postponed := Postponed, + module := Module, + isEnter := StateEnter, + status := FmtData, + timeouts := Timeouts, + log := Log, + reason := {Class, Reason, Stacktrace}, + client_info := ClientInfo + }, + #{depth := Depth} = FormatOpts) -> P = p(FormatOpts), {FixedReason, FixedStacktrace} = fix_reason(Class, Reason, Stacktrace), {ClientFmt, ClientArgs} = format_client_log(ClientInfo, P, Depth), @@ -2375,7 +2379,7 @@ format_client_log({_Pid, {Name, Stacktrace}}, P, Depth) -> end, {Format, Args}. -p(#{single_line:=Single, depth:=Depth, encoding:=Enc}) -> +p(#{single_line := Single, depth := Depth, encoding := Enc}) -> "~" ++ single(Single) ++ mod(Enc) ++ p(Depth); p(unlimited) -> "p"; diff --git a/src/gen_srv.erl b/src/gen_srv.erl index c020d3e..9117d97 100644 --- a/src/gen_srv.erl +++ b/src/gen_srv.erl @@ -967,17 +967,18 @@ error_info(_Reason, application_controller, _From, _Msg, _Mod, _State, _Debug) - ok; error_info(Reason, Name, From, Msg, Module, Debug, State) -> Log = sys:get_log(Debug), - ?LOG_ERROR(#{label=>{gen_server, terminate}, - name=>Name, - last_message=>Msg, - state=>format_status(terminate, Module, get(), State), - log=>format_log_state(Module, Log), - reason=>Reason, - client_info=>client_stacktrace(From)}, - #{domain=>[otp], - report_cb=>fun gen_server:format_log/2, - error_logger=>#{tag=>error, - report_cb=>fun gen_server:format_log/1}}), + ?LOG_ERROR(#{label => {gen_server, terminate}, + name => Name, + last_message => Msg, + state => format_status(terminate, Module, get(), State), + log => format_log_state(Module, Log), + reason => Reason, + client_info => client_stacktrace(From)}, + #{ + domain => [otp], + report_cb => fun gen_server:format_log/2, + error_logger => #{tag => error, report_cb => fun gen_server:format_log/1} + }), ok. client_stacktrace(undefined) -> @@ -1012,21 +1013,23 @@ format_log(Report) -> limit_report(Report, unlimited) -> Report; -limit_report(#{label:={gen_server, terminate}, - last_message:=Msg, - state:=State, - log:=Log, - reason:=Reason, - client_info:=Client} = Report, +limit_report(#{label := {gen_server, terminate}, + last_message := Msg, + state := State, + log := Log, + reason := Reason, + client_info := Client} = Report, Depth) -> - Report#{last_message=>io_lib:limit_term(Msg, Depth), - state=>io_lib:limit_term(State, Depth), - log=>[io_lib:limit_term(L, Depth) || L <- Log], - reason=>io_lib:limit_term(Reason, Depth), - client_info=>limit_client_report(Client, Depth)}; -limit_report(#{label:={gen_server, no_handle_info}, - message:=Msg} = Report, Depth) -> - Report#{message=>io_lib:limit_term(Msg, Depth)}. + Report#{ + last_message => io_lib:limit_term(Msg, Depth), + state => io_lib:limit_term(State, Depth), + log => [io_lib:limit_term(L, Depth) || L <- Log], + reason => io_lib:limit_term(Reason, Depth), + client_info => limit_client_report(Client, Depth) + }; +limit_report(#{label := {gen_server, no_handle_info}, + message := Msg} = Report, Depth) -> + Report#{message => io_lib:limit_term(Msg, Depth)}. limit_client_report({From, {Name, Stacktrace}}, Depth) -> {From, {Name, io_lib:limit_term(Stacktrace, Depth)}}; @@ -1043,22 +1046,22 @@ format_log(Report, FormatOpts0) -> FormatOpts = maps:merge(Default, FormatOpts0), IoOpts = case FormatOpts of - #{chars_limit:=unlimited} -> + #{chars_limit := unlimited} -> []; - #{chars_limit:=Limit} -> + #{chars_limit := Limit} -> [{chars_limit, Limit}] end, {Format, Args} = format_log_single(Report, FormatOpts), io_lib:format(Format, Args, IoOpts). -format_log_single(#{label:={gen_server, terminate}, - name:=Name, - last_message:=Msg, - state:=State, - log:=Log, - reason:=Reason, - client_info:=Client}, - #{single_line:=true, depth:=Depth} = FormatOpts) -> +format_log_single(#{label := {gen_server, terminate}, + name := Name, + last_message := Msg, + state := State, + log := Log, + reason := Reason, + client_info := Client}, + #{single_line := true, depth := Depth} = FormatOpts) -> P = p(FormatOpts), Format1 = lists:append(["Generic server ", P, " terminating. Reason: ", P, ". Last message: ", P, ". State: ", P, "."]), @@ -1074,10 +1077,10 @@ format_log_single(#{label:={gen_server, terminate}, end, {Format1 ++ ServerLogFormat ++ ClientLogFormat, Args1 ++ ServerLogArgs ++ ClientLogArgs}; -format_log_single(#{label:={gen_server, no_handle_info}, - module:=Module, - message:=Msg}, - #{single_line:=true, depth:=Depth} = FormatOpts) -> +format_log_single(#{label := {gen_server, no_handle_info}, + module := Module, + message := Msg}, + #{single_line := true, depth := Depth} = FormatOpts) -> P = p(FormatOpts), Format = lists:append(["Undefined handle_info in ", P, ". Unhandled message: ", P, "."]), @@ -1092,14 +1095,14 @@ format_log_single(#{label:={gen_server, no_handle_info}, format_log_single(Report, FormatOpts) -> format_log_multi(Report, FormatOpts). -format_log_multi(#{label:={gen_server, terminate}, - name:=Name, - last_message:=Msg, - state:=State, - log:=Log, - reason:=Reason, - client_info:=Client}, - #{depth:=Depth} = FormatOpts) -> +format_log_multi(#{label := {gen_server, terminate}, + name := Name, + last_message := Msg, + state := State, + log := Log, + reason := Reason, + client_info := Client}, + #{depth := Depth} = FormatOpts) -> Reason1 = fix_reason(Reason), {ClientFmt, ClientArgs} = format_client_log(Client, FormatOpts), P = p(FormatOpts), @@ -1131,10 +1134,10 @@ format_log_multi(#{label:={gen_server, terminate}, end ++ ClientArgs end, {Format, Args}; -format_log_multi(#{label:={gen_server, no_handle_info}, - module:=Module, - message:=Msg}, - #{depth:=Depth} = FormatOpts) -> +format_log_multi(#{label := {gen_server, no_handle_info}, + module := Module, + message := Msg}, + #{depth := Depth} = FormatOpts) -> P = p(FormatOpts), Format = "** Undefined handle_info in ~p~n" @@ -1214,7 +1217,7 @@ format_client_log({_From, {Name, Stacktrace}}, FormatOpts) -> end, {Format, Args}. -p(#{single_line:=Single, depth:=Depth, encoding:=Enc}) -> +p(#{single_line := Single, depth := Depth, encoding := Enc}) -> "~" ++ single(Single) ++ mod(Enc) ++ p(Depth); p(unlimited) -> "p"; diff --git a/test/ping1.erl b/test/ping1.erl index cc8e36f..d4ec884 100644 --- a/test/ping1.erl +++ b/test/ping1.erl @@ -10,7 +10,7 @@ call(N) -> doCall(0, Pid, StartTime) -> EndTime = erlang:system_time(nanosecond), exit(Pid, kill), - io:format("call1 over use time: ~p ns~n",[EndTime - StartTime]); + io:format("call1 over use time: ~p ns~n", [EndTime - StartTime]); doCall(N, Pid, StartTime) -> gen_server:call(Pid, ping), doCall(N - 1, Pid, StartTime). diff --git a/test/ping2.erl b/test/ping2.erl index 775741d..014c666 100644 --- a/test/ping2.erl +++ b/test/ping2.erl @@ -10,7 +10,7 @@ call(N) -> doCall(0, Pid, StartTime) -> EndTime = erlang:system_time(nanosecond), exit(Pid, kill), - io:format("call2 over use time: ~p ns~n",[EndTime - StartTime]); + io:format("call2 over use time: ~p ns~n", [EndTime - StartTime]); doCall(N, Pid, StartTime) -> gen_srv:call(Pid, ping), doCall(N - 1, Pid, StartTime). @@ -21,11 +21,11 @@ send(N) -> doSend(N, Pid, StartTime). doSend(0, Pid, StartTime) -> - % Ret = gen_srv:call(Pid, ping), + % Ret = gen_srv:call(Pid, ping), Ret = 1, EndTime = erlang:system_time(nanosecond), exit(Pid, kill), - io:format("send2 over use time: ~p ~p ns~n",[Ret, EndTime - StartTime]); + io:format("send2 over use time: ~p ~p ns~n", [Ret, EndTime - StartTime]); doSend(N, Pid, StartTime) -> gen_srv:send(Pid, ping), doSend(N - 1, Pid, StartTime). @@ -40,7 +40,7 @@ doCast(0, Pid, StartTime) -> Ret = 1, EndTime = erlang:system_time(nanosecond), exit(Pid, kill), - io:format("cast2 over use time: ~p ~p ns~n",[Ret, EndTime - StartTime]); + io:format("cast2 over use time: ~p ~p ns~n", [Ret, EndTime - StartTime]); doCast(N, Pid, StartTime) -> gen_srv:cast(Pid, ping), doCast(N - 1, Pid, StartTime). diff --git a/test/pong1.erl b/test/pong1.erl index ea5343f..827e795 100644 --- a/test/pong1.erl +++ b/test/pong1.erl @@ -7,7 +7,7 @@ start() -> gen_server:start(?MODULE, 0, []). -init(_Args) -> +init(_Args) -> {ok, 0}. handle_call(ping, _From, State) -> diff --git a/test/pong2.erl b/test/pong2.erl index b7b1188..1e13b3c 100644 --- a/test/pong2.erl +++ b/test/pong2.erl @@ -7,7 +7,7 @@ start() -> gen_srv:start(?MODULE, 0, []). -init(_Args) -> +init(_Args) -> {ok, 0}. handleCall(ping, _State, _From) ->