File 1111-common_test-header-option-for-cth_conn_log-hook-modu.patch of Package erlang

From a25c0b34fdfa4c8f07599c8651cd73f146ce4f14 Mon Sep 17 00:00:00 2001
From: Jakub Witczak <kuba@erlang.org>
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         | 58 +++++++++++++------
 lib/common_test/src/ct_telnet.erl             |  6 ++
 lib/common_test/src/cth_conn_log.erl          |  9 +--
 .../ct_telnet_own_server_SUITE.erl            |  5 +-
 4 files changed, 54 insertions(+), 24 deletions(-)

diff --git a/lib/common_test/src/ct_conn_log_h.erl b/lib/common_test/src/ct_conn_log_h.erl
index 216a8b7615..3959baedc5 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/doc/src/ct_telnet.xml b/lib/common_test/doc/src/ct_telnet.xml
index 6523e1d4b9..c227799b69 100644
--- a/lib/common_test/doc/src/ct_telnet.xml
+++ b/lib/common_test/doc/src/ct_telnet.xml
@@ -135,6 +135,12 @@
       is set to <c>html</c>, all Telnet communication is printed to the test
       case HTML log instead.</p>
 
+    <p>For raw logs, <c>prefix</c> option can be used for adjusting prefix data
+      added to connection log. The default value of this option is
+      <c>disabled</c>, which results with no prefix data. If the value is set to
+      <c>full</c> prefix contains timestamp and additonal information. If the
+      value is set to <c>short</c> prefix includes only human readable timestamp.</p>
+
     <p>All <c>cth_conn_log</c> hook options described can also be
       specified in a configuration file with configuration variable
       <c>ct_conn_log</c>.</p>
diff --git a/lib/common_test/src/cth_conn_log.erl b/lib/common_test/src/cth_conn_log.erl
index 0ad64995ab..e681f2bfb9 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 34df57027e..237dcee41b 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() ->
-- 
2.43.0

openSUSE Build Service is sponsored by