feat(logger): improve logger initialization

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
k4yt3x 2024-12-20 00:00:00 +00:00
parent f3999a431c
commit 7b8e186207
No known key found for this signature in database
3 changed files with 34 additions and 26 deletions

View File

@ -21,7 +21,7 @@ class LIBVIDEO2X_API LoggerManager {
std::shared_ptr<spdlog::logger> logger(); std::shared_ptr<spdlog::logger> logger();
void reconfigure_logger( bool reconfigure_logger(
const std::string &logger_name, const std::string &logger_name,
const std::vector<spdlog::sink_ptr> &sinks, const std::vector<spdlog::sink_ptr> &sinks,
const std::string &pattern = "%+" const std::string &pattern = "%+"
@ -35,7 +35,7 @@ class LIBVIDEO2X_API LoggerManager {
private: private:
LoggerManager(); LoggerManager();
std::shared_ptr<spdlog::logger> logger_; std::shared_ptr<spdlog::logger> logger_ = nullptr;
}; };
} // namespace logger_manager } // namespace logger_manager

View File

@ -48,9 +48,9 @@ namespace logger_manager {
LoggerManager::LoggerManager() { LoggerManager::LoggerManager() {
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_pattern("%+"); console_sink->set_pattern("%+");
console_sink->set_level(spdlog::level::info);
logger_ = std::make_shared<spdlog::logger>("video2x", console_sink); logger_ = std::make_shared<spdlog::logger>("video2x", console_sink);
spdlog::register_logger(logger_); spdlog::register_logger(logger_);
logger_->set_level(spdlog::level::info);
} }
LoggerManager &LoggerManager::instance() { LoggerManager &LoggerManager::instance() {
@ -62,31 +62,36 @@ std::shared_ptr<spdlog::logger> LoggerManager::logger() {
return logger_; return logger_;
} }
void LoggerManager::reconfigure_logger( bool LoggerManager::reconfigure_logger(
const std::string &logger_name, const std::string &logger_name,
const std::vector<spdlog::sink_ptr> &sinks, const std::vector<spdlog::sink_ptr> &sinks,
const std::string &pattern const std::string &pattern
) { ) {
if (!sinks.empty()) { if (logger_name.empty() || sinks.empty()) {
// If a logger with the same name exists, remove it first return false;
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<spdlog::logger>(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_);
} }
// Create a new logger with the given name, sinks, and pattern
std::shared_ptr<spdlog::logger> new_logger =
std::make_shared<spdlog::logger>(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<spdlog::logger> 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) { bool LoggerManager::set_log_level(const std::string &level_str) {

View File

@ -56,9 +56,12 @@ int main(int argc, char **argv) {
// Initialize newline-safe logger with custom formatting pattern // Initialize newline-safe logger with custom formatting pattern
std::shared_ptr<newline_safe_sink> logger_sink = std::make_shared<newline_safe_sink>(); std::shared_ptr<newline_safe_sink> logger_sink = std::make_shared<newline_safe_sink>();
std::vector<spdlog::sink_ptr> sinks = {logger_sink}; std::vector<spdlog::sink_ptr> sinks = {logger_sink};
video2x::logger_manager::LoggerManager::instance().reconfigure_logger( if (!video2x::logger_manager::LoggerManager::instance().reconfigure_logger(
"video2x", sinks, "[%Y-%m-%d %H:%M:%S] [%^%l%$] %v" "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 // Initialize argument and configuration structs
Arguments arguments; Arguments arguments;