diff --git a/include/spdlog/cfg/env-inl.h b/include/spdlog/cfg/env-inl.h index 6b76b214..46fead3a 100644 --- a/include/spdlog/cfg/env-inl.h +++ b/include/spdlog/cfg/env-inl.h @@ -84,27 +84,25 @@ SPDLOG_INLINE std::unordered_map extract_key_vals_(con return rv; } -inline details::logger_configs from_env_() +inline details::registry::logger_cfgs from_env_() { using details::os::getenv; - details::logger_configs configs; + details::registry::logger_cfgs configs; auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL")); auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN")); // merge to single dict. and take into account "*" - std::string default_level_name = "info"; - std::string default_pattern = "%+"; for (auto &name_level : levels) { auto &logger_name = name_level.first; auto level_name = to_lower_(name_level.second); - details::logger_cfg cfg; + details::registry::logger_cfg cfg; cfg.level_name = level_name; - configs[logger_name] = cfg; + configs.loggers.emplace(logger_name, cfg); if (logger_name == "*") { - default_level_name = cfg.level_name; + configs.default_cfg.level_name = cfg.level_name; } } @@ -112,37 +110,25 @@ inline details::logger_configs from_env_() { auto &logger_name = name_pattern.first; auto &pattern = name_pattern.second; - auto it = configs.find(logger_name); + auto it = configs.loggers.find(logger_name); - if (it != configs.end()) + if (it != configs.loggers.end()) { it->second.pattern = pattern; } else { - details::logger_cfg cfg; + details::registry::logger_cfg cfg; cfg.pattern = pattern; - configs.insert({logger_name, cfg}); + //configs.loggers.insert({logger_name, cfg}); + configs.loggers.emplace(logger_name, cfg); } if (logger_name == "*") { - default_pattern = pattern; + configs.default_cfg.pattern = pattern; } } - // fill missing fields with the default values - for (auto &cfg : configs) - { - auto &val = cfg.second; - if (val.pattern.empty()) - { - val.pattern = default_pattern; - } - if (val.level_name.empty()) - { - val.level_name = default_level_name; - } - } return configs; } diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 76355f2a..795fe062 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -263,40 +263,25 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat 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)); + // use default config if not found for this logger name + logger_cfg *cfg = cfg_it != configs.loggers.end() ? &cfg_it->second : &configs.default_cfg; + if(cfg->level_name.empty()) { + cfg->level_name = configs.default_cfg.level_name; } - 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); + if(cfg->pattern.empty()) { + cfg->pattern = configs.default_cfg.pattern; } + + logger->set_level(level::from_str(cfg->level_name)); + logger->set_formatter(details::make_unique(cfg->pattern)); } + logger_cfgs_ = std::move(configs); } SPDLOG_INLINE registry ®istry::instance() diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 43cc69ee..8b81501c 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -29,8 +29,8 @@ class registry public: struct logger_cfg { - std::string level_name; - std::string pattern; + std::string level_name = "info"; + std::string pattern = "%+"; }; struct logger_cfgs @@ -112,7 +112,7 @@ private: std::unique_ptr periodic_flusher_; std::shared_ptr default_logger_; bool automatic_registration_ = true; - logger_configs logger_configs_; + logger_cfgs logger_cfgs_; size_t backtrace_n_messages_ = 0; };