SHA256
finish log_manager.cpp
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
#include <featherlog/log_message.hpp>
|
#include <featherlog/log_message.hpp>
|
||||||
#include <featherlog/log_outputter.hpp>
|
#include <featherlog/log_outputter.hpp>
|
||||||
#include <featherlog/flog_definitions.h>
|
#include <featherlog/flog_definitions.h>
|
||||||
|
#include <featherlog/push_result.hpp>
|
||||||
|
|
||||||
namespace flog {
|
namespace flog {
|
||||||
class FLOG_API LogManager : public std::enable_shared_from_this<LogManager> {
|
class FLOG_API LogManager : public std::enable_shared_from_this<LogManager> {
|
||||||
@@ -25,13 +26,14 @@ namespace flog {
|
|||||||
|
|
||||||
_NODISCARD static std::shared_ptr<LogManager> FLOG_CALL create();
|
_NODISCARD static std::shared_ptr<LogManager> FLOG_CALL create();
|
||||||
|
|
||||||
~LogManager() = default;
|
~LogManager();
|
||||||
|
|
||||||
LogLevel FLOG_CALL get_min_log_level() const noexcept;
|
LogLevel FLOG_CALL get_min_log_level() const noexcept;
|
||||||
void FLOG_CALL set_min_log_level(LogLevel level) noexcept;
|
void FLOG_CALL set_min_log_level(LogLevel level) noexcept;
|
||||||
|
|
||||||
PushResult FLOG_CALL add_outputter(const std::string& name, std::unique_ptr<LogOutputter> outputter);
|
PushResult<std::unique_ptr<LogOutputter>> FLOG_CALL add_outputter(std::unique_ptr<LogOutputter> outputter);
|
||||||
std::optional<LogOutputter> FLOG_CALL get_outputter() noexcept;
|
bool FLOG_CALL contains_outputter(std::string_view name) const noexcept;
|
||||||
|
std::optional<std::unique_ptr<LogOutputter>> try_delete_outputter(std::string_view name) noexcept;
|
||||||
|
|
||||||
std::shared_ptr<Logger> FLOG_CALL get_logger(const std::string &name);
|
std::shared_ptr<Logger> FLOG_CALL get_logger(const std::string &name);
|
||||||
|
|
||||||
|
|||||||
@@ -10,14 +10,20 @@
|
|||||||
namespace flog {
|
namespace flog {
|
||||||
class FLOG_API LogOutputter {
|
class FLOG_API LogOutputter {
|
||||||
public:
|
public:
|
||||||
|
explicit LogOutputter(std::string name) noexcept : name(std::move(name)) {}
|
||||||
|
|
||||||
virtual ~LogOutputter() = default;
|
virtual ~LogOutputter() = default;
|
||||||
|
|
||||||
|
std::string& getName() const noexcept;
|
||||||
|
|
||||||
std::unique_ptr<LogFormatter> FLOG_CALL set_formatter(const std::unique_ptr<LogFormatter> &formatter);
|
std::unique_ptr<LogFormatter> FLOG_CALL set_formatter(const std::unique_ptr<LogFormatter> &formatter);
|
||||||
[[nodiscard]] const std::unique_ptr<LogFormatter> & FLOG_CALL get_formatter() const;
|
[[nodiscard]] const std::unique_ptr<LogFormatter> & FLOG_CALL get_formatter() const;
|
||||||
|
|
||||||
virtual void FLOG_CALL write(const LogMessage &message) = 0;
|
virtual void FLOG_CALL write(const LogMessage &message) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
std::string name;
|
||||||
|
|
||||||
std::unique_ptr<LogFormatter> m_logFormatter;
|
std::unique_ptr<LogFormatter> m_logFormatter;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,39 @@
|
|||||||
#define FEATHERLOG_PUSH_RESULT_HPP
|
#define FEATHERLOG_PUSH_RESULT_HPP
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace flog {
|
namespace flog {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class PushResult {
|
class PushResult {
|
||||||
public:
|
public:
|
||||||
static PushResult getVoid() {
|
_NODISCARD static PushResult success() noexcept { return PushResult(); }
|
||||||
return PushResult(3);
|
_NODISCARD static PushResult failWithValue(T value) noexcept {
|
||||||
|
return PushResult(std::move(std::optional<T>(std::move(value))));
|
||||||
|
}
|
||||||
|
|
||||||
|
_NODISCARD bool isSuccessful() const noexcept { return !m_value.has_value(); }
|
||||||
|
_NODISCARD bool isFail() const noexcept { return m_value.has_value(); }
|
||||||
|
|
||||||
|
void getValueIfFail(const std::function<void(const T& value)> func) const {
|
||||||
|
if (isFail()) {
|
||||||
|
func(m_value.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_NODISCARD T getValue() const {
|
||||||
|
return m_value.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
PushResult(const PushResult &other) noexcept {
|
||||||
|
m_value = other.m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit PushResult(std::optional<T> value) : m_value(value) {
|
PushResult() noexcept = default;
|
||||||
std::optional<T> bad;
|
explicit PushResult(std::optional<T> value) noexcept : m_value(std::move(value)) {}
|
||||||
std::map<int, std::optional<T>> mp;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<T> m_value;
|
std::optional<T> m_value = std::nullopt;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+36
-6
@@ -1,4 +1,6 @@
|
|||||||
|
#include <ranges>
|
||||||
#include <featherlog/log_manager.hpp>
|
#include <featherlog/log_manager.hpp>
|
||||||
|
#include <featherlog/push_result.hpp>
|
||||||
|
|
||||||
namespace flog {
|
namespace flog {
|
||||||
std::shared_ptr<LogManager> LogManager::create() {
|
std::shared_ptr<LogManager> LogManager::create() {
|
||||||
@@ -6,6 +8,17 @@ namespace flog {
|
|||||||
return std::shared_ptr<LogManager>(new LogManager());
|
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 {
|
LogLevel LogManager::get_min_log_level() const noexcept {
|
||||||
return m_minLogLevel.load(std::memory_order_relaxed);
|
return m_minLogLevel.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
@@ -14,13 +27,30 @@ namespace flog {
|
|||||||
m_minLogLevel.store(level, std::memory_order_relaxed);
|
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);
|
std::lock_guard lock(m_logOutputtersMutex);
|
||||||
if (m_logOutputters.contains(name)) {
|
if (m_logOutputters.contains(outputter->getName())) {
|
||||||
return false;
|
return PushResult<std::unique_ptr<LogOutputter>>::failWithValue(std::move(outputter));
|
||||||
}
|
}
|
||||||
m_logOutputters[name] = std::move(outputter);
|
m_logOutputters.emplace(outputter->getName(), std::move(outputter));
|
||||||
return true;
|
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) {
|
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 {
|
void LogManager::print_log(const LogMessage &message) const {
|
||||||
std::shared_lock lock(m_logOutputtersMutex);
|
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);
|
outputter->write(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user