Skip to content

Commit f7c7181

Browse files
author
Evgeny Bob
authored
Merge pull request #2 from alertlogic/scan-format
remove new-lines the otp logger way
2 parents 5b0dfac + e81dc77 commit f7c7181

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

src/erllambda_error_handler.erl

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
-author('Anton Zaets <[email protected]>').
1111
-author('Evgeny Bob <[email protected]>').
1212

13+
-ifdef(TEST).
14+
-include_lib("eunit/include/eunit.hrl").
15+
-endif.
16+
1317
-behaviour(gen_event).
1418

1519
%% ------------------------------------------------------------------
@@ -49,14 +53,14 @@ handle_event({EventType, _Gleader,
4953
when EventType =:= error;
5054
EventType =:= warning_msg;
5155
EventType =:= info_msg ->
52-
output(io_lib:format(Format, Data)),
56+
output(Format, Data),
5357
{ok, State};
5458
handle_event({EventType, _Gleader,
5559
{_Pid, _Type, Report}}, State)
5660
when EventType =:= error_report;
5761
EventType =:= warning_report;
5862
EventType =:= info_report ->
59-
output(io_lib:format("~p", [Report])),
63+
output("~p", [Report]),
6064
{ok, State};
6165
handle_event(_, State) ->
6266
{ok, State}.
@@ -77,21 +81,61 @@ code_change(_OldVsn, State, _Extra) ->
7781
%% ------------------------------------------------------------------
7882
%% Internal Function Definitions
7983
%% ------------------------------------------------------------------
80-
output(Str) ->
81-
io:fwrite("~s~n", [nonl(Str)]).
82-
83-
%% replace all '\n' with space and consume all the whitespace after '\n'
84-
nonl(S) ->
85-
nonl(S, []).
86-
87-
nonl([], Accu) ->
88-
lists:reverse(Accu);
89-
nonl([$\n|T], Accu) ->
90-
nonl(nows(T), [$\ | Accu]);
91-
nonl([H|T], Accu) ->
92-
nonl(T, [H | Accu]).
93-
94-
%% remove all the leading whitespace
95-
nows([]) -> [];
96-
nows([$\ | T]) -> nows(T);
97-
nows(T) -> T.
84+
output(Format, Data) ->
85+
io:fwrite(format(Format, Data)).
86+
87+
format(Format, Data) ->
88+
Format1 = io_lib:scan_format(Format, Data),
89+
Format2 = reformat(Format1),
90+
Text = io_lib:build_text(Format2),
91+
one_line_it(Text).
92+
93+
reformat(Format) ->
94+
reformat(Format, _Width = 134217721).
95+
96+
reformat([#{control_char := C} = M | T], Width) when C =:= $p; C =:= $P ->
97+
[M#{width => Width} | reformat(T, Width)];
98+
reformat([H | T], Width) ->
99+
[H | reformat(T, Width)];
100+
reformat([], Width) ->
101+
[].
102+
103+
one_line_it(Text) ->
104+
re:replace(string:trim(Text), "\r?\n\s*", " ", [{return,list},global,unicode]).
105+
106+
107+
%%====================================================================
108+
%% Test Functions
109+
%%====================================================================
110+
-ifdef(TEST).
111+
112+
format_test_() ->
113+
MultilineJson =
114+
<<"[\n"
115+
" {\n"
116+
" \"_id\": \"5be7f5f8e8c1ed9241898c1a\",\n"
117+
" \"index\": 0,\n"
118+
" \"guid\": \"581599e9-4f5d-45c7-9c5c-2bd5611f59fd\",\n"
119+
" \"isActive\": false,\n"
120+
" \"balance\": \"$3,819.73\",\n"
121+
" \"picture\": \"http://placehold.it/32x32\",\n"
122+
" \"age\": 21,\n"
123+
" \"eyeColor\": \"blue\",\n"
124+
" \"name\": {\n"
125+
" \"first\": \"Marcie\",\n"
126+
" \"last\": \"Byrd\"\n"
127+
" }\n"
128+
" }\n"
129+
"]\n">>,
130+
[
131+
?_assertEqual("List [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,"
132+
"21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,"
133+
"41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,"
134+
"61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,"
135+
"81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]",
136+
format("List ~p", [lists:seq(1, 100)])),
137+
?_assertEqual("Multiline Json [ { \"_id\": \"5be7f5f8e8c1ed9241898c1a\", \"index\": 0, \"guid\": \"581599e9-4f5d-45c7-9c5c-2bd5611f59fd\", \"isActive\": false, \"balance\": \"$3,819.73\", \"picture\": \"http://placehold.it/32x32\", \"age\": 21, \"eyeColor\": \"blue\", \"name\": { \"first\": \"Marcie\", \"last\": \"Byrd\" } } ]",
138+
format("Multiline Json ~s", [MultilineJson]))
139+
].
140+
141+
-endif.

0 commit comments

Comments
 (0)