diff --git a/CMakeLists.txt b/CMakeLists.txt index c5f8d2a..68688cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ set(${PROJECT_NAME}_SOURCES include/featherlog/log_formatter.hpp include/featherlog/log_level.hpp include/featherlog/log_message.hpp + include/featherlog/flog_definitions.h + include/featherlog/push_result.hpp src/log_manager.cpp) # 生成静态库 @@ -29,8 +31,7 @@ add_compile_definitions(FLOG_DLL_MODE) add_compile_definitions(FLOG_BUILDING_DLL) # 生成动态库 -add_library(${PROJECT_NAME}_shared SHARED ${${PROJECT_NAME}_SOURCES} - include/featherlog/flog_definitions.h) +add_library(${PROJECT_NAME}_shared SHARED ${${PROJECT_NAME}_SOURCES}) target_include_directories(${PROJECT_NAME}_shared PUBLIC include) set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME ${PROJECT_NAME} diff --git a/include/featherlog/log_manager.hpp b/include/featherlog/log_manager.hpp index a55898c..7014c1c 100644 --- a/include/featherlog/log_manager.hpp +++ b/include/featherlog/log_manager.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -24,14 +23,15 @@ namespace flog { LogManager(LogManager &&) = delete; LogManager &operator=(LogManager &&) = delete; - static std::shared_ptr FLOG_CALL create(); + _NODISCARD static std::shared_ptr FLOG_CALL create(); - ~LogManager(); + ~LogManager() = default; LogLevel FLOG_CALL get_min_log_level() const noexcept; void FLOG_CALL set_min_log_level(LogLevel level) noexcept; - void FLOG_CALL add_outputter(std::unique_ptr outputter); + PushResult FLOG_CALL add_outputter(const std::string& name, std::unique_ptr outputter); + std::optional FLOG_CALL get_outputter() noexcept; std::shared_ptr FLOG_CALL get_logger(const std::string &name); @@ -45,7 +45,7 @@ namespace flog { std::atomic m_minLogLevel{LogLevel::DEBUG}; mutable std::shared_mutex m_logOutputtersMutex; - std::vector> m_logOutputters; + std::unordered_map> m_logOutputters; mutable std::mutex m_loggersMutex; std::unordered_map> m_loggers; diff --git a/include/featherlog/push_result.hpp b/include/featherlog/push_result.hpp new file mode 100644 index 0000000..e65971e --- /dev/null +++ b/include/featherlog/push_result.hpp @@ -0,0 +1,24 @@ +#ifndef FEATHERLOG_PUSH_RESULT_HPP +#define FEATHERLOG_PUSH_RESULT_HPP + +#include + +namespace flog { + template + class PushResult { + public: + static PushResult getVoid() { + return PushResult(3); + } + + private: + explicit PushResult(std::optional value) : m_value(value) { + std::optional bad; + std::map> mp; + } + + std::optional m_value; + }; +} + +#endif diff --git a/src/log_manager.cpp b/src/log_manager.cpp index 9bc699d..bfe252b 100644 --- a/src/log_manager.cpp +++ b/src/log_manager.cpp @@ -6,17 +6,6 @@ namespace flog { return std::shared_ptr(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); } @@ -25,9 +14,13 @@ namespace flog { m_minLogLevel.store(level, std::memory_order_relaxed); } - void LogManager::add_outputter(std::unique_ptr outputter) { + bool LogManager::add_outputter(const std::string& name, std::unique_ptr outputter) { std::lock_guard lock(m_logOutputtersMutex); - m_logOutputters.push_back(std::move(outputter)); + if (m_logOutputters.contains(name)) { + return false; + } + m_logOutputters[name] = std::move(outputter); + return true; } std::shared_ptr LogManager::get_logger(const std::string &name) {