From 224e813fdc25f1932069f5e738447193adab5e63 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 21 Mar 2023 14:30:18 +0100 Subject: LogLevel: support conversion from floating-point numbers ... so that, e.g., we can set the logging from an expression value. --- src/buildtool/logging/log_level.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/buildtool/logging/log_level.hpp b/src/buildtool/logging/log_level.hpp index 55b66790..1902e960 100644 --- a/src/buildtool/logging/log_level.hpp +++ b/src/buildtool/logging/log_level.hpp @@ -16,6 +16,7 @@ #define INCLUDED_SRC_BUILDTOOL_LOGGING_LOG_LEVEL_HPP #include +#include #include #include @@ -40,6 +41,23 @@ constexpr auto kLastLogLevel = LogLevel::Trace; kLastLogLevel); } +[[nodiscard]] static inline auto ToLogLevel(double level) -> LogLevel { + if (level < static_cast(kFirstLogLevel)) { + return kFirstLogLevel; + } + if (level > static_cast(kLastLogLevel)) { + return kLastLogLevel; + } + // Now we're in range, so we can round and cast. + try { + return ToLogLevel( + static_cast>(std::lround(level))); + } catch (...) { + // should not happen, but chose the most conservative value + return kLastLogLevel; + } +} + [[nodiscard]] static inline auto LogLevelToString(LogLevel level) -> std::string { switch (level) { -- cgit v1.2.3