mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-24 06:32:06 +08:00
Fix issue #1680
This commit is contained in:
parent
54a8259b42
commit
3a68eecb28
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/logger.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@ -17,30 +17,30 @@ class log_levels
|
|||||||
public:
|
public:
|
||||||
void set(const std::string &logger_name, level::level_enum lvl)
|
void set(const std::string &logger_name, level::level_enum lvl)
|
||||||
{
|
{
|
||||||
if (logger_name.empty())
|
levels_[logger_name] = lvl;
|
||||||
{
|
|
||||||
default_level_ = lvl;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
levels_[logger_name] = lvl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_default(level::level_enum lvl)
|
void set_default(level::level_enum lvl)
|
||||||
{
|
{
|
||||||
default_level_ = lvl;
|
levels_[""] = lvl;
|
||||||
}
|
}
|
||||||
|
|
||||||
level::level_enum get(const std::string &logger_name)
|
// configure log level of given logger if it appears in the config list or if default level is set
|
||||||
|
void update_logger_level(spdlog::logger &logger)
|
||||||
{
|
{
|
||||||
|
auto &logger_name = logger.name();
|
||||||
auto it = levels_.find(logger_name);
|
auto it = levels_.find(logger_name);
|
||||||
return it != levels_.end() ? it->second : default_level_;
|
|
||||||
}
|
|
||||||
|
|
||||||
level::level_enum default_level()
|
// if logger was not configured, check if default log level was configured
|
||||||
{
|
if (it == levels_.end())
|
||||||
return default_level_;
|
{
|
||||||
|
it = levels_.find(""); //
|
||||||
|
}
|
||||||
|
|
||||||
|
if (it != levels_.end())
|
||||||
|
{
|
||||||
|
logger.set_level(it->second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace cfg
|
} // namespace cfg
|
||||||
|
@ -67,7 +67,8 @@ 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_);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_logger->set_level(levels_.get(new_logger->name()));
|
levels_.update_logger_level(*new_logger);
|
||||||
|
|
||||||
new_logger->flush_on(flush_level_);
|
new_logger->flush_on(flush_level_);
|
||||||
|
|
||||||
if (backtrace_n_messages_ > 0)
|
if (backtrace_n_messages_ > 0)
|
||||||
@ -270,7 +271,7 @@ SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels)
|
|||||||
for (auto &l : loggers_)
|
for (auto &l : loggers_)
|
||||||
{
|
{
|
||||||
auto &logger = l.second;
|
auto &logger = l.second;
|
||||||
logger->set_level(levels_.get(logger->name()));
|
levels_.update_logger_level(*logger);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ TEST_CASE("argv7", "[cfg]")
|
|||||||
spdlog::set_level(spdlog::level::info);
|
spdlog::set_level(spdlog::level::info);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("level-not-found-test1", "[cfg]")
|
TEST_CASE("level-not-set-test1", "[cfg]")
|
||||||
{
|
{
|
||||||
spdlog::drop("l1");
|
spdlog::drop("l1");
|
||||||
const char *argv[] = {"ignore", ""};
|
const char *argv[] = {"ignore", ""};
|
||||||
@ -103,10 +103,30 @@ TEST_CASE("level-not-found-test1", "[cfg]")
|
|||||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("level-not-found-test2", "[cfg]")
|
TEST_CASE("level-not-set-test2", "[cfg]")
|
||||||
{
|
{
|
||||||
spdlog::drop("l1");
|
spdlog::drop("l1");
|
||||||
|
spdlog::drop("l2");
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||||
|
|
||||||
|
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||||
|
l1->set_level(spdlog::level::warn);
|
||||||
|
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
||||||
|
l2->set_level(spdlog::level::warn);
|
||||||
|
|
||||||
|
load_argv_levels(2, argv);
|
||||||
|
|
||||||
|
REQUIRE(l1->level() == spdlog::level::trace);
|
||||||
|
REQUIRE(l2->level() == spdlog::level::warn);
|
||||||
|
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("level-not-set-test3", "[cfg]")
|
||||||
|
{
|
||||||
|
spdlog::drop("l1");
|
||||||
|
spdlog::drop("l2");
|
||||||
|
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||||
|
|
||||||
load_argv_levels(2, argv);
|
load_argv_levels(2, argv);
|
||||||
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||||
@ -116,3 +136,19 @@ TEST_CASE("level-not-found-test2", "[cfg]")
|
|||||||
REQUIRE(l2->level() == spdlog::level::info);
|
REQUIRE(l2->level() == spdlog::level::info);
|
||||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("level-not-set-test4", "[cfg]")
|
||||||
|
{
|
||||||
|
spdlog::drop("l1");
|
||||||
|
spdlog::drop("l2");
|
||||||
|
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,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::trace);
|
||||||
|
REQUIRE(l2->level() == spdlog::level::warn);
|
||||||
|
REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user