From 68ed281461ec13c4b5c1c5f3d78397df88f75e93 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 21 Dec 2019 15:54:09 +0200 Subject: [PATCH] Refactored to cfg::log_levels class --- example/example.cpp | 3 +- include/spdlog/cfg/argv_loader.h | 24 +++---------- include/spdlog/cfg/cfg.h | 49 +++++++++++++++++++++++++++ include/spdlog/cfg/text_loader-inl.h | 17 +++------- include/spdlog/cfg/text_loader.h | 3 +- include/spdlog/details/registry-inl.h | 13 +++---- include/spdlog/details/registry.h | 11 ++---- 7 files changed, 68 insertions(+), 52 deletions(-) create mode 100644 include/spdlog/cfg/cfg.h diff --git a/example/example.cpp b/example/example.cpp index ebcdc35a..9ff70b10 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -25,9 +25,8 @@ void syslog_example(); int main(int args, char *argv[]) { - spdlog::cfg::init_from_env(); + //spdlog::cfg::init_from_env(); spdlog::cfg::init_from_argv(args, argv); - spdlog::debug("HELLO DEBUG"); spdlog::info("HELLO INFO"); auto l1 = spdlog::stderr_color_st("l1"); diff --git a/include/spdlog/cfg/argv_loader.h b/include/spdlog/cfg/argv_loader.h index 51ee9773..4a9ed1de 100644 --- a/include/spdlog/cfg/argv_loader.h +++ b/include/spdlog/cfg/argv_loader.h @@ -6,32 +6,17 @@ #include // -// Init levels and patterns from env variables SPDLOG_LEVEL -// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger). -// Note - fallback to "info" level on unrecognized levels -// -// Examples: -// -// set global level to debug: -// export SPDLOG_LEVEL=debug -// -// turn off all logging except for logger1: -// export SPDLOG_LEVEL="off,logger1=debug" -// -// turn off all logging except for logger1 and logger2: -// export SPDLOG_LEVEL="off,logger1=debug,logger2=info" +// Init levels from argv SPDLOG_LEVEL +// Example: my_program.exe "SPDLOG_LEVEL=trace" + namespace spdlog { namespace cfg { // search for SPDLOG_LEVEL= in the args and use it to init the levels void init_from_argv(int args, char *argv[]) { - if (args < 2) - { - return; - } const std::string spdlog_level_prefix = "SPDLOG_LEVEL="; - for (int i = 0; i < args; i++) + for (int i = 1; i < args; i++) { std::string arg = argv[i]; if (arg.find(spdlog_level_prefix) == 0) @@ -39,7 +24,6 @@ void init_from_argv(int args, char *argv[]) auto cfg_string = arg.substr(spdlog_level_prefix.size()); auto levels = text_loader::load_levels(cfg_string); spdlog::details::registry::instance().set_levels(levels); - return; } } } diff --git a/include/spdlog/cfg/cfg.h b/include/spdlog/cfg/cfg.h new file mode 100644 index 00000000..9f9d3c5b --- /dev/null +++ b/include/spdlog/cfg/cfg.h @@ -0,0 +1,49 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +namespace spdlog { + namespace cfg { + class log_levels + { + + public: + using levels_map = std::unordered_map; + + void set(const std::string& logger_name, level::level_enum lvl) + { + if (logger_name.empty()) + { + default_level_ = lvl; + } + else + { + levels_[logger_name] = lvl; + } + } + + level::level_enum get(const std::string &logger_name) + { + auto it = levels_.find(logger_name); + return it != levels_.end() ? it->second : default_level_; + } + + level::level_enum get() + { + return default_level_; + } + + private: + std::unordered_map levels_; + spdlog::level::level_enum default_level_ = level::info; + + }; + } // namespace cfg +} // namespace spdlog + + diff --git a/include/spdlog/cfg/text_loader-inl.h b/include/spdlog/cfg/text_loader-inl.h index de586a17..5ceb3fc5 100644 --- a/include/spdlog/cfg/text_loader-inl.h +++ b/include/spdlog/cfg/text_loader-inl.h @@ -79,10 +79,10 @@ SPDLOG_INLINE std::unordered_map extract_key_vals_(con return rv; } -inline details::registry::logger_levels extract_levels_(const std::string &input) +inline log_levels extract_levels_(const std::string &input) { auto key_vals = extract_key_vals_(input); - details::registry::logger_levels rv; + log_levels rv; for (auto &name_level : key_vals) { @@ -94,22 +94,15 @@ inline details::registry::logger_levels extract_levels_(const std::string &input { level = level::info; } + rv.set(logger_name, level); - if (logger_name.empty() || logger_name == "*") - { - rv.default_level = level; - } - else - { - rv.levels[logger_name] = level; - } } return rv; } -SPDLOG_INLINE details::registry::logger_levels load_levels(const std::string &cfg) +SPDLOG_INLINE log_levels load_levels(const std::string &txt) { - return extract_levels_(cfg); + return extract_levels_(txt); } } // namespace text_loader diff --git a/include/spdlog/cfg/text_loader.h b/include/spdlog/cfg/text_loader.h index ade7eb25..a0c8cd3f 100644 --- a/include/spdlog/cfg/text_loader.h +++ b/include/spdlog/cfg/text_loader.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -26,7 +27,7 @@ namespace spdlog { namespace cfg { namespace text_loader { -details::registry::logger_levels load_levels(const std::string &cfg); +log_levels load_levels(const std::string &txt); } } // namespace cfg diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 7d4fa746..2697f070 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -30,11 +30,6 @@ namespace spdlog { namespace details { -SPDLOG_INLINE level::level_enum registry::logger_levels::get_or_default(const std::string &name) -{ - auto it = levels.find(name); - return it != levels.end() ? it->second : default_level; -} SPDLOG_INLINE registry::registry() : formatter_(new pattern_formatter()) @@ -70,7 +65,7 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr new_logge new_logger->set_error_handler(err_handler_); } - new_logger->set_level(levels_.get_or_default(new_logger->name())); + new_logger->set_level(levels_.get(new_logger->name())); new_logger->flush_on(flush_level_); if (backtrace_n_messages_ > 0) @@ -174,7 +169,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level) { l.second->set_level(log_level); } - levels_.default_level = log_level; + levels_.set("*", log_level); } SPDLOG_INLINE void registry::flush_on(level::level_enum log_level) @@ -266,14 +261,14 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat automatic_registration_ = automatic_registration; } -SPDLOG_INLINE void registry::set_levels(logger_levels levels) +SPDLOG_INLINE void registry::set_levels(cfg::log_levels levels) { std::lock_guard lock(logger_map_mutex_); levels_ = std::move(levels); for (auto &l : loggers_) { auto &logger = l.second; - logger->set_level(levels_.get_or_default(logger->name())); + logger->set_level(levels_.get(logger->name())); } } diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 4590c5b8..1f7dcde8 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -9,6 +9,7 @@ // This class is thread safe #include +#include #include #include @@ -27,12 +28,6 @@ class periodic_worker; class registry { public: - struct logger_levels - { - std::unordered_map levels; - spdlog::level::level_enum default_level = level::info; - spdlog::level::level_enum get_or_default(const std::string &name); - }; registry(const registry &) = delete; registry &operator=(const registry &) = delete; @@ -86,7 +81,7 @@ public: void set_automatic_registration(bool automatic_registration); - void set_levels(logger_levels levels); + void set_levels(cfg::log_levels levels); static registry &instance(); @@ -99,7 +94,7 @@ private: std::mutex logger_map_mutex_, flusher_mutex_; std::recursive_mutex tp_mutex_; std::unordered_map> loggers_; - logger_levels levels_; + cfg::log_levels levels_; std::unique_ptr formatter_; level::level_enum flush_level_ = level::off; void (*err_handler_)(const std::string &msg);