Skip to content

Commit

Permalink
common_test: header option for cth_conn_log hook module
Browse files Browse the repository at this point in the history
  • Loading branch information
u3s committed Oct 9, 2024
1 parent 928d03e commit e6df096
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
15 changes: 10 additions & 5 deletions lib/common_test/src/ct_conn_log_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
-export([init/1,
handle_event/2, handle_call/2, handle_info/2,
terminate/2]).

-record(state, {logs=[], default_gl}).
-record(state, {logs=[], default_gl, header=false}).

-define(WIDTH,80).

-define(now, os:timestamp()).

%%%-----------------------------------------------------------------
%%% Callbacks
init({GL,ConnLogs}) ->
open_files(GL,ConnLogs,#state{default_gl=GL}).
init({GL,ConnLogs,Opts}) ->
open_files(GL,ConnLogs,#state{default_gl=GL,
header = proplists:get_value(header, Opts)}).

open_files(GL,[{ConnMod,{LogType,LogFiles}}|T],State=#state{logs=Logs}) ->
case do_open_files(LogFiles,[]) of
Expand Down Expand Up @@ -103,6 +103,9 @@ terminate(_,#state{logs=Logs}) ->

%%%-----------------------------------------------------------------
%%% Writing reports
write_report(Time,#conn_log{header=false,module=ConnMod}=Info,Data,GL,
#state{header=true}=State) ->
write_report_with_header(Info, GL, State, ConnMod, Data, Time);
write_report(_Time,#conn_log{header=false,module=ConnMod}=Info,Data,GL,State) ->
case get_log(Info,GL,State) of
{silent,_,_} ->
Expand All @@ -113,8 +116,10 @@ write_report(_Time,#conn_log{header=false,module=ConnMod}=Info,Data,GL,State) ->
end,
io:format(Fd,Str,[format_data(ConnMod,LogType,Data)])
end;

write_report(Time,#conn_log{module=ConnMod}=Info,Data,GL,State) ->
write_report_with_header(Info, GL, State, ConnMod, Data, Time).

write_report_with_header(Info, GL, State, ConnMod, Data, Time) ->
case get_log(Info,GL,State) of
{silent,_,_} ->
ok;
Expand Down
9 changes: 4 additions & 5 deletions lib/common_test/src/cth_conn_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ get_log_opts(Mod,Opts) ->
end,
LogType = proplists:get_value(log_type,Opts,DefaultLogType),
Hosts = proplists:get_value(hosts,Opts,[]),
{LogType,Hosts}.
{LogType,Hosts,[{header, proplists:get_value(header,Opts,false)}]}.

pre_init_per_testcase(_Suite,TestCase,Config,CthState) ->
pre_init_per_testcase(_Suite,TestCase,Config,[{_, {_, _, Opts}}] = CthState) ->
Logs =
lists:map(
fun({ConnMod,{LogType,Hosts}}) ->
fun({ConnMod,{LogType,Hosts, _Opts}}) ->
ct_util:set_testdata({{?MODULE,ConnMod},LogType}),
case LogType of
LogType when LogType==raw; LogType==pretty ->
Expand Down Expand Up @@ -128,11 +128,10 @@ pre_init_per_testcase(_Suite,TestCase,Config,CthState) ->
end
end,
CthState),

GL = group_leader(),
Update =
fun(Init) when Init == undefined; Init == [] ->
error_logger:add_report_handler(ct_conn_log_h,{GL,Logs}),
error_logger:add_report_handler(ct_conn_log_h,{GL,Logs,Opts}),
[TestCase];
(PrevUsers) ->
error_logger:info_report(update,{GL,Logs}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
%%--------------------------------------------------------------------

suite() ->
[
{require,telnet_server_conn1,{unix,[telnet]}},
[{require,telnet_server_conn1,{unix,[telnet]}},
{require,ct_conn_log},
{ct_hooks, [{cth_conn_log,[]}]}
{ct_hooks, [{cth_conn_log,[{ct_telnet, [{header, true}]}]}]}
].

all() ->
Expand Down

0 comments on commit e6df096

Please sign in to comment.