From 052b639c0c537891dc7636b104d3d731ad3802a3 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Thu, 11 Aug 2011 21:15:07 -0400 Subject: [PATCH] Wrap to 80 columns IBM punchcards rejoice! Basho supports your right to continue to affect how we format source code :) --- src/error_logger_lager_h.erl | 25 +++++++---- src/lager.erl | 11 +++-- src/lager_crash_log.erl | 12 ++++-- src/lager_file_backend.erl | 13 +++--- src/lager_util.erl | 81 ++++++++++++++++++++++++------------ 5 files changed, 94 insertions(+), 48 deletions(-) diff --git a/src/error_logger_lager_h.erl b/src/error_logger_lager_h.erl index b359ee5..41141e6 100644 --- a/src/error_logger_lager_h.erl +++ b/src/error_logger_lager_h.erl @@ -90,7 +90,9 @@ handle_event(Event, State) -> case lists:sort(D) of [{errorContext, Ctx}, {offender, Off}, {reason, Reason}, {supervisor, Name}] -> Offender = format_offender(Off), - ?LOG(error, Pid, "Supervisor ~w had child ~s exit with reason ~s in context ~w", [element(2, Name), Offender, format_reason(Reason), Ctx]); + ?LOG(error, Pid, + "Supervisor ~w had child ~s exit with reason ~s in context ~w", + [element(2, Name), Offender, format_reason(Reason), Ctx]); _ -> ?LOG(error, Pid, ["SUPERVISOR REPORT ", print_silly_list(D)]) end; @@ -107,7 +109,8 @@ handle_event(Event, State) -> Details = lists:sort(D), case Details of [{application, App}, {exited, Reason}, {type, _Type}] -> - ?LOG(info, Pid, "Application ~w exited with reason: ~w", [App, Reason]); + ?LOG(info, Pid, "Application ~w exited with reason: ~w", + [App, Reason]); _ -> ?LOG(info, Pid, print_silly_list(D)) end; @@ -122,7 +125,8 @@ handle_event(Event, State) -> [{started, Started}, {supervisor, Name}] -> MFA = format_mfa(proplists:get_value(mfargs, Started)), Pid = proplists:get_value(pid, Started), - ?LOG(debug, P, "Supervisor ~w started ~s at pid ~w", [element(2, Name), MFA, Pid]); + ?LOG(debug, P, "Supervisor ~w started ~s at pid ~w", + [element(2, Name), MFA, Pid]); _ -> ?LOG(info, P, ["PROGRESS REPORT ", print_silly_list(D)]) end; @@ -145,21 +149,25 @@ code_change(_OldVsn, State, _Extra) -> format_crash_report(Report, Neighbours) -> Name = proplists:get_value(registered_name, Report, proplists:get_value(pid, Report)), {_Class, Reason, _Trace} = proplists:get_value(error_info, Report), - io_lib:format("Process ~w with ~w neighbours crashed with reason: ~s", [Name, length(Neighbours), format_reason(Reason)]). + io_lib:format("Process ~w with ~w neighbours crashed with reason: ~s", + [Name, length(Neighbours), format_reason(Reason)]). format_offender(Off) -> case proplists:get_value(name, Off) of undefined -> %% supervisor_bridge - io_lib:format("at module ~w at ~w", [proplists:get_value(mod, Off), proplists:get_value(pid, Off)]); + io_lib:format("at module ~w at ~w", + [proplists:get_value(mod, Off), proplists:get_value(pid, Off)]); Name -> %% regular supervisor MFA = format_mfa(proplists:get_value(mfargs, Off)), - io_lib:format("~w started with ~s at ~w", [Name, MFA, proplists:get_value(pid, Off)]) + io_lib:format("~w started with ~s at ~w", + [Name, MFA, proplists:get_value(pid, Off)]) end. format_reason({'function not exported', [{M, F, A},MFA|_]}) -> - ["call to undefined function ", format_mfa({M, F, length(A)}), " from ", format_mfa(MFA)]; + ["call to undefined function ", format_mfa({M, F, length(A)}), + " from ", format_mfa(MFA)]; format_reason({undef, [MFA|_]}) -> ["call to undefined function ", format_mfa(MFA)]; format_reason({bad_return_value, Val}) -> @@ -205,7 +213,8 @@ format_reason({badarg, [MFA,MFA2|_]}) -> end; format_reason({{badarity, {Fun, Args}}, [MFA|_]}) -> {arity, Arity} = lists:keyfind(arity, 1, erlang:fun_info(Fun)), - [io_lib:format("fun called with wrong arity of ~w instead of ~w in ", [length(Args), Arity]), format_mfa(MFA)]; + [io_lib:format("fun called with wrong arity of ~w instead of ~w in ", + [length(Args), Arity]), format_mfa(MFA)]; format_reason({noproc, MFA}) -> ["no such process or port in call to ", format_mfa(MFA)]; format_reason({{badfun, Term}, [MFA|_]}) -> diff --git a/src/lager.erl b/src/lager.erl index f63c0b2..cbd07b7 100644 --- a/src/lager.erl +++ b/src/lager.erl @@ -62,22 +62,25 @@ log(Level, Module, Function, Line, Pid, Time, Message) -> ok | {error, lager_not_running}. 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:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)], + Msg = [["[", atom_to_list(Level), "] "], + io_lib:format("~p@~p:~p:~p ", [Pid, Module, Function, Line]), + string:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)], safe_notify(lager_util:level_to_num(Level), Timestamp, Msg). %% @doc Manually log a message into lager without using the parse transform. -spec log(log_level(), pid(), list()) -> ok | {error, lager_not_running}. log(Level, Pid, Message) -> Timestamp = lager_util:format_time(), - Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]), string:strip(lists:flatten(Message), right, $\n)], + Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]), + string:strip(lists:flatten(Message), right, $\n)], safe_notify(lager_util:level_to_num(Level), Timestamp, Msg). %% @doc Manually log a message into lager without using the parse transform. -spec log(log_level(), pid(), string(), list()) -> ok | {error, lager_not_running}. log(Level, Pid, Format, Args) -> Timestamp = lager_util:format_time(), - Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]), string:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)], + Msg = [["[", atom_to_list(Level), "] "], io_lib:format("~p ", [Pid]), + string:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)], safe_notify(lager_util:level_to_num(Level), Timestamp, Msg). %% @doc Set the loglevel for a particular backend. diff --git a/src/lager_crash_log.erl b/src/lager_crash_log.erl index 9023632..5934deb 100644 --- a/src/lager_crash_log.erl +++ b/src/lager_crash_log.erl @@ -97,12 +97,15 @@ handle_cast({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap, lager_util:rotate_logfile(Name, Count), handle_cast({log, Event}, State); {ok, {NewFD, NewInode, _Size}} -> - {Date, TS} = lager_util:format_time(lager_stdlib:maybe_utc(erlang:localtime())), + {Date, TS} = lager_util:format_time( + lager_stdlib:maybe_utc(erlang:localtime())), Time = [Date, " ", TS," =", ReportStr, "====\n"], NodeSuffix = other_node_suffix(Pid), - case file:write(NewFD, io_lib:format("~s~s~s", [Time, MsgStr, NodeSuffix])) of + Msg = io_lib:format("~s~s~s", [Time, MsgStr, NodeSuffix]), + case file:write(NewFD, Msg) of {error, Reason} when Flap == false -> - ?INT_LOG(error, "Failed to write log message to file ~s: ~s", [Name, file:format_error(Reason)]), + ?INT_LOG(error, "Failed to write log message to file ~s: ~s", + [Name, file:format_error(Reason)]), {noreply, State#state{fd=NewFD, inode=NewInode, flap=true}}; ok -> {noreply, State#state{fd=NewFD, inode=NewInode, flap=false}}; @@ -114,7 +117,8 @@ handle_cast({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap, true -> {noreply, State}; _ -> - ?INT_LOG(error, "Failed to reopen logfile ~s with error ~w", [Name, file:format_error(Reason)]), + ?INT_LOG(error, "Failed to reopen logfile ~s with error ~w", + [Name, file:format_error(Reason)]), {noreply, State#state{flap=true}} end end diff --git a/src/lager_file_backend.erl b/src/lager_file_backend.erl index 8ee9560..66c8f86 100644 --- a/src/lager_file_backend.erl +++ b/src/lager_file_backend.erl @@ -61,8 +61,8 @@ init(LogFiles) -> schedule_rotation(Name, Date), case lager_util:open_logfile(Name, true) of {ok, {FD, Inode, _}} -> - #file{name=Name, level=lager_util:level_to_num(Level), fd=FD, - inode=Inode, size=Size, date=Date, count=Count}; + #file{name=Name, level=lager_util:level_to_num(Level), + fd=FD, inode=Inode, size=Size, date=Date, count=Count}; {error, Reason} -> ?INT_LOG(error, "Failed to open log file ~s with error ~s", [Name, file:format_error(Reason)]), @@ -84,7 +84,8 @@ handle_call({set_loglevel, Ident, Level}, #state{files=Files} = State) -> _ -> NewFiles = lists:map( fun(#file{name=Name} = File) when Name == Ident -> - ?INT_LOG(notice, "Changed loglevel of ~s to ~p", [Ident, Level]), + ?INT_LOG(notice, "Changed loglevel of ~s to ~p", + [Ident, Level]), File#file{level=lager_util:level_to_num(Level)}; (X) -> X end, Files), @@ -150,7 +151,8 @@ write(#file{name=Name, fd=FD, inode=Inode, flap=Flap, size=RotSize, %% force a sync on any message at error severity or above Flap2 = case file:datasync(NewFD) of {error, Reason2} when Flap == false -> - ?INT_LOG(error, "Failed to write log message to file ~s: ~s", [Name, file:format_error(Reason2)]), + ?INT_LOG(error, "Failed to write log message to file ~s: ~s", + [Name, file:format_error(Reason2)]), true; ok -> false; @@ -166,7 +168,8 @@ write(#file{name=Name, fd=FD, inode=Inode, flap=Flap, size=RotSize, true -> File; _ -> - ?INT_LOG(error, "Failed to reopen logfile ~s with error ~s", [Name, file:format_error(Reason)]), + ?INT_LOG(error, "Failed to reopen logfile ~s with error ~s", + [Name, file:format_error(Reason)]), File#file{flap=true} end end. diff --git a/src/lager_util.erl b/src/lager_util.erl index e618f17..60e25a0 100644 --- a/src/lager_util.erl +++ b/src/lager_util.erl @@ -129,13 +129,17 @@ format_time() -> format_time(maybe_utc(localtime_ms())). format_time({utc, {{Y, M, D}, {H, Mi, S, Ms}}}) -> - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b UTC", [H, Mi, S, Ms])}; + {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), + io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b UTC", [H, Mi, S, Ms])}; format_time({{Y, M, D}, {H, Mi, S, Ms}}) -> - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b", [H, Mi, S, Ms])}; + {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), + io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b", [H, Mi, S, Ms])}; format_time({utc, {{Y, M, D}, {H, Mi, S}}}) -> - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), io_lib:format("~2..0b:~2..0b:~2..0b UTC", [H, Mi, S])}; + {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), + io_lib:format("~2..0b:~2..0b:~2..0b UTC", [H, Mi, S])}; format_time({{Y, M, D}, {H, Mi, S}}) -> - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), io_lib:format("~2..0b:~2..0b:~2..0b", [H, Mi, S])}. + {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]), + io_lib:format("~2..0b:~2..0b:~2..0b", [H, Mi, S])}. parse_rotation_day_spec([], Res) -> {ok, Res ++ [{hour, 0}]}; @@ -183,7 +187,8 @@ parse_rotation_date_spec(_) -> calculate_next_rotation(Spec) -> Now = calendar:local_time(), Later = calculate_next_rotation(Spec, Now), - calendar:datetime_to_gregorian_seconds(Later) - calendar:datetime_to_gregorian_seconds(Now). + calendar:datetime_to_gregorian_seconds(Later) - + calendar:datetime_to_gregorian_seconds(Now). calculate_next_rotation([], Now) -> Now; @@ -295,31 +300,53 @@ parse_fail_test() -> ok. rotation_calculation_test() -> - ?assertMatch({{2000, 1, 2}, {0, 0, 0}}, calculate_next_rotation([{hour, 0}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, calculate_next_rotation([{hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 2}, {12, 0, 0}}, calculate_next_rotation([{hour, 12}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 15}, {12, 34, 43}})), - ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 2}, {12, 34, 43}})), - ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 31}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, calculate_next_rotation([{date, 1}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 15}, {16, 0, 0}}, calculate_next_rotation([{date, 15}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 31}, {16, 0, 0}}, calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 31}, {16, 0, 0}}, calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {12, 34, 43}})), - ?assertMatch({{2000, 2, 29}, {16, 0, 0}}, calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {17, 34, 43}})), - ?assertMatch({{2001, 2, 28}, {16, 0, 0}}, calculate_next_rotation([{date, last}, {hour, 16}], {{2001, 1, 31}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 2}, {0, 0, 0}}, + calculate_next_rotation([{hour, 0}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, + calculate_next_rotation([{hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 2}, {12, 0, 0}}, + calculate_next_rotation([{hour, 12}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, + calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, + calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 15}, {12, 34, 43}})), + ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, + calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 2}, {12, 34, 43}})), + ?assertMatch({{2000, 2, 1}, {12, 0, 0}}, + calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 31}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, + calculate_next_rotation([{date, 1}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 15}, {16, 0, 0}}, + calculate_next_rotation([{date, 15}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 31}, {16, 0, 0}}, + calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 31}, {16, 0, 0}}, + calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {12, 34, 43}})), + ?assertMatch({{2000, 2, 29}, {16, 0, 0}}, + calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {17, 34, 43}})), + ?assertMatch({{2001, 2, 28}, {16, 0, 0}}, + calculate_next_rotation([{date, last}, {hour, 16}], {{2001, 1, 31}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), - ?assertMatch({{2000, 1, 8}, {16, 0, 0}}, calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 7}, {16, 0, 0}}, calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 3}, {16, 0, 0}}, calculate_next_rotation([{day, 1}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 2}, {16, 0, 0}}, calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 9}, {16, 0, 0}}, calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 2}, {17, 34, 43}})), - ?assertMatch({{2000, 2, 3}, {16, 0, 0}}, calculate_next_rotation([{day, 4}, {hour, 16}], {{2000, 1, 29}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 1}, {16, 0, 0}}, + calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})), + ?assertMatch({{2000, 1, 8}, {16, 0, 0}}, + calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 7}, {16, 0, 0}}, + calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 3}, {16, 0, 0}}, + calculate_next_rotation([{day, 1}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 2}, {16, 0, 0}}, + calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 9}, {16, 0, 0}}, + calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 2}, {17, 34, 43}})), + ?assertMatch({{2000, 2, 3}, {16, 0, 0}}, + calculate_next_rotation([{day, 4}, {hour, 16}], {{2000, 1, 29}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 7}, {16, 0, 0}}, calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 3}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 7}, {16, 0, 0}}, + calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 3}, {17, 34, 43}})), - ?assertMatch({{2000, 1, 3}, {16, 0, 0}}, calculate_next_rotation([{day, 1}, {hour, 16}], {{1999, 12, 28}, {17, 34, 43}})), + ?assertMatch({{2000, 1, 3}, {16, 0, 0}}, + calculate_next_rotation([{day, 1}, {hour, 16}], {{1999, 12, 28}, {17, 34, 43}})), ok. rotate_file_test() ->