This commit is contained in:
gabime 2019-12-12 20:00:18 +02:00
parent cff7448fb2
commit 8d9d9899b7
5 changed files with 70 additions and 40 deletions

View File

@ -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)
{

View File

@ -16,16 +16,9 @@
#include <sstream>
namespace spdlog {
namespace cfg {
namespace env {
struct logger_cfg
{
std::string level_name;
std::string pattern;
};
using cfg_map = std::unordered_map<std::string, logger_cfg>;
using name_val_pair = std::pair<std::string, std::string>;
// inplace convert to lowercase
@ -92,10 +85,10 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> 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

View File

@ -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

View File

@ -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<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));
}
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 &registry::instance()
{
static registry s_instance;

View File

@ -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<std::string, logger_cfg> 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_worker> periodic_flusher_;
std::shared_ptr<logger> default_logger_;
bool automatic_registration_ = true;
logger_configs logger_configs_;
size_t backtrace_n_messages_ = 0;
};