SHA256
finish log_manager.cpp
This commit is contained in:
+36
-6
@@ -1,4 +1,6 @@
|
||||
#include <ranges>
|
||||
#include <featherlog/log_manager.hpp>
|
||||
#include <featherlog/push_result.hpp>
|
||||
|
||||
namespace flog {
|
||||
std::shared_ptr<LogManager> LogManager::create() {
|
||||
@@ -6,6 +8,17 @@ namespace flog {
|
||||
return std::shared_ptr<LogManager>(new LogManager());
|
||||
}
|
||||
|
||||
LogManager::~LogManager() {
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
m_loggers.clear();
|
||||
}
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
m_logOutputters.clear();
|
||||
}
|
||||
}
|
||||
|
||||
LogLevel LogManager::get_min_log_level() const noexcept {
|
||||
return m_minLogLevel.load(std::memory_order_relaxed);
|
||||
}
|
||||
@@ -14,13 +27,30 @@ namespace flog {
|
||||
m_minLogLevel.store(level, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
bool LogManager::add_outputter(const std::string& name, std::unique_ptr<LogOutputter> outputter) {
|
||||
PushResult<std::unique_ptr<LogOutputter>> LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter)
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
if (m_logOutputters.contains(name)) {
|
||||
return false;
|
||||
if (m_logOutputters.contains(outputter->getName())) {
|
||||
return PushResult<std::unique_ptr<LogOutputter>>::failWithValue(std::move(outputter));
|
||||
}
|
||||
m_logOutputters[name] = std::move(outputter);
|
||||
return true;
|
||||
m_logOutputters.emplace(outputter->getName(), std::move(outputter));
|
||||
return PushResult<std::unique_ptr<LogOutputter>>::success();
|
||||
}
|
||||
|
||||
bool LogManager::contains_outputter(const std::string_view name) const noexcept {
|
||||
std::shared_lock lock(m_logOutputtersMutex);
|
||||
return m_logOutputters.contains(name.data());
|
||||
}
|
||||
|
||||
std::optional<std::unique_ptr<LogOutputter>> LogManager::try_delete_outputter(const std::string_view name) noexcept {
|
||||
if (contains_outputter(name)) {
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
const auto it = m_logOutputters.find(name.data());
|
||||
std::optional outputter = std::move(it->second);
|
||||
m_logOutputters.erase(it);
|
||||
return std::move(outputter);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::shared_ptr<Logger> LogManager::get_logger(const std::string &name) {
|
||||
@@ -50,7 +80,7 @@ namespace flog {
|
||||
|
||||
void LogManager::print_log(const LogMessage &message) const {
|
||||
std::shared_lock lock(m_logOutputtersMutex);
|
||||
for (auto &outputter: m_logOutputters) {
|
||||
for (const std::unique_ptr<LogOutputter> &outputter: m_logOutputters | std::views::values) {
|
||||
outputter->write(message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user