From eea29bece1c042ec5c9fafbb8e9faeb01d1b7d5c Mon Sep 17 00:00:00 2001 From: Scott Lystig Fritchie Date: Mon, 22 Aug 2011 16:31:16 -0500 Subject: [PATCH] Rework using safe_format() from one of Andrew's branches --- src/lager.erl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lager.erl b/src/lager.erl index cb91aab..7f15f8c 100644 --- a/src/lager.erl +++ b/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}]).