Explorar el Código

Add builtin chomping to lager_format

pull/25/head
Andrew Thompson hace 13 años
padre
commit
3311702a6d
Se han modificado 4 ficheros con 52 adiciones y 27 borrados
  1. +7
    -4
      src/lager.erl
  2. +37
    -18
      src/lager_format.erl
  3. +3
    -3
      src/lager_handler_watcher.erl
  4. +5
    -2
      src/lager_trunc_io.erl

+ 7
- 4
src/lager.erl Ver fichero

@ -56,7 +56,7 @@ log(Level, Module, Function, Line, Pid, Time, Message) ->
Timestamp = lager_util:format_time(Time),
Msg = [["[", atom_to_list(Level), "] "],
io_lib:format("~p@~p:~p:~p ", [Pid, Module, Function, Line]),
safe_format_chop("~s", [Message], 4096)],
safe_format_chop(Message, [], 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @private
@ -74,7 +74,7 @@ log(Level, Module, Function, Line, Pid, Time, Format, Args) ->
log(Level, Pid, Message) ->
Timestamp = lager_util:format_time(),
Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]),
safe_format_chop("~s", [Message], 4096)],
safe_format_chop(Message, [], 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Manually log a message into lager without using the parse transform.
@ -147,7 +147,10 @@ safe_notify(Level, Timestamp, Msg) ->
%% arguments. The caller is NOT crashed.
safe_format(Fmt, Args, Limit) ->
try lager_trunc_io:format(Fmt, Args, Limit) of
safe_format(Fmt, Args, Limit, []).
safe_format(Fmt, Args, Limit, Options) ->
try lager_trunc_io:format(Fmt, Args, Limit, Options) of
Result -> Result
catch
_:_ -> lager_trunc_io:format("FORMAT ERROR: ~p ~p", [Fmt, Args], Limit)
@ -155,4 +158,4 @@ safe_format(Fmt, Args, Limit) ->
%% @private
safe_format_chop(Fmt, Args, Limit) ->
re:replace(safe_format(Fmt, Args, Limit), "\n$", "", [{return, list}]).
safe_format(Fmt, Args, Limit, [{chomp, true}]).

+ 37
- 18
src/lager_format.erl Ver fichero

@ -20,19 +20,27 @@
%% fork of io_lib_format that uses trunc_io to protect against large terms
-export([format/3]).
-export([format/3, format/4]).
-record(options, {
chomp = false
}).
format(FmtStr, Args, MaxLen) ->
format(FmtStr, Args, MaxLen, []).
format(FmtStr, Args, MaxLen, Opts) ->
Options = make_options(Opts, #options{}),
Cs = collect(FmtStr, Args),
{Cs2, MaxLen2} = build(Cs, [], MaxLen),
{Cs2, MaxLen2} = build(Cs, [], MaxLen, Options),
%% count how many terms remain
Count = lists:foldl(
fun({_C, _As, _F, _Adj, _P, _Pad, _Enc}, Acc) ->
Acc + 1;
(_, Acc) ->
Acc
end, 0, Cs2),
build2(Cs2, Count, MaxLen2).
{Count, StrLen} = lists:foldl(
fun({_C, _As, _F, _Adj, _P, _Pad, _Enc}, {Terms, Chars}) ->
{Terms + 1, Chars};
(_, {Terms, Chars}) ->
{Terms, Chars + 1}
end, {0, 0}, Cs2),
build2(Cs2, Count, MaxLen2 - StrLen).
collect([$~|Fmt0], Args0) ->
{C,Fmt1,Args1} = collect_cseq(Fmt0, Args0),
@ -117,16 +125,22 @@ collect_cc([$i|Fmt], [A|Args]) -> {$i,[A],Fmt,Args}.
%% remaining and only calculate indentation when necessary. Must also
%% be smart when calculating indentation for characters in format.
build([{C,As,F,Ad,P,Pad,Enc}|Cs], Acc, MaxLen) ->
build([{$n, _, _, _, _, _, _}], Acc, MaxLen, #options{chomp=true}) ->
%% trailing ~n, ignore
{lists:reverse(Acc), MaxLen};
build([{C,As,F,Ad,P,Pad,Enc}|Cs], Acc, MaxLen, O) ->
{S, MaxLen2} = control(C, As, F, Ad, P, Pad, Enc, MaxLen),
build(Cs, [S|Acc], MaxLen2);
build([$\n|Cs], Acc, MaxLen) ->
build(Cs, [$\n|Acc], MaxLen - 1);
build([$\t|Cs], Acc, MaxLen) ->
build(Cs, [$\t|Acc], MaxLen - 1);
build([C|Cs], Acc, MaxLen) ->
build(Cs, [C|Acc], MaxLen - 1);
build([], Acc, MaxLen) ->
build(Cs, [S|Acc], MaxLen2, O);
build([$\n], Acc, MaxLen, #options{chomp=true}) ->
%% trailing \n, ignore
{lists:reverse(Acc), MaxLen};
build([$\n|Cs], Acc, MaxLen, O) ->
build(Cs, [$\n|Acc], MaxLen - 1, O);
build([$\t|Cs], Acc, MaxLen, O) ->
build(Cs, [$\t|Acc], MaxLen - 1, O);
build([C|Cs], Acc, MaxLen, O) ->
build(Cs, [C|Acc], MaxLen - 1, O);
build([], Acc, MaxLen, _O) ->
{lists:reverse(Acc), MaxLen}.
build2([{C,As,F,Ad,P,Pad,Enc}|Cs], Count, MaxLen) ->
@ -234,6 +248,11 @@ maybe_flatten(X) when is_list(X) ->
maybe_flatten(X) ->
X.
make_options([], Options) ->
Options;
make_options([{chomp, Bool}|T], Options) when is_boolean(Bool) ->
make_options(T, Options#options{chomp=Bool}).
-ifdef(UNICODE_AS_BINARIES).
uniconv(C) ->
unicode:characters_to_binary(C,unicode).

+ 3
- 3
src/lager_handler_watcher.erl Ver fichero

@ -110,7 +110,7 @@ reinstall_on_initial_failure_test_() ->
try
?assertEqual(1, lager_test_backend:count()),
{_Level, _Time, [_, _, Message]} = lager_test_backend:pop(),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, Message),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Message)),
?assertEqual(0, lager_test_backend:count()),
timer:sleep(6000),
?assertEqual(0, lager_test_backend:count()),
@ -139,9 +139,9 @@ reinstall_on_runtime_failure_test_() ->
timer:sleep(6000),
?assertEqual(2, lager_test_backend:count()),
{_Level, _Time, [_, _, Message]} = lager_test_backend:pop(),
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", Message),
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Message)),
{_Level2, _Time2, [_, _, Message2]} = lager_test_backend:pop(),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, Message2),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Message2)),
?assertEqual(false, lists:member(lager_crash_backend, gen_event:which_handlers(lager_event)))
after
application:stop(lager),

+ 5
- 2
src/lager_trunc_io.erl Ver fichero

@ -33,7 +33,7 @@
-module(lager_trunc_io).
-author('matthias@corelatus.se').
%% And thanks to Chris Newcombe for a bug fix
-export([format/3, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions
-export([format/3, format/4, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions
-version("$Id: trunc_io.erl,v 1.11 2009-02-23 12:01:06 matthias Exp $").
-ifdef(TEST).
@ -52,7 +52,10 @@
}).
format(Fmt, Args, Max) ->
try lager_format:format(Fmt, Args, Max) of
format(Fmt, Args, Max, []).
format(Fmt, Args, Max, Options) ->
try lager_format:format(Fmt, Args, Max, Options) of
Result -> Result
catch
_:_ ->

Cargando…
Cancelar
Guardar