Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:23
erlang
7881-ct-Introduce-replace-mode-to-cth_log_redir...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 7881-ct-Introduce-replace-mode-to-cth_log_redirect.patch of Package erlang
From 7e6dc244a27b6623b26093d6a9e7233ec86487b0 Mon Sep 17 00:00:00 2001 From: Johannes Christ <jc@jchri.st> Date: Mon, 20 Nov 2023 16:09:32 +0100 Subject: [PATCH] ct: Introduce replace mode to cth_log_redirect In addition to the existing way of logging to both the CT HTML logs and console, allow users to completely replace the standard logging handler with the log redirect hook, effectively silencing console logging output during Common Test runs. Mostly taken from #7375, with the caveat that "add" mode continues being the default, and no changes outside of the hook take place. Additionally, group cth_log_redirect test cases together to allow for easier module verification. --- lib/common_test/doc/src/ct_hooks_chapter.xml | 19 +++++ lib/common_test/src/cth_log_redirect.erl | 21 ++++-- lib/common_test/test/ct_hooks_SUITE.erl | 78 ++++++++++++-------- 3 files changed, 80 insertions(+), 38 deletions(-) diff --git a/lib/common_test/doc/src/ct_hooks_chapter.xml b/lib/common_test/doc/src/ct_hooks_chapter.xml index ca8c0d2e0e..6253505693 100644 --- a/lib/common_test/doc/src/ct_hooks_chapter.xml +++ b/lib/common_test/doc/src/ct_hooks_chapter.xml @@ -511,6 +511,25 @@ results(State) -> use another level either change the <c>default</c> handler level before starting common_test, or use the <seemfa marker="kernel:logger#set_handler_config/3"> <c>logger:set_handler_config/3</c></seemfa> API.</p> + <p>This hook supports the following options:</p> + <taglist> + <tag><c>{mode, add}</c></tag> + <item> + <p>Add <c>cth_log_redirect</c> to the default logging handler: + Logs will be emitted to both standard output via the + default handler, and into the Common Test HTML logs. + This is the default behaviour.</p> + </item> + <tag><c>{mode, replace}</c></tag> + <item> + <p>Replace the <c>default</c> logging handler with <c>cth_log_redirect</c> + instead of logging to both the default handler and this handler. + This effectively silences any logger output which would normally be printed + to standard output during test runs. To enable this mode, you can pass + the following options to <c>ct_run</c>:</p> + <p><c>-enable_builtin_hooks false -ct_hooks cth_log_redirect [{mode,replace}]</c></p> + </item> + </taglist> </item> <tag><c>cth_surefire</c></tag> <item> diff --git a/lib/common_test/src/cth_log_redirect.erl b/lib/common_test/src/cth_log_redirect.erl index f51cfd4bc6..b9a8536acf 100644 --- a/lib/common_test/src/cth_log_redirect.erl +++ b/lib/common_test/src/cth_log_redirect.erl @@ -21,7 +21,8 @@ %%% Common Test Framework functions handling test specifications. %%% -%%% This module redirects sasl and error logger info to common test log. +%%% This module redirects sasl, error logger, and standard logger messages to +%%% the common test log. %% CTH Callbacks -export([id/1, init/2, @@ -56,9 +57,9 @@ id(_Opts) -> ?MODULE. -init(?MODULE, _Opts) -> +init(?MODULE, Opts) -> ct_util:mark_process(), - ok = start_log_handler(), + ok = start_log_handler(Opts), {ok, tc_log_async}. pre_init_per_suite(Suite, Config, State) -> @@ -115,7 +116,7 @@ post_end_per_group(_Suite, _Group, Config, Return, State) -> set_curr_func({group,undefined}, Config), {Return, State}. -start_log_handler() -> +start_log_handler(Options) -> case whereis(?MODULE) of undefined -> ChildSpec = @@ -137,9 +138,15 @@ start_log_handler() -> _Else -> {{?DEFAULT_FORMATTER,?DEFAULT_FORMAT_CONFIG},info} end, - ok = logger:add_handler(?MODULE,?MODULE, - #{level=>DefaultLevel, - formatter=>DefaultFormatter}). + HandlerConfig = #{level => DefaultLevel, formatter => DefaultFormatter}, + HandlerName = case proplists:get_value(mode, Options, add) of + add -> + ?MODULE; + replace -> + ok = logger:remove_handler(default), + default + end, + ok = logger:add_handler(HandlerName, ?MODULE, HandlerConfig). init([]) -> {ok, #eh_state{log_func = tc_log_async}}. diff --git a/lib/common_test/test/ct_hooks_SUITE.erl b/lib/common_test/test/ct_hooks_SUITE.erl index a59bc3bd5a..a1752c1786 100644 --- a/lib/common_test/test/ct_hooks_SUITE.erl +++ b/lib/common_test/test/ct_hooks_SUITE.erl @@ -112,10 +112,16 @@ all(suite) -> no_init_suite_config, no_init_config, no_end_config, failed_sequence, repeat_force_stop, config_clash, callbacks_on_skip, fallback, data_dir, - cth_log, cth_log_formatter, cth_log_unexpect + {group, cth_log_redirect} ] ). +groups() -> + [ + {cth_log_redirect, [], [cth_log_unexpect, cth_log_formatter, + cth_log, cth_log_mode_replace]} + ]. + %%-------------------------------------------------------------------- %% TEST CASES @@ -300,36 +306,7 @@ data_dir(Config) when is_list(Config) -> cth_log(Config) when is_list(Config) -> %% test that cth_log_redirect writes properly to %% html I/O log - ct:timetrap({minutes,10}), - StartOpts = do_test(cth_log, "cth_log_SUITE.erl", [], Config), - Logdir = proplists:get_value(logdir, StartOpts), - TCLogs = - filelib:wildcard( - filename:join(Logdir, - "ct_run*/cth.tests*/run*/cth_log_suite.tc*.html")), - lists:foreach( - fun(TCLog) -> - {ok,Bin} = file:read_file(TCLog), - Ts = string:lexemes(binary_to_list(Bin),[$\n]), - Matches = lists:foldl(fun("=ERROR"++_, {E,I,N,L}) -> - {E+1,I,N,L}; - ("=INFO"++_, {E,I,N,L}) -> - {E,I+1,N,L}; - ("=NOTICE"++_, {E,I,N,L}) -> - {E,I,N+1,L}; - ("Logger"++_, {E,I,N,L}) -> - {E,I,N,L+1}; - (_, N) -> N - end, {0,0,0,0}, Ts), - ct:pal("~p ({Error,Info,Notice,Log}) matches in ~tp", - [Matches,TCLog]), - MatchList = tuple_to_list(Matches), - case [N || N <- MatchList, N<1] of - [] -> ok; - _ -> exit({missing_io,TCLog}) - end - end, TCLogs), - ok. + verify_cth_log_output(Config, [], []). cth_log_formatter(Config) when is_list(Config) -> %% test that cth_log_redirect writes properly to @@ -398,6 +375,12 @@ cth_log_unexpect(Config) when is_list(Config) -> end, UnexpIoLogs), ok. +cth_log_mode_replace(Config) when is_list(Config) -> + %% test that cth_log_redirect writes properly to + %% html I/O log when replace mode is used + verify_cth_log_output(Config, [{cth_log_redirect, [{mode, replace}]}], + [{enable_builtin_hooks, false}]). + %% OTP-10599 adds the Suite argument as first argument to all hook %% callbacks that did not have a Suite argument from before. This test %% checks that ct_hooks will fall back to old versions of callbacks if @@ -542,6 +525,39 @@ gen_config(Name,KeyVals,Config) -> || {Key,Value} <- KeyVals]), File. +verify_cth_log_output(Config, CTHooks, ExtraOpts) -> + ct:timetrap({minutes,10}), + StartOpts = do_test(cth_log, "cth_log_SUITE.erl", CTHooks, Config, ok, 2, ExtraOpts), + Logdir = proplists:get_value(logdir, StartOpts), + TCLogs = + filelib:wildcard( + filename:join(Logdir, + "ct_run*/cth.tests*/run*/cth_log_suite.tc*.html")), + lists:foreach( + fun(TCLog) -> + {ok,Bin} = file:read_file(TCLog), + Ts = string:lexemes(binary_to_list(Bin),[$\n]), + Matches = lists:foldl(fun("=ERROR"++_, {E,I,N,L}) -> + {E+1,I,N,L}; + ("=INFO"++_, {E,I,N,L}) -> + {E,I+1,N,L}; + ("=NOTICE"++_, {E,I,N,L}) -> + {E,I,N+1,L}; + ("Logger"++_, {E,I,N,L}) -> + {E,I,N,L+1}; + (_, N) -> N + end, {0,0,0,0}, Ts), + ct:pal("~p ({Error,Info,Notice,Log}) matches in ~tp", + [Matches,TCLog]), + MatchList = tuple_to_list(Matches), + case [N || N <- MatchList, N<1] of + [] -> ok; + _ -> exit({missing_io,TCLog}) + end + end, TCLogs), + ok. + + %%%----------------------------------------------------------------- %%% TEST EVENTS %%%----------------------------------------------------------------- -- 2.35.3
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor