diff --git a/src/crashLog/rumCrashLog.erl b/src/crashLog/rumCrashLog.erl index a668c1d..24be013 100644 --- a/src/crashLog/rumCrashLog.erl +++ b/src/crashLog/rumCrashLog.erl @@ -95,33 +95,19 @@ scheduleRotation(Date) -> erlang:send_after(rumUtil:calcNextRotateMs(Date), self(), mRotate), ok. -%% ===== Begin code lifted from riak_err ===== - --spec limited_fmt(string(), list(), integer()) -> iolist(). -%% @doc Format Fmt and Args similar to what io_lib:format/2 does but with -%% limits on how large the formatted string may be. -%% -%% If the Args list's size is larger than TermMaxSize, then the -%% formatting is done by trunc_io:print/2, where FmtMaxBytes is used -%% to limit the formatted string's size. - -limited_fmt(Fmt, Args, FmtMaxBytes) -> - eRum:safe_format(Fmt, Args, FmtMaxBytes). - -limited_str(Term, FmtMaxBytes) -> - {Str, _} = rumTruncIo:print(Term, FmtMaxBytes), - Str. - -other_node_suffix(Pid) when node(Pid) =/= node() -> - "** at node " ++ atom_to_list(node(Pid)) ++ " **\n"; -other_node_suffix(_) -> - "". +otherNodeSuffix(Pid) when node(Pid) =/= node() -> + PidNode = node(Pid), + case PidNode =/= node() of + true -> + <<"** at node ", (atom_to_binary(node(Pid), utf8))/binary, " **\n">>; + _ -> + <<"">> + end. -perhaps_a_sasl_report(error_report, {Pid, Type, Report}, FmtMaxBytes) -> - case rumStdlib:is_my_error_report(Type) of +perhapsSaslReport(error_report, {Pid, Type, Report}, FmtMaxBytes) -> + case rumStdlib:isErrorReport(Type) of true -> - {sasl_type_to_report_head(Type), Pid, - sasl_limited_str(Type, Report, FmtMaxBytes), true}; + {saslTypeToReportHead(Type), Pid, saslLimitedStr(Type, Report, FmtMaxBytes), true}; false -> {ignore, ignore, ignore, false} end; @@ -133,79 +119,74 @@ perhaps_a_sasl_report(error_report, {Pid, Type, Report}, FmtMaxBytes) -> %false -> %{ignore, ignore, ignore, false} %end; -perhaps_a_sasl_report(_, _, _) -> +perhapsSaslReport(_, _, _) -> {ignore, ignore, ignore, false}. -sasl_type_to_report_head(supervisor_report) -> - "SUPERVISOR REPORT"; -sasl_type_to_report_head(crash_report) -> - "CRASH REPORT"; -sasl_type_to_report_head(progress) -> - "PROGRESS REPORT". +saslTypeToReportHead(supervisor_report) -> + <<"SUPERVISOR REPORT">>; +saslTypeToReportHead(crash_report) -> + <<"CRASH REPORT">>; +saslTypeToReportHead(progress) -> + <<"PROGRESS REPORT">>. -sasl_limited_str(supervisor_report, Report, FmtMaxBytes) -> +saslLimitedStr(supervisor_report, Report, FmtMaxBytes) -> Name = rumStdlib:sup_get(supervisor, Report), Context = rumStdlib:sup_get(errorContext, Report), Reason = rumStdlib:sup_get(reason, Report), Offender = rumStdlib:sup_get(offender, Report), - FmtString = " Supervisor: ~p~n Context: ~p~n Reason: " - "~s~n Offender: ~s~n~n", - {ReasonStr, _} = rumTruncIo:print(Reason, FmtMaxBytes), - {OffenderStr, _} = rumTruncIo:print(Offender, FmtMaxBytes), - io_lib:format(FmtString, [Name, Context, ReasonStr, OffenderStr]); -sasl_limited_str(progress, Report, FmtMaxBytes) -> + FmtString = <<" Supervisor: ~p~n Context: ~p~n Reason: " + "~s~n Offender: ~s~n~n">>, + ReasonStr = eFmt:formatBin(<<"~p">>, [Reason], [{charsLimit, FmtMaxBytes}]), + OffenderStr = eFmt:formatBin(<<"~p">>, [Offender], [{charsLimit, FmtMaxBytes}]), + eFmt:format(FmtString, [Name, Context, ReasonStr, OffenderStr]); +saslLimitedStr(progress, Report, FmtMaxBytes) -> [begin - {Str, _} = rumTruncIo:print(Data, FmtMaxBytes), - io_lib:format(" ~16w: ~s~n", [Tag, Str]) + BinStr = eFmt:formatBin(<<"~p">>, [Data], [{charsLimit, FmtMaxBytes}]), + eFmt:formatBin(" ~16w: ~s~n", [Tag, BinStr]) end || {Tag, Data} <- Report]; -sasl_limited_str(crash_report, Report, FmtMaxBytes) -> +saslLimitedStr(crash_report, Report, FmtMaxBytes) -> rumStdlib:proc_lib_format(Report, FmtMaxBytes). -writeLog(Event, #state{fileName = Name, fd = FD, inode = Inode, ctime = Ctime, flap = Flap, - maxFmtSize = FmtMaxBytes, maxFileSize = RotSize, count = Count, rotator = Rotator} = State) -> +writeLog(Event, #state{fileName = FileName, fd = FD, inode = Inode, ctime = Ctime, flap = Flap, maxFmtSize = FmtMaxBytes, maxFileSize = RotSize, count = Count, rotator = Rotator} = State) -> %% borrowed from riak_err {ReportStr, Pid, MsgStr, _ErrorP} = case Event of {error, _GL, {Pid1, Fmt, Args}} -> - {"ERROR REPORT", Pid1, limited_fmt(Fmt, Args, FmtMaxBytes), true}; + {<<"ERROR REPORT">>, Pid1, eFmt:formatBin(Fmt, Args, [{charsLimit, FmtMaxBytes}]), true}; {error_report, _GL, {Pid1, std_error, Rep}} -> - {"ERROR REPORT", Pid1, limited_str(Rep, FmtMaxBytes) ++ "\n", true}; + {<<"ERROR REPORT">>, Pid1, eFmt:formatBin(<<"~p\n">>, [Rep], [{charsLimit, FmtMaxBytes}]), true}; {error_report, _GL, Other} -> - perhaps_a_sasl_report(error_report, Other, FmtMaxBytes); + perhapsSaslReport(error_report, Other, FmtMaxBytes); _ -> {ignore, ignore, ignore, false} end, - if ReportStr == ignore -> - {ok, State}; - true -> - case Rotator:ensureLogfile(Name, FD, Inode, Ctime, false) of - {ok, {_FD, _Inode, _Ctime, Size}} when RotSize /= 0, Size > RotSize -> - _ = Rotator:rotateLogfile(Name, Count), - handleCast({mWriteLog, Event}, State); - {ok, {NewFD, NewInode, NewCtime, _Size}} -> - TimeBinStr = rumUtil:msToBinStr(), - Time = [TimeBinStr, " =", ReportStr, "====\n"], - NodeSuffix = other_node_suffix(Pid), - Msg = io_lib:format("~s~s~s", [Time, MsgStr, NodeSuffix]), - case file:write(NewFD, unicode:characters_to_binary(Msg)) of - {error, Reason} when Flap == false -> - ?INT_LOG(error, "Failed to write log message to file ~ts: ~s", - [Name, file:format_error(Reason)]), - {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime, flap = true}}; - ok -> - {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime, flap = false}}; + case ReportStr of + ignore -> + {ok, State}; + _ -> + case Rotator:ensureLogfile(FileName, FD, Inode, Ctime, false) of + {ok, NewFD, NewInode, NewCtime, FileSize} -> + case RotSize > 0 andalso FileSize > RotSize of + true -> + _ = Rotator:rotateLogfile(FileName, Count), + handleCast({mWriteLog, Event}, State); _ -> - {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime}} + TimeBinStr = rumUtil:msToBinStr(), + Time = [TimeBinStr, <<" =">>, ReportStr, <<"====\n">>], + NodeSuffix = otherNodeSuffix(Pid), + Msg = eFmt:formatBin("~s~s~s", [Time, MsgStr, NodeSuffix]), + case file:write(NewFD, unicode:characters_to_binary(Msg)) of + {error, Reason} when Flap == false -> + ?INT_LOG(error, "Failed to write log message to file ~ts: ~s", [FileName, file:format_error(Reason)]), + {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime, flap = true}}; + ok -> + {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime, flap = false}}; + _ -> + {ok, State#state{fd = NewFD, inode = NewInode, ctime = NewCtime}} + end end; {error, Reason} -> - case Flap of - true -> - {ok, State}; - _ -> - ?INT_LOG(error, "Failed to reopen crash log ~ts with error: ~s", - [Name, file:format_error(Reason)]), - {ok, State#state{flap = true}} - end + ?IIF(Flap, {ok, State}, begin ?INT_LOG(error, "Failed to reopen crash log ~ts with error: ~s", [FileName, file:format_error(Reason)]), {ok, State#state{flap = true}} end) end end. diff --git a/src/utils/rumStdlib.erl b/src/utils/rumStdlib.erl index 95e60f8..44a913c 100644 --- a/src/utils/rumStdlib.erl +++ b/src/utils/rumStdlib.erl @@ -2,7 +2,7 @@ -export([string_p/1]). -export([write_time/2, maybe_utc/1]). --export([is_my_error_report/1, is_my_info_report/1]). +-export([isErrorReport/1, is_my_info_report/1]). -export([sup_get/2]). -export([proc_lib_format/2]). @@ -92,10 +92,10 @@ month(12) -> "Dec". %% From OTP sasl's sasl_report.erl ... These functions aren't %% exported. --spec is_my_error_report(atom()) -> boolean(). -is_my_error_report(supervisor_report) -> true; -is_my_error_report(crash_report) -> true; -is_my_error_report(_) -> false. +-spec isErrorReport(atom()) -> boolean(). +isErrorReport(supervisor_report) -> true; +isErrorReport(crash_report) -> true; +isErrorReport(_) -> false. -spec is_my_info_report(atom()) -> boolean(). is_my_info_report(progress) -> true;