From 4fc7ef37c21c06f31de4e8db283bacca735da1f803a08fd1757c32af910f4f5c Mon Sep 17 00:00:00 2001 From: CupWater Date: Mon, 17 Aug 2026 21:01:29 +0800 Subject: [PATCH] modify something --- CMakeLists.txt | 5 ++- include/featherlog/featherlog.hpp | 14 +++++++ include/featherlog/log_formatter.hpp | 6 ++- include/featherlog/log_level.hpp | 6 ++- include/featherlog/log_manager.hpp | 8 ++-- include/featherlog/log_message.hpp | 10 +++-- include/featherlog/log_message_flag.hpp | 32 +++++++++++++++ include/featherlog/log_outputter.hpp | 2 +- include/featherlog/logger.hpp | 12 +++--- include/featherlog/push_result.hpp | 22 +++++------ src/log_manager.cpp | 52 +++++++++++++++++-------- src/test.cpp | 11 ++++++ 12 files changed, 133 insertions(+), 47 deletions(-) create mode 100644 include/featherlog/featherlog.hpp create mode 100644 include/featherlog/log_message_flag.hpp create mode 100644 src/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae57bf1..d2a5b4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,9 @@ set(${PROJECT_NAME}_SOURCES include/featherlog/log_message.hpp include/featherlog/flog_definitions.h include/featherlog/push_result.hpp - src/log_manager.cpp - src/log_outputter.cpp) + include/featherlog/log_message_flag.hpp + include/featherlog/featherlog.hpp + src/log_manager.cpp) # 生成静态库 add_library(${PROJECT_NAME}_static STATIC ${${PROJECT_NAME}_SOURCES}) diff --git a/include/featherlog/featherlog.hpp b/include/featherlog/featherlog.hpp new file mode 100644 index 0000000..dc0af5c --- /dev/null +++ b/include/featherlog/featherlog.hpp @@ -0,0 +1,14 @@ +#ifndef FEATHERLOG_FEATHERLOG_HPP +#define FEATHERLOG_FEATHERLOG_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/include/featherlog/log_formatter.hpp b/include/featherlog/log_formatter.hpp index 6063c17..98cfd89 100644 --- a/include/featherlog/log_formatter.hpp +++ b/include/featherlog/log_formatter.hpp @@ -4,8 +4,10 @@ #include #include -namespace flog { - class FLOG_API LogFormatter { +namespace flog +{ + class FLOG_API LogFormatter + { public: virtual ~LogFormatter() = default; diff --git a/include/featherlog/log_level.hpp b/include/featherlog/log_level.hpp index 065162b..473a900 100644 --- a/include/featherlog/log_level.hpp +++ b/include/featherlog/log_level.hpp @@ -3,9 +3,11 @@ #include -namespace flog { +namespace flog +{ using LogLevelType = uint8_t; - enum class LogLevel : LogLevelType { + enum class LogLevel : LogLevelType + { TRACE = 0, DEBUG = 1, INFO = 2, diff --git a/include/featherlog/log_manager.hpp b/include/featherlog/log_manager.hpp index b84df5e..f38dd64 100644 --- a/include/featherlog/log_manager.hpp +++ b/include/featherlog/log_manager.hpp @@ -14,10 +14,10 @@ #include #include -namespace flog { - class FLOG_API LogManager : public std::enable_shared_from_this { - friend class LoggerCallback; - +namespace flog +{ + class FLOG_API LogManager : public std::enable_shared_from_this + { public: LogManager(const LogManager &) = delete; LogManager &operator=(const LogManager &) = delete; diff --git a/include/featherlog/log_message.hpp b/include/featherlog/log_message.hpp index 2959b2e..7286d9f 100644 --- a/include/featherlog/log_message.hpp +++ b/include/featherlog/log_message.hpp @@ -6,13 +6,17 @@ #include #include +#include -namespace flog { - struct LogMessage { +namespace flog +{ + struct LogMessage + { std::chrono::system_clock::time_point timestamp; - LogLevel level; std::string_view logger_name; std::string text; + LogLevel level; + MessageFlag flag; }; } diff --git a/include/featherlog/log_message_flag.hpp b/include/featherlog/log_message_flag.hpp new file mode 100644 index 0000000..4752c50 --- /dev/null +++ b/include/featherlog/log_message_flag.hpp @@ -0,0 +1,32 @@ +#ifndef FEATHERLOG_LOG_MESSAGE_FLAG_HPP +#define FEATHERLOG_LOG_MESSAGE_FLAG_HPP + +#include + +namespace flog +{ + typedef uint32_t MessageFlag; + + class MessageFlagManager + { + public: + MessageFlagManager() noexcept = default; + ~MessageFlagManager() = default; + + MessageFlagManager(const MessageFlagManager&) noexcept = default; + MessageFlagManager& operator=(const MessageFlagManager&) noexcept = default; + MessageFlagManager(MessageFlagManager&&) noexcept = default; + MessageFlagManager& operator=(MessageFlagManager&&) noexcept = default; + + _NODISCARD bool isFull() const noexcept { return flagCount >= std::numeric_limits::digits; } + _NODISCARD bool pushable() const noexcept { return !isFull(); } + _NODISCARD bool isEmpty() const noexcept { return flagCount == 0; } + // _NODISCARD + + private: + std::array::digits> m_flagMap{}; + size_t flagCount { 0 }; + }; +} + +#endif diff --git a/include/featherlog/log_outputter.hpp b/include/featherlog/log_outputter.hpp index e86e1e8..5e92576 100644 --- a/include/featherlog/log_outputter.hpp +++ b/include/featherlog/log_outputter.hpp @@ -23,7 +23,7 @@ namespace flog { virtual void FLOG_CALL write_log(const LogMessage &message) = 0; protected: - std::string m_name; + const std::string m_name; std::unique_ptr m_logFormatter; }; diff --git a/include/featherlog/logger.hpp b/include/featherlog/logger.hpp index 8aa9fea..d2d9a84 100644 --- a/include/featherlog/logger.hpp +++ b/include/featherlog/logger.hpp @@ -7,10 +7,12 @@ #include -namespace flog { - class LoggerManager; +namespace flog +{ + class LogManager; - class FLOG_API Logger { + class FLOG_API Logger final + { friend class LogManager; public: @@ -25,8 +27,8 @@ namespace flog { m_logManager(manager) { } - std::string m_name; - std::weak_ptr m_logManager; + const std::string m_name; + const std::weak_ptr m_logManager; }; } diff --git a/include/featherlog/push_result.hpp b/include/featherlog/push_result.hpp index 7ee4394..e9cfea7 100644 --- a/include/featherlog/push_result.hpp +++ b/include/featherlog/push_result.hpp @@ -4,37 +4,37 @@ #include #include -namespace flog { +namespace flog +{ template - class PushResult { + class PushResult + { public: _NODISCARD static PushResult success() noexcept { return PushResult(); } - _NODISCARD static PushResult failWithValue(T value) noexcept { + _NODISCARD static PushResult failWithValue(T value) noexcept + { return PushResult(std::move(std::optional(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 func) const { + void getValueIfFail(const std::function func) const + { if (isFail()) { func(m_value.value()); } } - _NODISCARD T getValue() const { - return m_value.value(); - } + _NODISCARD T getValue() const { return m_value.value(); } - PushResult(const PushResult &other) noexcept { - m_value = other.m_value; - } + PushResult(const PushResult &other) noexcept { m_value = other.m_value; } private: PushResult() noexcept = default; explicit PushResult(std::optional value) noexcept : m_value(std::move(value)) {} - std::optional m_value = std::nullopt; + const std::optional m_value = std::nullopt; }; } diff --git a/src/log_manager.cpp b/src/log_manager.cpp index f7303d0..eec6fac 100644 --- a/src/log_manager.cpp +++ b/src/log_manager.cpp @@ -2,13 +2,16 @@ #include #include -namespace flog { - std::shared_ptr LogManager::create() { +namespace flog +{ + std::shared_ptr LogManager::create() + { // ReSharper disable once CppDFAMemoryLeak return std::shared_ptr(new LogManager()); } - LogManager::~LogManager() { + LogManager::~LogManager() + { { std::lock_guard lock(m_loggersMutex); m_loggers.clear(); @@ -19,31 +22,37 @@ namespace flog { } } - LogLevel LogManager::get_min_log_level() const noexcept { + LogLevel LogManager::get_min_log_level() const noexcept + { return m_minLogLevel.load(std::memory_order_relaxed); } - void LogManager::set_min_log_level(const LogLevel level) noexcept { + void LogManager::set_min_log_level(const LogLevel level) noexcept + { m_minLogLevel.store(level, std::memory_order_relaxed); } PushResult> LogManager::add_outputter(std::unique_ptr outputter) { std::lock_guard lock(m_logOutputtersMutex); - if (m_logOutputters.contains(outputter->getName())) { + if (m_logOutputters.contains(outputter->getName())) + { return PushResult>::failWithValue(std::move(outputter)); } m_logOutputters.emplace(outputter->getName(), std::move(outputter)); return PushResult>::success(); } - bool LogManager::contains_outputter(const std::string_view name) const noexcept { + 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> LogManager::try_delete_outputter(const std::string_view name) noexcept { - if (contains_outputter(name)) { + std::optional> 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); @@ -53,9 +62,11 @@ namespace flog { return std::nullopt; } - std::shared_ptr LogManager::get_logger(const std::string &name) { + std::shared_ptr LogManager::get_logger(const std::string &name) + { std::lock_guard lock(m_loggersMutex); - if (const auto it = m_loggers.find(name); it != m_loggers.end()) { + if (const auto it = m_loggers.find(name); it != m_loggers.end()) + { return it->second; } @@ -64,23 +75,30 @@ namespace flog { return logger; } - size_t LogManager::clean_unused_loggers() { + size_t LogManager::clean_unused_loggers() + { std::lock_guard lock(m_loggersMutex); size_t count = 0; - for (auto it = m_loggers.begin(); it != m_loggers.end();) { - if (it->second.use_count() == 1) { + for (auto it = m_loggers.begin(); it != m_loggers.end();) + { + if (it->second.use_count() == 1) + { it = m_loggers.erase(it); count += 1; - } else { + } + else + { ++it; } } return count; } - void LogManager::write_log(const LogMessage &message) const { + void LogManager::write_log(const LogMessage &message) const + { std::shared_lock lock(m_logOutputtersMutex); - for (const std::unique_ptr &outputter: m_logOutputters | std::views::values) { + for (const std::unique_ptr &outputter: m_logOutputters | std::views::values) + { outputter->write_log(message); } } diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..1b475de --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,11 @@ +#include + +int main() { + auto manager = flog::LogManager::create(); + // std::unique_ptr formatter =... + manager->add_outputter(/* outputter("name", formatter) */); + auto /*std::shared_ptr*/ logger = manager->get_logger("name"); + logger->info("fmt", ...); + + return 0; +}