mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 10:31:34 +08:00
wip
This commit is contained in:
parent
dac61d4e9c
commit
35e9482574
@ -84,27 +84,25 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> 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;
|
||||
}
|
||||
|
||||
|
@ -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<std::mutex> 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_formatter>(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<pattern_formatter>(cfg->pattern));
|
||||
}
|
||||
logger_cfgs_ = std::move(configs);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE registry ®istry::instance()
|
||||
|
@ -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_worker> periodic_flusher_;
|
||||
std::shared_ptr<logger> default_logger_;
|
||||
bool automatic_registration_ = true;
|
||||
logger_configs logger_configs_;
|
||||
logger_cfgs logger_cfgs_;
|
||||
size_t backtrace_n_messages_ = 0;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user