wip fix #1680 again

This commit is contained in:
gabime 2020-09-26 14:41:33 +03:00
parent 3a68eecb28
commit 1ac2dcc537
8 changed files with 98 additions and 29 deletions

View File

@ -31,7 +31,7 @@ inline void load_argv_levels(int argc, const char **argv)
{ {
auto levels_string = arg.substr(spdlog_level_prefix.size()); auto levels_string = arg.substr(spdlog_level_prefix.size());
auto levels = helpers::extract_levels(levels_string); auto levels = helpers::extract_levels(levels_string);
details::registry::instance().update_levels(std::move(levels)); details::registry::instance().set_levels(std::move(levels));
} }
} }
} }

View File

@ -31,7 +31,7 @@ inline void load_env_levels()
if (!env_val.empty()) if (!env_val.empty())
{ {
auto levels = helpers::extract_levels(env_val); auto levels = helpers::extract_levels(env_val);
details::registry::instance().update_levels(std::move(levels)); details::registry::instance().set_levels(std::move(levels));
} }
} }

View File

@ -73,27 +73,31 @@ inline std::unordered_map<std::string, std::string> extract_key_vals_(const std:
continue; continue;
} }
auto kv = extract_kv_('=', token); auto kv = extract_kv_('=', token);
if (kv.first.empty())
{
kv.first = "*";
}
rv[kv.first] = kv.second; rv[kv.first] = kv.second;
} }
return rv; return rv;
} }
SPDLOG_INLINE log_levels extract_levels(const std::string &input) SPDLOG_INLINE std::unordered_map<std::string, spdlog::level::level_enum> extract_levels(const std::string &input)
{ {
auto key_vals = extract_key_vals_(input); auto key_vals = extract_key_vals_(input);
log_levels rv; std::unordered_map<std::string, spdlog::level::level_enum> rv;
for (auto &name_level : key_vals) for (auto &name_level : key_vals)
{ {
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);
auto level = level::from_str(level_name); auto level = level::from_str(level_name);
// fallback to "info" if unrecognized level name // ignore unrecognized level names
if (level == level::off && level_name != "off") if (level == level::off && level_name != "off")
{ {
level = level::info; continue;
} }
rv.set(logger_name, level); rv[logger_name] = level;
} }
return rv; return rv;
} }

View File

