Skip to content

Commit

Permalink
Plug some large error_logger message holes with trunc_io
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagabond committed Jul 26, 2011
1 parent d0338f1 commit 0d3e45a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/error_logger_lager_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ handle_event(Event, State) ->
[ID, Name, format_reason(Reason)]);
_ ->
?CRASH_LOG(Event),
?LOG(error, Pid, Fmt, Args)
?LOG(error, Pid, lager_trunc_io:format(Fmt, Args, 4096))
end;
{error_report, _GL, {Pid, std_error, D}} ->
?CRASH_LOG(Event),
Expand All @@ -98,11 +98,11 @@ handle_event(Event, State) ->
?CRASH_LOG(Event),
?LOG(error, Pid, ["CRASH REPORT ", format_crash_report(Self, Neighbours)]);
{warning_msg, _GL, {Pid, Fmt, Args}} ->
?LOG(warning, Pid, Fmt, Args);
?LOG(warning, Pid, lager_trunc_io:format(Fmt, Args, 4096));
{warning_report, _GL, {Pid, std_warning, Report}} ->
?LOG(warning, Pid, print_silly_list(Report));
{info_msg, _GL, {Pid, Fmt, Args}} ->
?LOG(info, Pid, Fmt, Args);
?LOG(info, Pid, lager_trunc_io:format(Fmt, Args, 4096));
{info_report, _GL, {Pid, std_info, D}} when is_list(D) ->
Details = lists:sort(D),
case Details of
Expand Down Expand Up @@ -235,14 +235,18 @@ format_args([_|T], Acc) ->

print_silly_list(L) when is_list(L) ->
case lager_stdlib:string_p(L) of
true -> L;
_ -> print_silly_list(L, [], [])
true ->
lager_trunc_io:format("~s", [L], 4096);
_ ->
print_silly_list(L, [], [])
end;
print_silly_list(L) ->
io_lib:format("~w", [L]).
{Str, _} = lager_trunc_io:print(L, 4096),
Str.

print_silly_list([], Fmt, Acc) ->
io_lib:format(string:join(lists:reverse(Fmt), ", "), lists:reverse(Acc));
lager_trunc_io:format(string:join(lists:reverse(Fmt), ", "),
lists:reverse(Acc), 4096);
print_silly_list([{K,V}|T], Fmt, Acc) ->
print_silly_list(T, ["~w: ~w" | Fmt], [V, K | Acc]);
print_silly_list([H|T], Fmt, Acc) ->
Expand Down
44 changes: 43 additions & 1 deletion test/lager_test_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},
{"error messages are truncated at 4096 characters",
fun() ->
error_logger:error_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
timer:sleep(100),
{_, _, Msg} = pop(),
?assert(length(lists:flatten(Msg)) < 5100)
end
},
{"info reports are printed",
fun() ->
error_logger:info_report([{this, is}, a, {silly, format}]),
Expand All @@ -395,6 +403,14 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},
{"info reports are truncated at 4096 characters",
fun() ->
error_logger:info_report([[{this, is}, a, {silly, format}] || _ <- lists:seq(0, 600)]),
timer:sleep(1000),
{_, _, Msg} = pop(),
?assert(length(lists:flatten(Msg)) < 5000)
end
},
{"single term info reports are printed",
fun() ->
error_logger:info_report({foolish, bees}),
Expand Down Expand Up @@ -422,6 +438,15 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},
{"string info reports are truncated at 4096 characters",
fun() ->
error_logger:info_report(string:copies("this is less silly", 1000)),
timer:sleep(100),
{_, _, Msg} = pop(),
?assert(length(lists:flatten(Msg)) < 5100)
end
},

{"info messages are printed",
fun() ->
error_logger:info_msg("doom, doom has come upon you all"),
Expand All @@ -431,6 +456,15 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},
{"info messages are truncated at 4096 characters",
fun() ->
error_logger:info_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
timer:sleep(100),
{_, _, Msg} = pop(),
?assert(length(lists:flatten(Msg)) < 5100)
end
},

{"warning messages are printed at the correct level",
fun() ->
error_logger:warning_msg("doom, doom has come upon you all"),
Expand Down Expand Up @@ -571,7 +605,15 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},

{"crash report for unknown system limit should be truncated at 500 characters",
fun() ->
error_logger:error_report(crash_report, [[{pid, self()}, {error_info, {error, {system_limit,[{wtf,boom,[string:copies("aaaa", 4096)]}]}, []}}], []]),
timer:sleep(100),
{_, _, Msg} = pop(),
?assert(length(lists:flatten(Msg)) > 500),
?assert(length(lists:flatten(Msg)) < 700)
end
},
{"messages should not be generated if they don't satisfy the threshold",
fun() ->
lager:set_loglevel(?MODULE, error),
Expand Down

0 comments on commit 0d3e45a

Please sign in to comment.