25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Use correct stacktrace in logging macros

By calling erlang:get_stacktrace() inside a lager function
we obtain actually a stacktrace of the lager function, not
the one we got during exception. This is not a problem for
newest Erlang versions though.
This commit is contained in:
Evgeny Khramtsov 2019-06-26 00:05:41 +03:00
parent 6697a3e3f1
commit b479fe5315
11 changed files with 53 additions and 31 deletions

View File

@ -151,7 +151,8 @@ get_certificates(Domains) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, get_certificates} {error, get_certificates}
end; end;
false -> false ->
@ -243,7 +244,8 @@ get_certificate(CAUrl, DomainName, PrivateKey) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, DomainName, get_certificate} {error, DomainName, get_certificate}
end. end.
@ -382,7 +384,8 @@ renew_certificates() ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, get_certificates} {error, get_certificates}
end. end.
@ -447,7 +450,8 @@ list_certificates(Verbose) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, list_certificates} {error, list_certificates}
end; end;
false -> false ->
@ -489,7 +493,8 @@ format_certificate(DataCert, Verbose) ->
end end
catch catch
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
fail_format_certificate(DomainName) fail_format_certificate(DomainName)
end. end.
@ -614,7 +619,8 @@ revoke_certificates(DomainOrFile) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, revoke_certificate} {error, revoke_certificate}
end. end.
@ -1085,7 +1091,8 @@ save_certificate({ok, DomainName, Cert}) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, ?EX_STACK(St)]), StackTrace = ?EX_STACK(St),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, StackTrace]),
{error, DomainName, saving} {error, DomainName, saving}
end. end.

View File

@ -73,8 +73,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, ?EX_STACK(St)}}]) [xmpp:pp(Packet), {E, {R, StackTrace}}])
end. end.
-spec route_iq(iq(), function()) -> ok. -spec route_iq(iq(), function()) -> ok.

View File

@ -90,8 +90,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, ?EX_STACK(St)}}]) [xmpp:pp(Packet), {E, {R, StackTrace}}])
end. end.
-spec route(jid(), jid(), xmlel() | stanza()) -> ok. -spec route(jid(), jid(), xmlel() | stanza()) -> ok.

View File

@ -122,10 +122,11 @@ row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
catch _:{bad_node, _} -> catch _:{bad_node, _} ->
[]; [];
?EX_RULE(E, R, St) -> ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to decode row from 'route' table:~n" ?ERROR_MSG("Failed to decode row from 'route' table:~n"
"Row = ~p~n" "Row = ~p~n"
"Domain = ~s~n" "Domain = ~s~n"
"Reason = ~p", "Reason = ~p",
[Row, Domain, {E, {R, ?EX_STACK(St)}}]), [Row, Domain, {E, {R, StackTrace}}]),
[] []
end. end.

View File

@ -91,8 +91,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, ?EX_STACK(St)}}]) [xmpp:pp(Packet), {E, {R, StackTrace}}])
end. end.
clean_temporarily_blocked_table() -> clean_temporarily_blocked_table() ->

View File

@ -141,9 +141,10 @@ route(Packet) ->
Packet1 -> Packet1 ->
try do_route(Packet1), ok try do_route(Packet1), ok
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("Failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet1), [xmpp:pp(Packet1),
{E, {R, ?EX_STACK(St)}}]) {E, {R, StackTrace}}])
end end
end. end.

View File

