diff --git a/lib/common_test/src/ct_conn_log_h.erl b/lib/common_test/src/ct_conn_log_h.erl index 216a8b7615ae..3959baedc545 100644 --- a/lib/common_test/src/ct_conn_log_h.erl +++ b/lib/common_test/src/ct_conn_log_h.erl @@ -31,8 +31,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, prefix=disabled}). -define(WIDTH,80). @@ -40,8 +39,10 @@ %%%----------------------------------------------------------------- %%% Callbacks -init({GL,ConnLogs}) -> - open_files(GL,ConnLogs,#state{default_gl=GL}). +init({GL,ConnLogs,Opts}) -> + open_files(GL,ConnLogs, + #state{default_gl = GL, + prefix = proplists:get_value(prefix, Opts, disabled)}). open_files(GL,[{ConnMod,{LogType,LogFiles}}|T],State=#state{logs=Logs}) -> case do_open_files(LogFiles,[]) of @@ -104,6 +105,10 @@ terminate(_,#state{logs=Logs}) -> %%%----------------------------------------------------------------- %%% Writing reports +write_report(Time,#conn_log{header=false,module=ConnMod}=Info,Data,GL, + #state{prefix=PrefixType}=State) + when PrefixType==full;PrefixType==short -> + 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,_,_} -> @@ -114,8 +119,14 @@ 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(Time,#conn_log{module=ConnMod}=Info,Data,GL,State0) -> + %% setting to full so output matches with legacy behavior when + %% header field is set to true + State = State0#state{prefix=full}, + write_report_with_header(Info, GL, State, ConnMod, Data, Time). + +write_report_with_header(Info, GL,#state{prefix=PrefixType}=State, + ConnMod, Data, Time) -> case get_log(Info,GL,State) of {silent,_,_} -> ok; @@ -129,13 +140,17 @@ write_report(Time,#conn_log{module=ConnMod}=Info,Data,GL,State) -> true -> "~n~ts~ts~ts" end, - io:format(Fd,Str,[format_head(ConnMod,LogType,Time), - format_title(LogType,Info), - FormattedData]) + io:format(Fd,Str, + [format_head(ConnMod,LogType,PrefixType,Time), + format_title(LogType,PrefixType,Info), + FormattedData]) end end. write_error(Time,#conn_log{module=ConnMod}=Info,Report,GL,State) -> + %% this function was including all prefix data no matter what + %% header field value is - leaving behavior as is it was + PrefixType = full, case get_log(Info,GL,State) of {LogType,_,_} when LogType==html; LogType==silent -> %% The error will anyway be written in the html log by the @@ -145,8 +160,8 @@ write_error(Time,#conn_log{module=ConnMod}=Info,Report,GL,State) -> Str = if LogType == html, Dest == gl -> ["$tc_html","~n~ts~ts~ts"]; true -> "~n~ts~ts~ts" end, - io:format(Fd,Str,[format_head(ConnMod,LogType,Time," ERROR"), - format_title(LogType,Info), + io:format(Fd,Str,[format_head(ConnMod,LogType,PrefixType,Time," ERROR"), + format_title(LogType,PrefixType,Info), format_error(LogType,Report)]) end. @@ -177,18 +192,22 @@ get_fd(#conn_log{name=ConnName},Fds) -> %%%----------------------------------------------------------------- %%% Formatting -format_head(ConnMod,LogType,Time) -> - format_head(ConnMod,LogType,Time,""). +format_head(ConnMod,LogType,PrefixType,Time) -> + format_head(ConnMod,LogType,PrefixType,Time,""). -format_head(ConnMod,raw,Time,Text) -> +format_head(_ConnMod,raw,short,Time,_Text) -> + io_lib:format("~n~s, ",[pretty_head(now_to_time(Time))]); +format_head(ConnMod,raw,_,Time,Text) -> io_lib:format("~n~w, ~w~ts, ",[now_to_time(Time),ConnMod,Text]); -format_head(ConnMod,_,Time,Text) -> +format_head(ConnMod,_,_,Time,Text) -> Head = pad_char_end(?WIDTH,pretty_head(now_to_time(Time),ConnMod,Text),$=), io_lib:format("~n~ts",[Head]). -format_title(raw,#conn_log{client=Client}=Info) -> +format_title(raw,short,_Info) -> + ""; +format_title(raw,full,#conn_log{client=Client}=Info) -> io_lib:format("Client ~tw ~s ~ts",[Client,actionstr(Info),serverstr(Info)]); -format_title(_,Info) -> +format_title(_,_,Info) -> Title = pad_char_end(?WIDTH,pretty_title(Info),$=), io_lib:format("~n~ts", [Title]). @@ -230,6 +249,11 @@ pretty_head({{{Y,Mo,D},{H,Mi,S}},MicroS},ConnMod,Text0) -> [Text,t(D),month(Mo),Y,t(H),t(Mi),t(S), micro2milli(MicroS)]). +pretty_head({{{Y,Mo,D},{H,Mi,S}},MicroS}) -> + io_lib:format("~s-~s-~w::~s:~s:~s,~s ", + [t(D),month(Mo),Y,t(H),t(Mi),t(S), + micro2milli(MicroS)]). + pretty_title(#conn_log{client=Client}=Info) -> io_lib:format("= Client ~tw ~s ~ts ", [Client,actionstr(Info),serverstr(Info)]). diff --git a/lib/common_test/src/ct_telnet.erl b/lib/common_test/src/ct_telnet.erl index 6523e1d4b9dc..c227799b692b 100644 --- a/lib/common_test/src/ct_telnet.erl +++ b/lib/common_test/src/ct_telnet.erl @@ -104,6 +104,12 @@ default value of this option is `raw`, which results in the behavior described above. If the value is set to `html`, all Telnet communication is printed to the test case HTML log instead. +For raw logs, `prefix` option can be used for adjusting prefix data +added to connection log. The default value of this option is +`disabled`, which results with no prefix data. If the value is set to +`full` prefix contains timestamp and additonal information. If the +value is set to `short` prefix includes only human readable timestamp. + All `cth_conn_log` hook options described can also be specified in a configuration file with configuration variable `ct_conn_log`. diff --git a/lib/common_test/src/cth_conn_log.erl b/lib/common_test/src/cth_conn_log.erl index 0ad64995ab19..e681f2bfb9c7 100644 --- a/lib/common_test/src/cth_conn_log.erl +++ b/lib/common_test/src/cth_conn_log.erl @@ -94,12 +94,13 @@ get_log_opts(Mod,Opts) -> end, LogType = proplists:get_value(log_type,Opts,DefaultLogType), Hosts = proplists:get_value(hosts,Opts,[]), - {LogType,Hosts}. + {LogType,Hosts,[{prefix, proplists:get_value(prefix,Opts,disabled)}]}. pre_init_per_testcase(_Suite,TestCase,Config,CthState) -> + {_, _, CtTelnetOpts} = proplists:get_value(ct_telnet, CthState, {null, null, []}), 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 -> @@ -131,11 +132,11 @@ 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,CtTelnetOpts}), [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..237dcee41ba5 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, [{prefix, short}]}]}]} ]. all() ->