Skip to content

Commit ebf0786

Browse files
committed
log_level_from_value function
1 parent c5bd48b commit ebf0786

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/infra/log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,6 @@ struct LogRegistration
290290
Log::uninitialize();
291291
}
292292
};
293+
294+
Log::Level log_level_from_value(int32_t value);
293295
}

log.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "infra/log.h"
2+
#include "infra/exception.h"
23
#include "infra/string.h"
34

45
#include <spdlog/async.h>
@@ -95,7 +96,6 @@ void Log::initialize_async(const std::string& name)
9596
_log = std::make_shared<spdlog::async_logger>(name, begin(_sinks), end(_sinks), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
9697
_log->set_pattern("%^[%L] %v%$");
9798
_log->set_level(spdlog::level::warn);
98-
9999
}
100100

101101
void Log::uninitialize()
@@ -143,4 +143,22 @@ void Log::set_pattern(std::string_view pattern)
143143
{
144144
_log->set_pattern(fmt::format("%^{}%$", pattern));
145145
}
146+
147+
Log::Level log_level_from_value(int32_t value)
148+
{
149+
switch (value) {
150+
case 1:
151+
return Log::Level::Debug;
152+
case 2:
153+
return Log::Level::Info;
154+
case 3:
155+
return Log::Level::Warning;
156+
case 4:
157+
return Log::Level::Error;
158+
case 5:
159+
return Log::Level::Critical;
160+
default:
161+
throw RuntimeError("Invalid log level specified '{}': value must be in range [1-5]", value);
162+
}
163+
}
146164
}

test/workerthreadtest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <doctest/doctest.h>
44
#include <set>
55
#include <thread>
6+
#include <stdexcept>
67

78
namespace inf::test {
89

0 commit comments

Comments
 (0)