aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/test/logger_legacy_SUITE.erl
blob: a0d133b7e9cbfc5fbdab55cd09d9c9f32106bda7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(logger_legacy_SUITE).

-compile(export_all).
-compile({nowarn_deprecated_function,[{gen_fsm,start,3},
                                      {gen_fsm,send_all_state_event,2}]}).

-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/logger.hrl").
-include_lib("kernel/src/logger_internal.hrl").

%%%-----------------------------------------------------------------
%%% This test suite test that log events from within OTP can be
%%% delivered to legacy error_logger event handlers on the same format
%%% as before 'logger' was introduced.
%%%
%%% Before changing the expected format of any of the log events in
%%% this suite, please make sure that the backwards incompatibility it
%%% introduces is ok.
%%% -----------------------------------------------------------------

-define(check(Expected),
        receive Expected ->
                [] = test_server:messages_get()
        after 1000 ->
                ct:fail({report_not_received,
                         {line,?LINE},
                         {got,test_server:messages_get()}})
        end).
-define(check_no_flush(Expected),
        receive Expected ->
                ok
        after 1000 ->
                ct:fail({report_not_received,
                         {line,?LINE},
                         {got,test_server:messages_get()}})
        end).

suite() ->
    [{timetrap,{seconds,30}}].

init_per_suite(Config) ->
    logger:add_handler(error_logger,error_logger,
                       #{level=>info,filter_default=>stop}),
    Config.

end_per_suite(_Config) ->
    logger:remove_handler(error_logger),
    ok.

init_per_group(std, Config) ->
    ok = logger:set_handler_config(
           error_logger,filters,
           [{domain,{fun logger_filters:domain/2,{log,super,[otp]}}}]),
    Config;
init_per_group(sasl, Config) ->
    %% Since default level is notice, and progress reports are info,
    %% we need to raise the global logger level to info in order to
    %% receive these.
    ok = logger:set_primary_config(level,info),
    ok = logger:set_handler_config(
           error_logger,filters,
           [{domain,{fun logger_filters:domain/2,{log,super,[otp,sasl]}}}]),

    %% cth_log_redirect checks if sasl is started before displaying
    %% any sasl reports - so just to see the real sasl reports in tc
    %% log:
    {ok,Apps} = application:ensure_all_started(sasl),
    [{stop_apps,Apps}|Config];
init_per_group(_Group, Config) ->
    Config.

end_per_group(sasl, Config) ->
    Apps = ?config(stop_apps,Config),
    [application:stop(App) || App <- Apps],
    ok;
end_per_group(_Group, _Config) ->
    ok.

init_per_testcase(_TestCase, Config) ->
    error_logger:add_report_handler(?MODULE,{event_handler,self()}),
    Config.

end_per_testcase(Case, Config) ->
    %% Using gen_event directly here, instead of
    %% error_logger:delete_report_handler. This is to avoid
    %% automatically stopping the error_logger process due to removing
    %% the last handler.
    gen_event:delete_handler(error_logger,?MODULE,[]),
    try apply(?MODULE,Case,[cleanup,Config])
    catch error:undef -> ok
    end,
    ok.

groups() ->
    [{std,[],[gen_server,
              gen_event,
              gen_fsm,
              gen_statem]},
     {sasl,[],[sasl_reports,
               supervisor_handle_info]}].

all() ->
    [{group,std},
     {group,sasl}].

gen_server(_Config) ->
    {ok,Pid} = gen_server:start(?MODULE,gen_server,[]),
    Msg = fun() -> erlang:error({badmatch,b}) end,
    Pid ! Msg,
    ?check({warning_msg,"** Undefined handle_info in ~p"++_,[?MODULE,Msg]}),
    ok = gen_server:cast(Pid,Msg),
    ?check({error,"** Generic server ~tp terminating"++_,
            [Pid,{'$gen_cast',Msg},gen_server,{{badmatch,b},_}]}).

gen_event(_Config) ->
    {ok,Pid} = gen_event:start(),
    ok = gen_event:add_handler(Pid,?MODULE,gen_event),
    Msg = fun() -> erlang:error({badmatch,b}) end,
    Pid ! Msg,
    ?check({warning_msg,"** Undefined handle_info in ~tp"++_,[?MODULE,Msg]}),
    gen_event:notify(Pid,Msg),
    ?check({error,"** gen_event handler ~p crashed."++_,
            [?MODULE,Pid,Msg,gen_event,{{badmatch,b},_}]}).

gen_fsm(_Config) ->
    {ok,Pid} = gen_fsm:start(?MODULE,gen_fsm,[]),
    Msg = fun() -> erlang:error({badmatch,b}) end,
    Pid ! Msg,
    ?check({warning_msg,"** Undefined handle_info in ~p"++_,[?MODULE,Msg]}),
    gen_fsm:send_all_state_event(Pid,Msg),
    ?check({error,"** State machine ~tp terminating"++_,
            [Pid,Msg,mystate,gen_fsm,{{badmatch,b},_}]}).

gen_statem(_Config) ->
    {ok,Pid} = gen_statem:start(?MODULE,gen_statem,[]),
    Msg = fun() -> erlang:error({badmatch,b}) end,
    Pid ! Msg,
    ?check({error,"** State machine ~tp terminating"++_,
            [Pid,{info,Msg},{mystate,gen_statem},error,{badmatch,b}|_]}).

sasl_reports(Config) ->
    App = {application,?MODULE,[{description, ""},
                                {vsn, "1.0"},
                                {modules, [?MODULE]},
                                {registered, []},
                                {applications, []},
                                {mod, {?MODULE, []}}]},
    AppStr = io_lib:format("~p.",[App]),
    Dir = ?config(priv_dir,Config),
    AppFile = filename:join(Dir,?MODULE_STRING++".app"),
    ok = file:write_file(AppFile,AppStr),
    true = code:add_patha(Dir),
    ok = application:start(?MODULE),
    SupName = sup_name(),
    Pid = whereis(SupName),
    [{ch,ChPid,_,_}] = supervisor:which_children(Pid),
    Node = node(),
    ?check_no_flush({info_report,progress,[{application,?MODULE},
                                           {started_at,Node}]}),
    ?check({info_report,progress,[{supervisor,{local,SupName}},
                                  {started,[{pid,ChPid}|_]}]}),
    ok = gen_server:cast(ChPid, fun() ->
                                        spawn_link(fun() -> receive x->ok end end)
                                end),
    Msg = fun() -> erlang:error({badmatch,b}) end,
    ok = gen_server:cast(ChPid,Msg),
    ?check_no_flush({error,"** Generic server ~tp terminating"++_,
                     [ChPid,{'$gen_cast',Msg},gen_server,{{badmatch,b},_}]}),
    ?check_no_flush({error_report,crash_report,
                     [[{initial_call,_},
                       {pid,ChPid},
                       {registered_name,[]},
                       {error_info,{error,{badmatch,b},_}},
                       {ancestors,_},
                       {message_queue_len,_},
                       {messages,_},
                       {links,[Pid,Neighbour]},
                       {dictionary,_},
                       {trap_exit,_},
                       {status,_},
                       {heap_size,_},
                       {stack_size,_},
                       {reductions,_}],
                      [{neighbour,[{pid,Neighbour},
                                   {registered_name,_},
                                   {initial_call,_},
                                   {current_function,_},
                                   {ancestors,_},
                                   {message_queue_len,_},
                                   {links,[ChPid]},
                                   {trap_exit,_},
                                   {status,_},
                                   {heap_size,_},
                                   {stack_size,_},
                                   {reductions,_},
                                   {current_stacktrace,_}]}]]}),
    ?check_no_flush({error_report,supervisor_report,
                     [{supervisor,{local,SupName}},
                      {errorContext,child_terminated},
                      {reason,{{badmatch,b},_}},
                      {offender,[{pid,ChPid}|_]}]}),
    ?check({info_report,progress,[{supervisor,{local,SupName}},
                                  {started,_}]}),

    ok = application:stop(?MODULE),
    ?check({info_report,std_info,[{application,?MODULE},
                                  {exited,stopped},
                                  {type,temporary}]}).

sasl_reports(cleanup,_Config) ->
    application:stop(?MODULE).

supervisor_handle_info(_Config) ->
    {ok,Pid} = supervisor:start_link({local,sup_name()},?MODULE,supervisor),
    ?check({info_report,progress,[{supervisor,_},{started,_}]}),
    Pid ! msg,
    ?check({error,"Supervisor received unexpected message: ~tp~n",[msg]}).

supervisor_handle_info(cleanup,_Config) ->
    Pid = whereis(sup_name()),
    unlink(Pid),
    exit(Pid,shutdown).

%%%-----------------------------------------------------------------
%%% Callbacks for error_logger event handler, gen_server, gen_statem,
%%% gen_fsm, gen_event, supervisor and application.
start(_,_) ->
    supervisor:start_link({local,sup_name()},?MODULE,supervisor).

init(supervisor) ->
    {ok,{#{},[#{id=>ch,start=>{gen_server,start_link,[?MODULE,gen_server,[]]}}]}};
init(StateMachine) when StateMachine==gen_statem; StateMachine==gen_fsm ->
    {ok,mystate,StateMachine};
init(State) ->
    {ok,State}.

%% error_logger event handler
handle_event({Tag,_Gl,{_Pid,Type,Report}},{_,Pid}=State) ->
    Pid ! {Tag,Type,Report},
    {ok,State};
%% other gen_event
handle_event(Fun,State) when is_function(Fun) ->
    Fun(),
    {next_state,State}.

%% gen_fsm
handle_event(Fun,State,Data) when is_function(Fun)  ->
    Fun(),
    {next_state,State,Data}.

%% gen_statem
handle_event(info,Fun,State,Data) when is_function(Fun)  ->
    Fun(),
    {next_state,State,Data}.

%% gen_server
handle_cast(Fun,State) when is_function(Fun)  ->
    Fun(),
    {noreply,State}.

%% gen_statem
callback_mode() ->
    handle_event_function.

%%%-----------------------------------------------------------------
%%% Internal
sup_name() ->
    list_to_atom(?MODULE_STRING++"_sup").