This commit is contained in:
gabime 2019-12-13 01:14:38 +02:00
parent dac61d4e9c
commit 35e9482574
3 changed files with 24 additions and 53 deletions

View File

@ -84,27 +84,25 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
return rv; return rv;
} }
inline details::logger_configs from_env_() inline details::registry::logger_cfgs from_env_()
{ {
using details::os::getenv; using details::os::getenv;
details::logger_configs configs; details::registry::logger_cfgs configs;
auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL")); auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL"));
auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN")); auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN"));
// merge to single dict. and take into account "*" // merge to single dict. and take into account "*"
std::string default_level_name = "info";
std::string default_pattern = "%+";
for (auto &name_level : levels) for (auto &name_level : levels)
{ {
auto &logger_name = name_level.first; auto &logger_name = name_level.first;
auto level_name = to_lower_(name_level.second); auto level_name = to_lower_(name_level.second);
details::logger_cfg cfg; details::registry::logger_cfg cfg;
cfg.level_name = level_name; cfg.level_name = level_name;
configs[logger_name] = cfg; configs.loggers.emplace(logger_name, cfg);
if (logger_name == "*") 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 &logger_name = name_pattern.first;
auto &pattern = name_pattern.second; 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; it->second.pattern = pattern;
} }
else else
{ {
details::logger_cfg cfg; details::registry::logger_cfg cfg;
cfg.pattern = pattern; cfg.pattern = pattern;
configs.insert({logger_name, cfg}); //configs.loggers.insert({logger_name, cfg});
configs.loggers.emplace(logger_name, cfg);
} }
if (logger_name == "*") 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; return configs;
} }

View File

@ -263,40 +263,25 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
SPDLOG_INLINE void registry::set_configs(logger_cfgs configs) SPDLOG_INLINE void registry::set_configs(logger_cfgs configs)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
logger_configs_ = std::move(configs);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
auto &logger = l.second; auto &logger = l.second;
auto cfg_it = configs.loggers.find(logger->name()); auto cfg_it = configs.loggers.find(logger->name());
if (cfg_it != configs.loggers.end()) // use default config if not found for this logger name
{ logger_cfg *cfg = cfg_it != configs.loggers.end() ? &cfg_it->second : &configs.default_cfg;
// set level if(cfg->level_name.empty()) {
auto level = level::from_str(cfg_it->second.level_name); cfg->level_name = configs.default_cfg.level_name;
logger->set_level(level);
// set pattern
auto &pattern = cfg_it->second.pattern;
logger->set_formatter(details::make_unique<pattern_formatter>(pattern));
} }
else // not found in cfgs - give it default settings
{
logger
}
}
for (const auto &logger_cfg : configs) if(cfg->pattern.empty()) {
{ cfg->pattern = configs.default_cfg.pattern;
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);
} }
logger->set_level(level::from_str(cfg->level_name));
logger->set_formatter(details::make_unique<pattern_formatter>(cfg->pattern));
} }
logger_cfgs_ = std::move(configs);
} }
SPDLOG_INLINE registry &registry::instance() SPDLOG_INLINE registry &registry::instance()

View File

@ -29,8 +29,8 @@ class registry
public: public:
struct logger_cfg struct logger_cfg
{ {
std::string level_name; std::string level_name = "info";
std::string pattern; std::string pattern = "%+";
}; };
struct logger_cfgs struct logger_cfgs
@ -112,7 +112,7 @@ private:
std::unique_ptr<periodic_worker> periodic_flusher_; std::unique_ptr<periodic_worker> periodic_flusher_;
std::shared_ptr<logger> default_logger_; std::shared_ptr<logger> default_logger_;
bool automatic_registration_ = true; bool automatic_registration_ = true;
logger_configs logger_configs_; logger_cfgs logger_cfgs_;
size_t backtrace_n_messages_ = 0; size_t backtrace_n_messages_ = 0;
}; };