From a0cae2fa7b9bbf6d7fbe8575699bad8c41f9e84c Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 9 Apr 2024 11:06:37 +0200 Subject: log_sink_cmdline: support restricted log limit Messages on the command line can be more disturbing than, e.g., in a log file. In particular, for debugging it often is useful to have very verbose logs. In order to have the command-line experience manageable also in this cases, support restricting the command-line logging further. In this way, while interacting with concise command-line messages, verbose logs are still written for later analysis. --- src/buildtool/logging/log_sink_cmdline.hpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/buildtool/logging/log_sink_cmdline.hpp b/src/buildtool/logging/log_sink_cmdline.hpp index 8cbceb1c..89990f1c 100644 --- a/src/buildtool/logging/log_sink_cmdline.hpp +++ b/src/buildtool/logging/log_sink_cmdline.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -29,11 +30,17 @@ class LogSinkCmdLine final : public ILogSink { public: - static auto CreateFactory(bool colored = true) -> LogSinkFactory { - return [=]() { return std::make_shared(colored); }; + static auto CreateFactory(bool colored = true, + std::optional restrict_level = + std::nullopt) -> LogSinkFactory { + return [=]() { + return std::make_shared(colored, restrict_level); + }; } - explicit LogSinkCmdLine(bool colored) noexcept : colored_{colored} {} + explicit LogSinkCmdLine(bool colored, + std::optional restrict_level) noexcept + : colored_{colored}, restrict_level_{restrict_level} {} ~LogSinkCmdLine() noexcept final = default; LogSinkCmdLine(LogSinkCmdLine const&) noexcept = delete; LogSinkCmdLine(LogSinkCmdLine&&) noexcept = delete; @@ -45,6 +52,12 @@ class LogSinkCmdLine final : public ILogSink { LogLevel level, std::string const& msg) const noexcept final { static std::mutex mutex{}; + + if (restrict_level_ and + (static_cast(*restrict_level_) < static_cast(level))) { + return; + } + auto prefix = LogLevelToString(level); if (logger != nullptr) { @@ -78,6 +91,7 @@ class LogSinkCmdLine final : public ILogSink { private: bool colored_{}; + std::optional restrict_level_{}; [[nodiscard]] auto FormatPrefix(LogLevel level, std::string const& prefix) const noexcept -- cgit v1.2.3