diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-09 11:06:37 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-10 16:18:07 +0200 |
commit | a0cae2fa7b9bbf6d7fbe8575699bad8c41f9e84c (patch) | |
tree | eb6a1a7ca6fe9de581683322a2bf22e2af70e296 | |
parent | fa5e06c28a7e09a71bc706d0311d6a6707b00c0c (diff) | |
download | justbuild-a0cae2fa7b9bbf6d7fbe8575699bad8c41f9e84c.tar.gz |
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.
-rw-r--r-- | src/buildtool/logging/log_sink_cmdline.hpp | 20 |
1 files changed, 17 insertions, 3 deletions
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 <iterator> #include <memory> #include <mutex> +#include <optional> #include <sstream> #include <string> @@ -29,11 +30,17 @@ class LogSinkCmdLine final : public ILogSink { public: - static auto CreateFactory(bool colored = true) -> LogSinkFactory { - return [=]() { return std::make_shared<LogSinkCmdLine>(colored); }; + static auto CreateFactory(bool colored = true, + std::optional<LogLevel> restrict_level = + std::nullopt) -> LogSinkFactory { + return [=]() { + return std::make_shared<LogSinkCmdLine>(colored, restrict_level); + }; } - explicit LogSinkCmdLine(bool colored) noexcept : colored_{colored} {} + explicit LogSinkCmdLine(bool colored, + std::optional<LogLevel> 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<int>(*restrict_level_) < static_cast<int>(level))) { + return; + } + auto prefix = LogLevelToString(level); if (logger != nullptr) { @@ -78,6 +91,7 @@ class LogSinkCmdLine final : public ILogSink { private: bool colored_{}; + std::optional<LogLevel> restrict_level_{}; [[nodiscard]] auto FormatPrefix(LogLevel level, std::string const& prefix) const noexcept |