From 8d9d9899b7aa44ada290307f83866c68bb0b64fd Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 12 Dec 2019 20:00:18 +0200 Subject: [PATCH] wip --- example/example.cpp | 8 +---- include/spdlog/cfg/env-inl.h | 35 +++----------------- include/spdlog/cfg/env.h | 3 -- include/spdlog/details/registry-inl.h | 46 +++++++++++++++++++++++++++ include/spdlog/details/registry.h | 18 +++++++++++ 5 files changed, 70 insertions(+), 40 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c2101f84..fec6b0b4 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -11,14 +11,8 @@ int main(int, char *[]) { try { - spdlog::cfg::env::init(); + spdlog::env::init(); spdlog::info("Hello"); - - // auto cfg = spdlog::cfg::from_env(); - // for (auto &item : cfg) - // { - // spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern); - // } } catch (spdlog::spdlog_ex &ex) { diff --git a/include/spdlog/cfg/env-inl.h b/include/spdlog/cfg/env-inl.h index 0f590070..136eabd9 100644 --- a/include/spdlog/cfg/env-inl.h +++ b/include/spdlog/cfg/env-inl.h @@ -16,16 +16,9 @@ #include namespace spdlog { -namespace cfg { namespace env { -struct logger_cfg -{ - std::string level_name; - std::string pattern; -}; -using cfg_map = std::unordered_map; using name_val_pair = std::pair; // inplace convert to lowercase @@ -92,10 +85,10 @@ SPDLOG_INLINE std::unordered_map extract_key_vals_(con return rv; } -inline cfg_map from_env_() +inline details::logger_configs from_env_() { using details::os::getenv; - cfg_map configs; + details::logger_configs configs; auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL")); auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN")); @@ -107,7 +100,7 @@ inline cfg_map from_env_() { auto &logger_name = name_level.first; auto level_name = to_lower_(name_level.second); - logger_cfg cfg; + details::logger_cfg cfg; cfg.level_name = level_name; configs[logger_name] = cfg; if (logger_name == "*") @@ -128,7 +121,7 @@ inline cfg_map from_env_() } else { - logger_cfg cfg; + details::logger_cfg cfg; cfg.pattern = pattern; configs.insert({logger_name, cfg}); } @@ -156,26 +149,8 @@ inline cfg_map from_env_() SPDLOG_INLINE void init() { - auto cfg_map = from_env_(); - for (const auto &logger_cfg : cfg_map) - { - auto &logger_name = logger_cfg.first; - auto level = level::from_str(logger_cfg.second.level_name); - auto &pattern = logger_cfg.second.pattern; - - if (logger_name == "*") - { - spdlog::set_level(level); - spdlog::set_pattern(pattern); - } - else if (auto logger = spdlog::get(logger_name)) - { - logger->set_level(level); - logger->set_pattern(pattern); - } - } + spdlog::details::registry::instance().set_configs(from_env_()); } } // namespace env -} // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h index c52149eb..7402ed2f 100644 --- a/include/spdlog/cfg/env.h +++ b/include/spdlog/cfg/env.h @@ -32,12 +32,9 @@ // export SPDLOG_PATTERN="[%x] [%l] [%n] %v,logger1=[%u] %v" namespace spdlog { -namespace cfg { namespace env { void init(); } - -} // namespace cfg } // namespace spdlog #ifdef SPDLOG_HEADER_ONLY diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 562aa06c..5af3d4d4 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -260,6 +260,52 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat automatic_registration_ = automatic_registration; } +SPDLOG_INLINE void registry::set_configs(logger_cfgs configs) +{ + std::lock_guard lock(logger_map_mutex_); + logger_configs_ = std::move(configs); + + for (auto &l : loggers_) + { + auto &logger = l.second; + auto cfg_it = configs.loggers.find(logger->name()); + + if (cfg_it != configs.loggers.end()) + { + // set level + auto level = level::from_str(cfg_it->second.level_name); + logger->set_level(level); + // set pattern + auto &pattern = cfg_it->second.pattern; + logger->set_formatter(details::make_unique(pattern)); + } + else //not found in cfgs - give it default settings + { + logger + } + + + } + + for (const auto &logger_cfg : configs) + { + auto &logger_name = logger_cfg.first; + auto level = level::from_str(logger_cfg.second.level_name); + auto &pattern = logger_cfg.second.pattern; + + + if (auto logger = this->get(logger_name)) + { + logger->set_level(level); + logger->set_pattern(pattern); + } + + } + +} + + + SPDLOG_INLINE registry ®istry::instance() { static registry s_instance; diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 6ac571dc..ec2e8a0f 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -24,9 +24,24 @@ namespace details { class thread_pool; class periodic_worker; + + + class registry { public: + struct logger_cfg + { + std::string level_name; + std::string pattern; + }; + + struct logger_cfgs + { + std::unordered_map loggers; + logger_cfg default_cfg; + }; + registry(const registry &) = delete; registry &operator=(const registry &) = delete; @@ -79,6 +94,8 @@ public: void set_automatic_registration(bool automatic_registration); + void set_configs(logger_cfgs configs); + static registry &instance(); private: @@ -98,6 +115,7 @@ private: std::unique_ptr periodic_flusher_; std::shared_ptr default_logger_; bool automatic_registration_ = true; + logger_configs logger_configs_; size_t backtrace_n_messages_ = 0; };