@ -535,12 +535,13 @@ outer_transaction(F, NRestarts, _Reason) ->
put(?NESTING_KEY, ?TOP_LEVEL_TXN), put(?NESTING_KEY, ?TOP_LEVEL_TXN),
outer_transaction(F, NRestarts - 1, Reason); outer_transaction(F, NRestarts - 1, Reason);
?EX_RULE(throw, {aborted, Reason}, Stack) when NRestarts =:= 0 -> ?EX_RULE(throw, {aborted, Reason}, Stack) when NRestarts =:= 0 ->
StackTrace = ?EX_STACK(Stack),
?ERROR_MSG("SQL transaction restarts exceeded~n** " ?ERROR_MSG("SQL transaction restarts exceeded~n** "
"Restarts: ~p~n** Last abort reason: " "Restarts: ~p~n** Last abort reason: "
"~p~n** Stacktrace: ~p~n** When State " "~p~n** Stacktrace: ~p~n** When State "
"== ~p", "== ~p",
[?MAX_TRANSACTION_RESTARTS, Reason, [?MAX_TRANSACTION_RESTARTS, Reason,
?EX_STACK(Stack), get(?STATE_KEY)]), StackTrace, get(?STATE_KEY)]),
sql_query_internal([<<"rollback;">>]), sql_query_internal([<<"rollback;">>]),
{aborted, Reason}; {aborted, Reason};
?EX_RULE(exit, Reason, _) -> ?EX_RULE(exit, Reason, _) ->
@ -612,8 +613,9 @@ sql_query_internal(#sql_query{} = Query) ->
exit:{normal, _} -> exit:{normal, _} ->
{error, <<"terminated unexpectedly">>}; {error, <<"terminated unexpectedly">>};
?EX_RULE(Class, Reason, Stack) -> ?EX_RULE(Class, Reason, Stack) ->
StackTrace = ?EX_STACK(Stack),
?ERROR_MSG("Internal error while processing SQL query: ~p", ?ERROR_MSG("Internal error while processing SQL query: ~p",
[{Class, Reason, ?EX_STACK(Stack)}]), [{Class, Reason, StackTrace}]),
{error, <<"internal error">>} {error, <<"internal error">>}
end, end,
check_error(Res, Query); check_error(Res, Query);
@ -753,10 +755,11 @@ sql_query_format_res({selected, _, Rows}, SQLQuery) ->
[(SQLQuery#sql_query.format_res)(Row)] [(SQLQuery#sql_query.format_res)(Row)]
catch catch
?EX_RULE(Class, Reason, Stack) -> ?EX_RULE(Class, Reason, Stack) ->
StackTrace = ?EX_STACK(Stack),
?ERROR_MSG("Error while processing " ?ERROR_MSG("Error while processing "
"SQL query result: ~p~n" "SQL query result: ~p~n"
"row: ~p", "row: ~p",
[{Class, Reason, ?EX_STACK(Stack)}, Row]), [{Class, Reason, StackTrace}, Row]),
[] []
end end
end, Rows), end, Rows),

View File

@ -112,8 +112,9 @@ process_iq(_Host, Module, Function, IQ) ->
ignore -> ignore ->
ok ok
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to process iq:~n~s~nReason = ~p", ?ERROR_MSG("Failed to process iq:~n~s~nReason = ~p",
[xmpp:pp(IQ), {E, {R, ?EX_STACK(St)}}]), [xmpp:pp(IQ), {E, {R, StackTrace}}]),
Txt = ?T("Module failed to handle the query"), Txt = ?T("Module failed to handle the query"),
Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang), Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
ejabberd_router:route_error(IQ, Err) ejabberd_router:route_error(IQ, Err)

View File

