Skip to content

Commit 003091a

Browse files
committed
error_logger_file_h: Refactor and modernize code
Refactor, simplify, and modernize the code to facilitate future improvements in the following commits.
1 parent 7dbd3ca commit 003091a

File tree

1 file changed

+78
-86
lines changed

1 file changed

+78
-86
lines changed

lib/stdlib/src/error_logger_file_h.erl

Lines changed: 78 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@
3434
handle_event/2, handle_call/2, handle_info/2,
3535
terminate/2, code_change/3]).
3636

37+
-record(st,
38+
{fd,
39+
filename,
40+
prev_handler}).
41+
3742
%% This one is used when we takeover from the simple error_logger.
3843
init({File, {error_logger, Buf}}) ->
3944
case init(File, error_logger) of
40-
{ok, {Fd, File, PrevHandler}} ->
41-
write_events(Fd, Buf),
42-
{ok, {Fd, File, PrevHandler}};
45+
{ok, State} ->
46+
write_events(State, Buf),
47+
{ok, State};
4348
Error ->
4449
Error
4550
end;
@@ -51,20 +56,18 @@ init(File, PrevHandler) ->
5156
process_flag(trap_exit, true),
5257
case file:open(File, [write]) of
5358
{ok,Fd} ->
54-
{ok, {Fd, File, PrevHandler}};
59+
{ok, #st{fd=Fd,filename=File,prev_handler=PrevHandler}};
5560
Error ->
5661
Error
5762
end.
5863

5964
handle_event({_Type, GL, _Msg}, State) when node(GL) =/= node() ->
6065
{ok, State};
61-
handle_event(Event, {Fd, File, PrevHandler}) ->
62-
write_event(Fd, tag_event(Event)),
63-
{ok, {Fd, File, PrevHandler}};
64-
handle_event(_, State) ->
66+
handle_event(Event, State) ->
67+
write_event(State, Event),
6568
{ok, State}.
6669

67-
handle_info({'EXIT', Fd, _Reason}, {Fd, _File, PrevHandler}) ->
70+
handle_info({'EXIT', Fd, _Reason}, #st{fd=Fd,prev_handler=PrevHandler}) ->
6871
case PrevHandler of
6972
[] ->
7073
remove_handler;
@@ -74,8 +77,8 @@ handle_info({'EXIT', Fd, _Reason}, {Fd, _File, PrevHandler}) ->
7477
handle_info(_, State) ->
7578
{ok, State}.
7679

77-
handle_call(filename, {Fd, File, Prev}) ->
78-
{ok, File, {Fd, File, Prev}};
80+
handle_call(filename, #st{filename=File}=State) ->
81+
{ok, File, State};
7982
handle_call(_Query, State) ->
8083
{ok, {error, bad_query}, State}.
8184

@@ -95,58 +98,55 @@ code_change(_OldVsn, State, _Extra) ->
9598
%%% Misc. functions.
9699
%%% ------------------------------------------------------
97100

98-
tag_event(Event) ->
99-
{erlang:universaltime(), Event}.
101+
write_events(State, [Ev|Es]) ->
102+
%% Write the events in reversed order.
103+
write_events(State, Es),
104+
write_event(State, Ev);
105+
write_events(_State, []) ->
106+
ok.
100107

101-
write_events(Fd, Events) -> write_events1(Fd, lists:reverse(Events)).
108+
write_event(#st{fd=Fd}=State, Event) ->
109+
case parse_event(Event) of
110+
ignore ->
111+
ok;
112+
{Head,Pid,FormatList} ->
113+
Time = maybe_utc(erlang:universaltime()),
114+
Header = write_time(Time, Head),
115+
Body = format_body(State, FormatList),
116+
AtNode = if
117+
node(Pid) =/= node() ->
118+
["** at node ",atom_to_list(node(Pid))," **\n"];
119+
true ->
120+
[]
121+
end,
122+
io:put_chars(Fd, [Header,Body,AtNode])
123+
end.
102124

103-
write_events1(Fd, [Event|Es]) ->
104-
write_event(Fd, Event),
105-
write_events1(Fd, Es);
106-
write_events1(_, []) ->
107-
ok.
125+
format_body(State, [{Format,Args}|T]) ->
126+
S = try io_lib:format(Format, Args) of
127+
S0 ->
128+
S0
129+
catch
130+
_:_ ->
131+
io_lib:format("ERROR: ~p - ~p\n", [Format,Args])
132+
end,
133+
[S|format_body(State, T)];
134+
format_body(_State, []) ->
135+
[].
108136

109-
write_event(Fd, {Time, {error, _GL, {Pid, Format, Args}}}) ->
110-
T = write_time(maybe_utc(Time)),
111-
case catch io_lib:format(add_node(Format,Pid), Args) of
112-
S when is_list(S) ->
113-
io:format(Fd, T ++ S, []);
114-
_ ->
115-
F = add_node("ERROR: ~p - ~p~n", Pid),
116-
io:format(Fd, T ++ F, [Format,Args])
117-
end;
118-
write_event(Fd, {Time, {error_report, _GL, {Pid, std_error, Rep}}}) ->
119-
T = write_time(maybe_utc(Time)),
120-
S = format_report(Rep),
121-
io:format(Fd, T ++ S ++ add_node("", Pid), []);
122-
write_event(Fd, {Time, {info_report, _GL, {Pid, std_info, Rep}}}) ->
123-
T = write_time(maybe_utc(Time), "INFO REPORT"),
124-
S = format_report(Rep),
125-
io:format(Fd, T ++ S ++ add_node("", Pid), []);
126-
write_event(Fd, {Time, {info_msg, _GL, {Pid, Format, Args}}}) ->
127-
T = write_time(maybe_utc(Time), "INFO REPORT"),
128-
case catch io_lib:format(add_node(Format,Pid), Args) of
129-
S when is_list(S) ->
130-
io:format(Fd, T ++ S, []);
131-
_ ->
132-
F = add_node("ERROR: ~p - ~p~n", Pid),
133-
io:format(Fd, T ++ F, [Format,Args])
134-
end;
135-
write_event(Fd, {Time, {warning_report, _GL, {Pid, std_warning, Rep}}}) ->
136-
T = write_time(maybe_utc(Time), "WARNING REPORT"),
137-
S = format_report(Rep),
138-
io:format(Fd, T ++ S ++ add_node("", Pid), []);
139-
write_event(Fd, {Time, {warning_msg, _GL, {Pid, Format, Args}}}) ->
140-
T = write_time(maybe_utc(Time), "WARNING REPORT"),
141-
case catch io_lib:format(add_node(Format,Pid), Args) of
142-
S when is_list(S) ->
143-
io:format(Fd, T ++ S, []);
144-
_ ->
145-
F = add_node("ERROR: ~p - ~p~n", Pid),
146-
io:format(Fd, T ++ F, [Format,Args])
147-
end;
148-
write_event(_, _) ->
149-
ok.
137+
parse_event({error, _GL, {Pid, Format, Args}}) ->
138+
{"ERROR REPORT",Pid,[{Format,Args}]};
139+
parse_event({info_msg, _GL, {Pid, Format, Args}}) ->
140+
{"INFO REPORT",Pid,[{Format, Args}]};
141+
parse_event({warning_msg, _GL, {Pid, Format, Args}}) ->
142+
{"WARNING REPORT",Pid,[{Format,Args}]};
143+
parse_event({error_report, _GL, {Pid, std_error, Args}}) ->
144+
{"ERROR REPORT",Pid,format_term(Args)};
145+
parse_event({info_report, _GL, {Pid, std_info, Args}}) ->
146+
{"INFO REPORT",Pid,format_term(Args)};
147+
parse_event({warning_report, _GL, {Pid, std_warning, Args}}) ->
148+
{"WARNING REPORT",Pid,format_term(Args)};
149+
parse_event(_) -> ignore.
150150

151151
maybe_utc(Time) ->
152152
UTC = case application:get_env(sasl, utc_log) of
@@ -163,30 +163,27 @@ maybe_utc(Time) ->
163163
maybe_utc(Time, true) -> {utc, Time};
164164
maybe_utc(Time, _) -> {local, calendar:universal_time_to_local_time(Time)}.
165165

166-
format_report(Rep) when is_list(Rep) ->
167-
case string_p(Rep) of
166+
format_term(Term) when is_list(Term) ->
167+
case string_p(Term) of
168168
true ->
169-
io_lib:format("~s~n",[Rep]);
170-
_ ->
171-
format_rep(Rep)
169+
[{"~s\n",[Term]}];
170+
false ->
171+
format_term_list(Term)
172172
end;
173-
format_report(Rep) ->
174-
io_lib:format("~p~n",[Rep]).
175-
176-
format_rep([{Tag,Data}|Rep]) ->
177-
io_lib:format(" ~p: ~p~n",[Tag,Data]) ++ format_rep(Rep);
178-
format_rep([Other|Rep]) ->
179-
io_lib:format(" ~p~n",[Other]) ++ format_rep(Rep);
180-
format_rep(_) ->
173+
format_term(Term) ->
174+
[{"~p\n",[Term]}].
175+
176+
format_term_list([{Tag,Data}|T]) ->
177+
[{" ~p: ~p\n",[Tag,Data]}|format_term_list(T)];
178+
format_term_list([Data|T]) ->
179+
[{" ~p\n",[Data]}|format_term_list(T)];
180+
format_term_list([]) ->
181+
[];
182+
format_term_list(_) ->
183+
%% Continue to allow non-proper lists for now.
184+
%% FIXME: Remove this clause in OTP 19.
181185
[].
182186

183-
add_node(X, Pid) when is_atom(X) ->
184-
add_node(atom_to_list(X), Pid);
185-
add_node(X, Pid) when node(Pid) =/= node() ->
186-
lists:concat([X,"** at node ",node(Pid)," **~n"]);
187-
add_node(X, _) ->
188-
X.
189-
190187
string_p([]) ->
191188
false;
192189
string_p(Term) ->
@@ -202,15 +199,10 @@ string_p1([$\b|T]) -> string_p1(T);
202199
string_p1([$\f|T]) -> string_p1(T);
203200
string_p1([$\e|T]) -> string_p1(T);
204201
string_p1([H|T]) when is_list(H) ->
205-
case string_p1(H) of
206-
true -> string_p1(T);
207-
_ -> false
208-
end;
202+
string_p1(H) andalso string_p1(T);
209203
string_p1([]) -> true;
210204
string_p1(_) -> false.
211205

212-
write_time(Time) -> write_time(Time, "ERROR REPORT").
213-
214206
write_time({utc,{{Y,Mo,D},{H,Mi,S}}}, Type) ->
215207
io_lib:format("~n=~s==== ~p-~s-~p::~s:~s:~s UTC ===~n",
216208
[Type,D,month(Mo),Y,t(H),t(Mi),t(S)]);

0 commit comments

Comments
 (0)