summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2023-03-21 14:30:18 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2023-03-21 16:27:25 +0100
commit224e813fdc25f1932069f5e738447193adab5e63 (patch)
tree04bc471470eb2f591f027b447511ccde06915837 /src
parent53ef14352f9e1101972ca6fa22b19074321317fa (diff)
downloadjustbuild-224e813fdc25f1932069f5e738447193adab5e63.tar.gz
LogLevel: support conversion from floating-point numbers
... so that, e.g., we can set the logging from an expression value.
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) {