From 7ec3c01395304c47a8643610a4cfa7a9aa1f7b31 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 9 Apr 2024 15:11:15 +0200 Subject: Add just-mr command-line option to estrict log limit on stderr --- src/other_tools/just_mr/cli.hpp | 10 ++++++++++ src/other_tools/just_mr/launch.cpp | 6 ++++++ src/other_tools/just_mr/main.cpp | 6 +++++- src/other_tools/just_mr/rc.cpp | 20 +++++++++++++++----- 4 files changed, 36 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/other_tools/just_mr/cli.hpp b/src/other_tools/just_mr/cli.hpp index 04bda36a..d76eee69 100644 --- a/src/other_tools/just_mr/cli.hpp +++ b/src/other_tools/just_mr/cli.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -61,6 +62,7 @@ struct MultiRepoCommonArguments { struct MultiRepoLogArguments { std::vector log_files{}; std::optional log_limit{}; + std::optional restrict_stderr_log_limit{}; bool plain_log{false}; bool log_append{false}; }; @@ -276,6 +278,14 @@ static inline auto SetupMultiRepoLogArguments( static_cast(kLastLogLevel), static_cast(kDefaultLogLevel))) ->type_name("NUM"); + app->add_option_function>( + "--restrict-stderr-log-limit", + [clargs](auto const& limit) { + clargs->restrict_stderr_log_limit = ToLogLevel(limit); + }, + "Restrict logging on console to the minimum of the specified " + "--log-limit and this value") + ->type_name("NUM"); app->add_flag("--plain-log", clargs->plain_log, "Do not use ANSI escape sequences to highlight messages."); diff --git a/src/other_tools/just_mr/launch.cpp b/src/other_tools/just_mr/launch.cpp index 12c9155c..1236d9f4 100644 --- a/src/other_tools/just_mr/launch.cpp +++ b/src/other_tools/just_mr/launch.cpp @@ -126,6 +126,12 @@ auto CallJust(std::optional const& config_file, std::to_string(static_cast::type>( *log_args.log_limit))); } + if (log_args.restrict_stderr_log_limit) { + cmd.emplace_back("--restrict-stderr-log-limit"); + cmd.emplace_back( + std::to_string(static_cast::type>( + *log_args.restrict_stderr_log_limit))); + } if (log_args.plain_log) { cmd.emplace_back("--plain-log"); } diff --git a/src/other_tools/just_mr/main.cpp b/src/other_tools/just_mr/main.cpp index e0504ea7..442d74f6 100644 --- a/src/other_tools/just_mr/main.cpp +++ b/src/other_tools/just_mr/main.cpp @@ -184,7 +184,8 @@ void SetupLogging(MultiRepoLogArguments const& clargs) { else { LogConfig::SetLogLimit(kDefaultLogLevel); } - LogConfig::SetSinks({LogSinkCmdLine::CreateFactory(not clargs.plain_log)}); + LogConfig::SetSinks({LogSinkCmdLine::CreateFactory( + not clargs.plain_log, clargs.restrict_stderr_log_limit)}); for (auto const& log_file : clargs.log_files) { LogConfig::AddSink(LogSinkFile::CreateFactory( log_file, @@ -217,6 +218,9 @@ auto main(int argc, char* argv[]) -> int { SetupLogging(arguments.log); auto config_file = ReadJustMRRC(&arguments); + // As the rc file can contain logging parameters, reset the logging + // configuration + SetupLogging(arguments.log); if (arguments.common.repository_config) { config_file = arguments.common.repository_config; } diff --git a/src/other_tools/just_mr/rc.cpp b/src/other_tools/just_mr/rc.cpp index 90d0f2ed..c5de5ad5 100644 --- a/src/other_tools/just_mr/rc.cpp +++ b/src/other_tools/just_mr/rc.cpp @@ -426,7 +426,21 @@ namespace { std::exit(kExitConfigError); } clargs->log.log_limit = ToLogLevel(limit->Number()); - LogConfig::SetLogLimit(*clargs->log.log_limit); + } + } + // Set restrict stderr log limit, if specified and not set on the command + // line + if (not clargs->log.restrict_stderr_log_limit) { + auto limit = rc_config["restrict stderr log limit"]; + if (limit.IsNotNull()) { + if (not limit->IsNumber()) { + Logger::Log(LogLevel::Error, + "Configuration-file specified log-limit has to be " + "a number, but found {}", + limit->ToString()); + std::exit(kExitConfigError); + } + clargs->log.restrict_stderr_log_limit = ToLogLevel(limit->Number()); } } // Add additional log sinks specified in the rc file. @@ -444,10 +458,6 @@ namespace { ReadLocation(log_file->ToJson(), clargs->common.just_mr_paths->workspace_root); if (path) { - LogConfig::AddSink(LogSinkFile::CreateFactory( - path->first, - clargs->log.log_append ? LogSinkFile::Mode::Append - : LogSinkFile::Mode::Overwrite)); clargs->log.log_files.emplace_back(path->first); } } -- cgit v1.2.3