summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/logging/log_level.hpp18
1 files changed, 18 insertions, 0 deletions
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 <algorithm>
+#include <cmath>
#include <string>
#include <type_traits>
@@ -40,6 +41,23 @@ constexpr auto kLastLogLevel = LogLevel::Trace;
kLastLogLevel);
}
+[[nodiscard]] static inline auto ToLogLevel(double level) -> LogLevel {
+ if (level < static_cast<double>(kFirstLogLevel)) {
+ return kFirstLogLevel;
+ }
+ if (level > static_cast<double>(kLastLogLevel)) {
+ return kLastLogLevel;
+ }
+ // Now we're in range, so we can round and cast.
+ try {
+ return ToLogLevel(
+ static_cast<std::underlying_type_t<LogLevel>>(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) {