ソースを参照

Rework using safe_format() from one of Andrew's branches

pull/8/head
Scott Lystig Fritchie 13年前
コミット
eea29bece1
1個のファイルの変更20行の追加8行の削除
  1. +20
    -8
      src/lager.erl

+ 20
- 8
src/lager.erl ファイルの表示

@ -55,7 +55,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]),
string_reduce(Message)],
format_string_chop("~s", [Message], 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @private
@ -64,8 +64,8 @@ log(Level, Module, Function, Line, Pid, Time, Message) ->
log(Level, Module, Function, Line, Pid, Time, Format, Args) ->
Timestamp = lager_util:format_time(Time),
Msg = [["[", atom_to_list(Level), "] "],
io_lib:format("~p@~p:~p:~p ", [Pid, Module, Function, Line]),
string_reduce(lager_trunc_io:format(Format, Args, 4096))],
io_lib:format("~p@~p:~p:~p ", [Pid, Module, Function, Line]),
format_string_chop(Format, Args, 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Manually log a message into lager without using the parse transform.
@ -73,7 +73,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]),
string_reduce(Message)],
format_string_chop("~s", [Message], 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Manually log a message into lager without using the parse transform.
@ -81,7 +81,7 @@ log(Level, Pid, Message) ->
log(Level, Pid, Format, Args) ->
Timestamp = lager_util:format_time(),
Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]),
string_reduce(lager_trunc_io:format(Format, Args, 4096))],
format_string_chop(Format, Args, 4096)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Set the loglevel for a particular backend.
@ -118,7 +118,7 @@ posix_error(Error) when is_atom(Error) ->
Message -> Message
end;
posix_error(Error) ->
lists:flatten(lager_trunc_io:format("~p", [Error], 4096)).
format_string_chop("~p", [Error], 4096).
%% @private
get_loglevels() ->
@ -140,6 +140,18 @@ safe_notify(Level, Timestamp, Msg) ->
gen_event:sync_notify(Pid, {log, Level, Timestamp, Msg})
end.
%% @doc Print the format string `Fmt' with `Args' safely with a size
%% limit of `Limit'. If the format string is invalid, or not enough
%% arguments are supplied 'FORMAT ERROR' is printed with the offending
%% arguments. The caller is NOT crashed.
safe_format(Fmt, Args, Limit) ->
try lager_trunc_io:format(Fmt, Args, Limit) of
Result -> Result
catch
_:_ -> lager_trunc_io:format("FORMAT ERROR: ~p ~p", [Fmt, Args], Limit)
end.
%% @private
string_reduce(IOdata) ->
re:replace(IOdata, "\n$", "", [{return, list}]).
format_string_chop(Fmt, Args, Limit) ->
re:replace(safe_format(Fmt, Args, Limit), "\n$", "", [{return, list}]).

読み込み中…
キャンセル
保存