@ -192,7 +192,8 @@ process([Call], #request{method = 'POST', data = Data, ip = IPPort} = Req) ->
?DEBUG("Bad Request: ~p", [_Err]), ?DEBUG("Bad Request: ~p", [_Err]),
badrequest_response(<<"Invalid JSON input">>); badrequest_response(<<"Invalid JSON input">>);
?EX_RULE(_Class, _Error, Stack) -> ?EX_RULE(_Class, _Error, Stack) ->
?DEBUG("Bad Request: ~p ~p", [_Error, ?EX_STACK(Stack)]), StackTrace = ?EX_STACK(Stack),
?DEBUG("Bad Request: ~p ~p", [_Error, StackTrace]),
badrequest_response() badrequest_response()
end; end;
process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) -> process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) ->
@ -209,7 +210,8 @@ process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) ->
throw:{error, unknown_command} -> throw:{error, unknown_command} ->
json_format({404, 44, <<"Command not found.">>}); json_format({404, 44, <<"Command not found.">>});
?EX_RULE(_, _Error, Stack) -> ?EX_RULE(_, _Error, Stack) ->
?DEBUG("Bad Request: ~p ~p", [_Error, ?EX_STACK(Stack)]), StackTrace = ?EX_STACK(Stack),
?DEBUG("Bad Request: ~p ~p", [_Error, StackTrace]),
badrequest_response() badrequest_response()
end; end;
process([_Call], #request{method = 'OPTIONS', data = <<>>}) -> process([_Call], #request{method = 'OPTIONS', data = <<>>}) ->
@ -296,10 +298,11 @@ handle(Call, Auth, Args, Version) when is_atom(Call), is_list(Args) ->
throw:Msg when is_list(Msg); is_binary(Msg) -> throw:Msg when is_list(Msg); is_binary(Msg) ->
{400, iolist_to_binary(Msg)}; {400, iolist_to_binary(Msg)};
?EX_RULE(Class, Error, Stack) -> ?EX_RULE(Class, Error, Stack) ->
StackTrace = ?EX_STACK(Stack),
?ERROR_MSG("REST API Error: " ?ERROR_MSG("REST API Error: "
"~s(~p) -> ~p:~p ~p", "~s(~p) -> ~p:~p ~p",
[Call, hide_sensitive_args(Args), [Call, hide_sensitive_args(Args),
Class, Error, ?EX_STACK(Stack)]), Class, Error, StackTrace]),
{500, <<"internal_error">>} {500, <<"internal_error">>}
end. end.

View File

@ -746,8 +746,9 @@ terminate(Reason, _StateName,
end, end,
mod_muc:room_destroyed(Host, Room, self(), LServer) mod_muc:room_destroyed(Host, Room, self(), LServer)
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
mod_muc:room_destroyed(Host, Room, self(), LServer), mod_muc:room_destroyed(Host, Room, self(), LServer),
?ERROR_MSG("Got exception on room termination: ~p", [{E, {R, ?EX_STACK(St)}}]) ?ERROR_MSG("Got exception on room termination: ~p", [{E, {R, StackTrace}}])
end, end,
ok. ok.
@ -2782,6 +2783,7 @@ process_item_change(Item, SD, UJID) ->
SD1 SD1
end end
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
FromSuffix = case UJID of FromSuffix = case UJID of
#jid{} -> #jid{} ->
JidString = jid:encode(UJID), JidString = jid:encode(UJID),
@ -2790,7 +2792,7 @@ process_item_change(Item, SD, UJID) ->
<<"">> <<"">>
end, end,
?ERROR_MSG("Failed to set item ~p~s: ~p", ?ERROR_MSG("Failed to set item ~p~s: ~p",
[Item, FromSuffix, {E, {R, ?EX_STACK(St)}}]), [Item, FromSuffix, {E, {R, StackTrace}}]),
{error, xmpp:err_internal_server_error()} {error, xmpp:err_internal_server_error()}
end. end.

View File

@ -321,8 +321,9 @@ process_iq_get(#iq{to = To, lang = Lang,
ver = Version} ver = Version}
end) end)
catch ?EX_RULE(E, R, St) -> catch ?EX_RULE(E, R, St) ->
StackTrace = ?EX_STACK(St),
?ERROR_MSG("Failed to process roster get for ~s: ~p", ?ERROR_MSG("Failed to process roster get for ~s: ~p",
[jid:encode(To), {E, {R, ?EX_STACK(St)}}]), [jid:encode(To), {E, {R, StackTrace}}]),
Txt = ?T("Roster module has failed"), Txt = ?T("Roster module has failed"),
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)) xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
end. end.