1%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20-module(logger_test_lib).
21
22-include_lib("kernel/src/logger_internal.hrl").
23
24-export([setup/2, log/3, sync_and_read/3]).
25
26-export([init/2,
27         pre_init_per_suite/3, pre_init_per_testcase/4,
28         post_end_per_testcase/5, post_end_per_suite/3]).
29
30setup(Config,Vars) ->
31    Postfix = case proplists:get_value(postfix, Config) of
32                  undefined -> "";
33                  P -> ["_",P]
34              end,
35    FuncStr = lists:concat([proplists:get_value(suite, Config), "_",
36                            proplists:get_value(tc, Config)|
37                            Postfix]),
38    ConfigFileName = filename:join(proplists:get_value(priv_dir, Config), FuncStr),
39    file:write_file(ConfigFileName ++ ".config", io_lib:format("[{kernel, ~p}].",[Vars])),
40    Sname = lists:concat([proplists:get_value(tc,Config)|Postfix]),
41    case test_server:start_node(Sname, slave,
42                                [{args, ["-pa ",filename:dirname(code:which(?MODULE)),
43                                         " -boot start_sasl -kernel start_timer true "
44                                         "-config ",ConfigFileName]}]) of
45        {ok, Node} ->
46            L = rpc:call(Node, logger, get_config, []),
47            ct:log("~p",[L]),
48            {ok, L, Node};
49        {error, Reason} ->
50            ct:log("Failed to start node: ~p",[Reason]),
51            error
52    end.
53
54log(Node, F, A) ->
55    log(Node, logger, F, A).
56log(Node, M, F, A) ->
57    MD = #{ gl => rpc:call(Node, erlang, whereis, [logger]) },
58    rpc:call(Node, M, F, A ++ [MD]).
59
60sync_and_read(Node,disk_log,Log) ->
61    rpc:call(Node,logger_disk_log_h,filesync,[?STANDARD_HANDLER]),
62    file:read_file(Log ++ ".1");
63sync_and_read(Node, file,Log) ->
64    ok = rpc:call(Node,logger_std_h,filesync,[?STANDARD_HANDLER]),
65    file:read_file(Log).
66
67
68init(_, _) ->
69    {ok, []}.
70
71pre_init_per_suite(_Suite, Config, State) ->
72    {[{nodes, nodes()} | Config], State}.
73
74pre_init_per_testcase(Suite, TC, Config, State) ->
75    cleanup(Config),
76    {[{suite, Suite}, {tc, TC} | Config], State}.
77
78post_end_per_testcase(_, _TC, Config, Res, State) ->
79    cleanup(Config),
80    {Res, State}.
81
82post_end_per_suite(_, Config, State) ->
83    cleanup(Config),
84    {Config, State}.
85
86cleanup(Config) ->
87    [test_server:stop_node(N) || N <- nodes(),
88                                 not lists:member(N, proplists:get_value(nodes, Config))].
89