From e6df0967bb984726b18e407190df7fed5fa0fc20 Mon Sep 17 00:00:00 2001 From: Jakub Witczak Date: Wed, 9 Oct 2024 18:55:05 +0200 Subject: [PATCH] common_test: header option for cth_conn_log hook module --- lib/common_test/src/ct_conn_log_h.erl | 15 ++++++++++----- lib/common_test/src/cth_conn_log.erl | 9 ++++----- .../ct_telnet_own_server_SUITE.erl | 5 ++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/common_test/src/ct_conn_log_h.erl b/lib/common_test/src/ct_conn_log_h.erl index 3e83020f45eb..d62f83945736 100644 --- a/lib/common_test/src/ct_conn_log_h.erl +++ b/lib/common_test/src/ct_conn_log_h.erl @@ -30,8 +30,7 @@ -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). @@ -39,8 +38,9 @@ %%%----------------------------------------------------------------- %%% 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 @@ -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,_,_} -> @@ -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; diff --git a/lib/common_test/src/cth_conn_log.erl b/lib/common_test/src/cth_conn_log.erl index 744642c69fd4..69316055397a 100644 --- a/lib/common_test/src/cth_conn_log.erl +++ b/lib/common_test/src/cth_conn_log.erl @@ -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 -> @@ -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}), diff --git a/lib/common_test/test/ct_telnet_SUITE_data/ct_telnet_own_server_SUITE.erl b/lib/common_test/test/ct_telnet_SUITE_data/ct_telnet_own_server_SUITE.erl index 34df57027ebd..8613289167ef 100644 --- a/lib/common_test/test/ct_telnet_SUITE_data/ct_telnet_own_server_SUITE.erl +++ b/lib/common_test/test/ct_telnet_SUITE_data/ct_telnet_own_server_SUITE.erl @@ -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() ->