From 7b8e1862077cd2c73e954df9e72651a78aed4bd8 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Fri, 20 Dec 2024 00:00:00 +0000 Subject: [PATCH] feat(logger): improve logger initialization Signed-off-by: k4yt3x --- include/libvideo2x/logger_manager.h | 4 +-- src/logger_manager.cpp | 47 ++++++++++++++++------------- tools/video2x/src/video2x.cpp | 9 ++++-- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/include/libvideo2x/logger_manager.h b/include/libvideo2x/logger_manager.h index e36cdb9..5ca8a4b 100644 --- a/include/libvideo2x/logger_manager.h +++ b/include/libvideo2x/logger_manager.h @@ -21,7 +21,7 @@ class LIBVIDEO2X_API LoggerManager { std::shared_ptr logger(); - void reconfigure_logger( + bool reconfigure_logger( const std::string &logger_name, const std::vector &sinks, const std::string &pattern = "%+" @@ -35,7 +35,7 @@ class LIBVIDEO2X_API LoggerManager { private: LoggerManager(); - std::shared_ptr logger_; + std::shared_ptr logger_ = nullptr; }; } // namespace logger_manager diff --git a/src/logger_manager.cpp b/src/logger_manager.cpp index 51e130d..0abcd83 100644 --- a/src/logger_manager.cpp +++ b/src/logger_manager.cpp @@ -48,9 +48,9 @@ namespace logger_manager { LoggerManager::LoggerManager() { auto console_sink = std::make_shared(); console_sink->set_pattern("%+"); + console_sink->set_level(spdlog::level::info); logger_ = std::make_shared("video2x", console_sink); spdlog::register_logger(logger_); - logger_->set_level(spdlog::level::info); } LoggerManager &LoggerManager::instance() { @@ -62,31 +62,36 @@ std::shared_ptr LoggerManager::logger() { return logger_; } -void LoggerManager::reconfigure_logger( +bool LoggerManager::reconfigure_logger( const std::string &logger_name, const std::vector &sinks, const std::string &pattern ) { - if (!sinks.empty()) { - // If a logger with the same name exists, remove it first - auto old_logger = spdlog::get(logger_name); - if (old_logger) { - spdlog::drop(logger_name); - } - - // Create a new logger with the given name, sinks, and pattern - auto new_logger = std::make_shared(logger_name, sinks.begin(), sinks.end()); - new_logger->set_pattern(pattern); - - // Maintain the log level from the previous logger - if (logger_) { - new_logger->set_level(logger_->level()); - } - - // Replace the internal logger_ member and register the new one - logger_ = new_logger; - spdlog::register_logger(logger_); + if (logger_name.empty() || sinks.empty()) { + return false; } + + // Create a new logger with the given name, sinks, and pattern + std::shared_ptr new_logger = + std::make_shared(logger_name, sinks.begin(), sinks.end()); + new_logger->set_pattern(pattern); + + // Maintain the log level from the previous logger + if (logger_ != nullptr) { + new_logger->set_level(logger_->level()); + } + + // If a logger with the same name exists, remove it first + std::shared_ptr old_logger = spdlog::get(logger_name); + if (old_logger != nullptr) { + spdlog::drop(logger_name); + logger_ = nullptr; + } + + // Replace the internal logger_ member and register the new one + logger_ = new_logger; + spdlog::register_logger(logger_); + return true; } bool LoggerManager::set_log_level(const std::string &level_str) { diff --git a/tools/video2x/src/video2x.cpp b/tools/video2x/src/video2x.cpp index 86b0e72..06cd56a 100644 --- a/tools/video2x/src/video2x.cpp +++ b/tools/video2x/src/video2x.cpp @@ -56,9 +56,12 @@ int main(int argc, char **argv) { // Initialize newline-safe logger with custom formatting pattern std::shared_ptr logger_sink = std::make_shared(); std::vector sinks = {logger_sink}; - video2x::logger_manager::LoggerManager::instance().reconfigure_logger( - "video2x", sinks, "[%Y-%m-%d %H:%M:%S] [%^%l%$] %v" - ); + if (!video2x::logger_manager::LoggerManager::instance().reconfigure_logger( + "video2x", sinks, "[%Y-%m-%d %H:%M:%S] [%^%l%$] %v" + )) { + std::cerr << "Error: Failed to configure logger." << std::endl; + return 1; + } // Initialize argument and configuration structs Arguments arguments;