From d65184f67819a93313151bc4a4ada4766409b683d0838c050106f071ae1e9c93 Mon Sep 17 00:00:00 2001 From: CupWater Date: Tue, 18 Aug 2026 21:02:02 +0800 Subject: [PATCH] modify something --- include/featherlog/flog_definitions.h | 8 ++++-- include/featherlog/log_formatter.hpp | 2 +- include/featherlog/log_manager.hpp | 24 ++++++++++------ include/featherlog/log_message_flag.hpp | 37 +++++++++++++++---------- include/featherlog/log_outputter.hpp | 12 ++++---- include/featherlog/logger.hpp | 5 ++++ include/featherlog/push_result.hpp | 10 +++---- src/log_manager.cpp | 7 ++--- src/log_message_flag.cpp | 1 + 9 files changed, 66 insertions(+), 40 deletions(-) diff --git a/include/featherlog/flog_definitions.h b/include/featherlog/flog_definitions.h index f2617ee..5375e5f 100644 --- a/include/featherlog/flog_definitions.h +++ b/include/featherlog/flog_definitions.h @@ -27,8 +27,12 @@ #endif -#ifndef _NODISCARD -#define _NODISCARD [[nodiscard]] +#ifndef NODISCARD +# ifdef _NODISCARD +# define NODISCARD _NODISCARD +# else +# define NODISCARD [[nodiscard]] +# endif #endif #endif diff --git a/include/featherlog/log_formatter.hpp b/include/featherlog/log_formatter.hpp index e2be7b2..8340128 100644 --- a/include/featherlog/log_formatter.hpp +++ b/include/featherlog/log_formatter.hpp @@ -10,6 +10,6 @@ namespace flog public: virtual ~LogFormatter() = default; - _NODISCARD virtual std::string FLOG_CALL format(const LogMessage& message) = 0; + NODISCARD virtual std::string FLOG_CALL format(const LogMessage& message) = 0; }; } diff --git a/include/featherlog/log_manager.hpp b/include/featherlog/log_manager.hpp index 31a14a3..96bb23d 100644 --- a/include/featherlog/log_manager.hpp +++ b/include/featherlog/log_manager.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace flog { @@ -23,26 +24,30 @@ namespace flog LogManager(LogManager &&) = delete; LogManager &operator=(LogManager &&) = delete; - _NODISCARD static std::shared_ptr FLOG_CALL create(); + NODISCARD static std::shared_ptr FLOG_CALL create() noexcept { return {}; } + NODISCARD static std::shared_ptr FLOG_CALL create(LogMessageFlagManager flagManager); ~LogManager(); - _NODISCARD LogLevel FLOG_CALL get_min_log_level() const noexcept; - void FLOG_CALL set_min_log_level(LogLevel level) noexcept; + NODISCARD LogLevel FLOG_CALL get_min_log_level() const noexcept; + void FLOG_CALL set_min_log_level(LogLevel level) noexcept; - _NODISCARD PushResult> FLOG_CALL add_outputter( - std::unique_ptr outputter); - - _NODISCARD bool FLOG_CALL contains_outputter(std::string_view name) const; + NODISCARD PushResult> FLOG_CALL + add_outputter(std::unique_ptr outputter); + NODISCARD bool FLOG_CALL contains_outputter(std::string_view name) const; std::optional> FLOG_CALL try_delete_outputter(std::string_view name); - _NODISCARD std::shared_ptr FLOG_CALL get_logger(const std::string &name); + NODISCARD std::shared_ptr FLOG_CALL get_logger(const std::string &name); size_t FLOG_CALL clean_unused_loggers(); private: LogManager() noexcept = default; + explicit LogManager(LogMessageFlagManager flagManager) noexcept : + m_logMessageFlagManager(std::move(flagManager)) + { } + void FLOG_CALL write_log(const LogMessage &message) const; std::atomic m_minLogLevel{LogLevel::DEBUG}; @@ -52,5 +57,8 @@ namespace flog mutable std::mutex m_loggersMutex; std::unordered_map> m_loggers; + + mutable std::shared_mutex m_logMessageFlagManagerMutex; + LogMessageFlagManager m_logMessageFlagManager; }; } diff --git a/include/featherlog/log_message_flag.hpp b/include/featherlog/log_message_flag.hpp index 66eb1c2..533d66c 100644 --- a/include/featherlog/log_message_flag.hpp +++ b/include/featherlog/log_message_flag.hpp @@ -5,27 +5,36 @@ namespace flog { - typedef uint32_t MessageFlag; + typedef uint32_t LogMessageFlag; + typedef LogMessageFlag MessageFlag; - class MessageFlagManager + typedef class LogMessageFlagManager { public: - MessageFlagManager() noexcept = default; - ~MessageFlagManager() = default; + static constexpr MessageFlag ADD_FAILED = 0; - MessageFlagManager(const MessageFlagManager&) noexcept = default; - MessageFlagManager& operator=(const MessageFlagManager&) noexcept = default; - MessageFlagManager(MessageFlagManager&&) noexcept = default; - MessageFlagManager& operator=(MessageFlagManager&&) noexcept = default; + static constexpr size_t FLAG_ARRAY_SIZE = std::numeric_limits::digits; - _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; } + LogMessageFlagManager() noexcept = default; + ~LogMessageFlagManager() = default; - _NODISCARD bool contains(std::string_view name) const noexcept; + LogMessageFlagManager(const LogMessageFlagManager&) noexcept = default; + LogMessageFlagManager& operator=(const LogMessageFlagManager&) noexcept = default; + LogMessageFlagManager(LogMessageFlagManager&&) noexcept = default; + LogMessageFlagManager& operator=(LogMessageFlagManager&&) noexcept = default; + + NODISCARD bool isFull() const noexcept { return flagCount >= FLAG_ARRAY_SIZE; } + NODISCARD bool pushable() const noexcept { return !isFull(); } + NODISCARD bool isEmpty() const noexcept { return flagCount == 0; } + NODISCARD bool empty() const noexcept { return isEmpty(); } + + NODISCARD bool contains(std::string_view name) const noexcept; + + NODISCARD MessageFlag addFlag (std::string_view name); + NODISCARD MessageFlag findFlag(std::string_view name) const; private: - std::array::digits> m_flagMap{}; + std::array m_flagMap{}; size_t flagCount { 0 }; - }; + } MessageFlagManager; } diff --git a/include/featherlog/log_outputter.hpp b/include/featherlog/log_outputter.hpp index 724b10d..2188c5e 100644 --- a/include/featherlog/log_outputter.hpp +++ b/include/featherlog/log_outputter.hpp @@ -6,19 +6,21 @@ #include #include -namespace flog { - class FLOG_API LogOutputter { +namespace flog +{ + class FLOG_API LogOutputter + { public: - explicit LogOutputter(std::string name, std::unique_ptr formatter) noexcept + LogOutputter(std::string name, std::unique_ptr formatter) noexcept : m_name(std::move(name)), m_logFormatter(std::move(formatter)) { } virtual ~LogOutputter() = default; - _NODISCARD std::string FLOG_CALL getName() const noexcept { return m_name; } + NODISCARD std::string FLOG_CALL getName() const noexcept { return m_name; } std::unique_ptr FLOG_CALL set_formatter(const std::unique_ptr &formatter); - _NODISCARD const std::unique_ptr & FLOG_CALL get_formatter() const; + NODISCARD const std::unique_ptr & FLOG_CALL get_formatter() const; virtual void FLOG_CALL write_log(const LogMessage &message) = 0; diff --git a/include/featherlog/logger.hpp b/include/featherlog/logger.hpp index 5b4ae31..9ceec32 100644 --- a/include/featherlog/logger.hpp +++ b/include/featherlog/logger.hpp @@ -15,6 +15,11 @@ namespace flog friend class LogManager; public: + Logger(const Logger&) = delete; + Logger operator=(const Logger&) = delete; + Logger(const Logger&&) = delete; + Logger operator=(const Logger&&) = delete; + ~Logger() = default; template diff --git a/include/featherlog/push_result.hpp b/include/featherlog/push_result.hpp index e77bbb0..f85630f 100644 --- a/include/featherlog/push_result.hpp +++ b/include/featherlog/push_result.hpp @@ -9,14 +9,14 @@ namespace flog class PushResult { public: - _NODISCARD static PushResult success() noexcept { return PushResult(); } - _NODISCARD static PushResult failWithValue(T value) noexcept + NODISCARD static PushResult success() noexcept { return PushResult(); } + 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(); } + 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 { @@ -25,7 +25,7 @@ namespace flog } } - _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; } diff --git a/src/log_manager.cpp b/src/log_manager.cpp index c07af34..6e6cde6 100644 --- a/src/log_manager.cpp +++ b/src/log_manager.cpp @@ -1,13 +1,10 @@ -#include #include #include namespace flog { - std::shared_ptr LogManager::create() - { - // ReSharper disable once CppDFAMemoryLeak - return std::shared_ptr(new LogManager()); + std::shared_ptr LogManager::create(MessageFlagManager flagManager) { + return std::shared_ptr(new LogManager(std::move(flagManager))); } LogManager::~LogManager() diff --git a/src/log_message_flag.cpp b/src/log_message_flag.cpp index 473a0f4..ccc2f5a 100644 --- a/src/log_message_flag.cpp +++ b/src/log_message_flag.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file