10
10
-
author (
'Anton Zaets <[email protected] >' ).
11
11
-
author (
'Evgeny Bob <[email protected] >' ).
12
12
13
+ -ifdef (TEST ).
14
+ -include_lib (" eunit/include/eunit.hrl" ).
15
+ -endif .
16
+
13
17
-behaviour (gen_event ).
14
18
15
19
% % ------------------------------------------------------------------
@@ -49,14 +53,14 @@ handle_event({EventType, _Gleader,
49
53
when EventType =:= error ;
50
54
EventType =:= warning_msg ;
51
55
EventType =:= info_msg ->
52
- output (io_lib : format ( Format , Data ) ),
56
+ output (Format , Data ),
53
57
{ok , State };
54
58
handle_event ({EventType , _Gleader ,
55
59
{_Pid , _Type , Report }}, State )
56
60
when EventType =:= error_report ;
57
61
EventType =:= warning_report ;
58
62
EventType =:= info_report ->
59
- output (io_lib : format ( " ~p " , [Report ]) ),
63
+ output (" ~p " , [Report ]),
60
64
{ok , State };
61
65
handle_event (_ , State ) ->
62
66
{ok , State }.
@@ -77,21 +81,61 @@ code_change(_OldVsn, State, _Extra) ->
77
81
% % ------------------------------------------------------------------
78
82
% % Internal Function Definitions
79
83
% % ------------------------------------------------------------------
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