@ -3,7 +3,8 @@
#pragma once #pragma once
#include <spdlog/cfg/log_levels.h> #include <spdlog/common.h>
#include <unordered_map>
namespace spdlog { namespace spdlog {
namespace cfg { namespace cfg {
@ -17,7 +18,7 @@ namespace helpers {
// turn off all logging except for logger1: "off,logger1=debug" // turn off all logging except for logger1: "off,logger1=debug"
// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info" // turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info"
// //
SPDLOG_API log_levels extract_levels(const std::string &txt); SPDLOG_API std::unordered_map<std::string, spdlog::level::level_enum> extract_levels(const std::string &txt);
} // namespace helpers } // namespace helpers
} // namespace cfg } // namespace cfg

View File

@ -12,7 +12,7 @@ namespace cfg {
class log_levels class log_levels
{ {
std::unordered_map<std::string, spdlog::level::level_enum> levels_; std::unordered_map<std::string, spdlog::level::level_enum> levels_;
spdlog::level::level_enum default_level_ = level::info; spdlog::level::level_enum global_level_ = level::info;
public: public:
void set(const std::string &logger_name, level::level_enum lvl) void set(const std::string &logger_name, level::level_enum lvl)
@ -20,27 +20,32 @@ public:
levels_[logger_name] = lvl; levels_[logger_name] = lvl;
} }
void set_default(level::level_enum lvl) void set_global_level(level::level_enum lvl)
{ {
levels_[""] = lvl; global_level_ = lvl;
} }
// configure log level of given logger if it appears in the config list or if default level is set level::level_enum default_level()
void update_logger_level(spdlog::logger &logger)
{ {
auto &logger_name = logger.name(); return global_level_;
auto it = levels_.find(logger_name); }
// if logger was not configured, check if default log level was configured // update log level of given logger if it appears in the config list or if default level is set
// return true if updated
bool update_logger_level(spdlog::logger &logger)
{
// if logger was not configured, check if global log level was configured
auto it = levels_.find(logger.name());
if (it == levels_.end()) if (it == levels_.end())
{ {
it = levels_.find(""); // it = levels_.find("*");
} }
if (it != levels_.end()) if (it != levels_.end())
{ {
logger.set_level(it->second); logger.set_level(it->second);
return true;
} }
return false;
} }
}; };
} // namespace cfg } // namespace cfg

View File

@ -66,8 +66,19 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
{ {
new_logger->set_error_handler(err_handler_); new_logger->set_error_handler(err_handler_);
} }
auto cfg_level_it = cfg_levels_.find(new_logger->name());
levels_.update_logger_level(*new_logger); if (cfg_level_it == cfg_levels_.end())
{
cfg_level_it = cfg_levels_.find(("*"));
}
if (cfg_level_it != cfg_levels_.end())
{
new_logger->set_level(cfg_level_it->second);
}
else
{
new_logger->set_level(global_log_level_);
}
new_logger->flush_on(flush_level_); new_logger->flush_on(flush_level_);
@ -172,7 +183,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
{ {
l.second->set_level(log_level); l.second->set_level(log_level);
} }
levels_.set_default(log_level); global_log_level_ = log_level;
} }
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level) SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
@ -264,14 +275,35 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
automatic_registration_ = automatic_registration; automatic_registration_ = automatic_registration;
} }
SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels) SPDLOG_INLINE void registry::set_levels(std::unordered_map<std::string, spdlog::level::level_enum> levels)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
levels_ = std::move(levels); cfg_levels_ = std::move(levels);
for (auto &l : loggers_) // update global level if found
auto global_level_it = cfg_levels_.find("*");
if (global_level_it != cfg_levels_.end())
{ {
auto &logger = l.second; auto global_level = global_level_it->second;
levels_.update_logger_level(*logger); for (auto &logger : loggers_)
{
logger.second->set_level(global_level);
}
}
// update specific loggers
for (const auto &item : cfg_levels_)
{
const auto &logger_name = item.first;
if (logger_name == "*")
{
continue;
}
auto existing_logger_it = loggers_.find(logger_name);
if (existing_logger_it != loggers_.end())
{
auto level = item.second;
existing_logger_it->second->set_level(level);
}
} }
} }

View File

@ -80,7 +80,7 @@ public:
void set_automatic_registration(bool automatic_registration); void set_automatic_registration(bool automatic_registration);
void update_levels(cfg::log_levels levels); void set_levels(std::unordered_map<std::string, spdlog::level::level_enum> levels);
static registry &instance(); static registry &instance();
@ -93,8 +93,9 @@ private:
std::mutex logger_map_mutex_, flusher_mutex_; std::mutex logger_map_mutex_, flusher_mutex_;
std::recursive_mutex tp_mutex_; std::recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_; std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
cfg::log_levels levels_; std::unordered_map<std::string, spdlog::level::level_enum> cfg_levels_;
std::unique_ptr<formatter> formatter_; std::unique_ptr<formatter> formatter_;
spdlog::level::level_enum global_log_level_ = level::info;
level::level_enum flush_level_ = level::off; level::level_enum flush_level_ = level::off;
void (*err_handler_)(const std::string &msg); void (*err_handler_)(const std::string &msg);
std::shared_ptr<thread_pool> tp_; std::shared_ptr<thread_pool> tp_;

View File

@ -1,3 +1,4 @@
#include "includes.h" #include "includes.h"
#include "test_sink.h" #include "test_sink.h"
@ -149,6 +150,31 @@ TEST_CASE("level-not-set-test4", "[cfg]")
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2"); auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
REQUIRE(l1->level() == spdlog::level::trace); REQUIRE(l1->level() == spdlog::level::trace);
// REQUIRE(l2->level() == spdlog::level::warn);
// REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn);
}
TEST_CASE("level-not-set-test5", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=junk,warn"};
load_argv_levels(2, argv);
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
REQUIRE(l1->level() == spdlog::level::warn);
REQUIRE(l2->level() == spdlog::level::warn); REQUIRE(l2->level() == spdlog::level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn); REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn);
} }
TEST_CASE("restore-to-default", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=info"};
load_argv_levels(2, argv);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
}