mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
Modified from_str() to perform case-insensitive comparison for all level names. This allows environment variables and SPDLOG_LEVEL_NAMES to use uppercase or mixed case level names (e.g., DEBUG, INFO, Warning) while maintaining full backward compatibility with existing lowercase names.
This commit is contained in:
@@ -29,15 +29,31 @@ SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOE
|
||||
}
|
||||
|
||||
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT {
|
||||
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
|
||||
auto it = std::find_if(std::begin(level_string_views), std::end(level_string_views),
|
||||
[&name](const string_view_t &level_name) {
|
||||
return level_name.size() == name.size() &&
|
||||
std::equal(name.begin(), name.end(), level_name.begin(),
|
||||
[](char a, char b) {
|
||||
return std::tolower(static_cast<unsigned char>(a)) ==
|
||||
std::tolower(static_cast<unsigned char>(b));
|
||||
});
|
||||
});
|
||||
if (it != std::end(level_string_views))
|
||||
return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it));
|
||||
|
||||
// check also for "warn" and "err" before giving up..
|
||||
if (name == "warn") {
|
||||
auto iequals = [](const std::string &a, const std::string &b) {
|
||||
return a.size() == b.size() &&
|
||||
std::equal(a.begin(), a.end(), b.begin(), [](char ac, char bc) {
|
||||
return std::tolower(static_cast<unsigned char>(ac)) ==
|
||||
std::tolower(static_cast<unsigned char>(bc));
|
||||
});
|
||||
};
|
||||
|
||||
if (iequals(name, "warn")) {
|
||||
return level::warn;
|
||||
}
|
||||
if (name == "err") {
|
||||
if (iequals(name, "err")) {
|
||||
return level::err;
|
||||
}
|
||||
return level::off;
|
||||
|
||||
@@ -176,3 +176,33 @@ TEST_CASE("restore-to-default", "[cfg]") {
|
||||
load_argv_levels(2, argv);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("uppercase-level-names", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
|
||||
#ifdef CATCH_PLATFORM_WINDOWS
|
||||
_putenv_s("SPDLOG_LEVEL", "l1=DEBUG,INFO");
|
||||
#else
|
||||
setenv("SPDLOG_LEVEL", "l1=DEBUG,INFO", 1);
|
||||
#endif
|
||||
load_env_levels();
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
auto l2 = spdlog::create<test_sink_st>("l2");
|
||||
|
||||
REQUIRE(l1->level() == spdlog::level::debug);
|
||||
REQUIRE(l2->level() == spdlog::level::info);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||
|
||||
// Test with argv
|
||||
spdlog::drop("l3");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l3=WARN,ERROR"};
|
||||
load_argv_levels(2, argv);
|
||||
auto l3 = spdlog::create<test_sink_st>("l3");
|
||||
|
||||
REQUIRE(l3->level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
|
||||
|
||||
// Reset to info
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,17 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]") {
|
||||
REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
|
||||
REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level::from_str("null") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level::from_str("TRACE") == spdlog::level::trace);
|
||||
REQUIRE(spdlog::level::from_str("DEBUG") == spdlog::level::debug);
|
||||
REQUIRE(spdlog::level::from_str("INFO") == spdlog::level::info);
|
||||
REQUIRE(spdlog::level::from_str("WARNING") == spdlog::level::warn);
|
||||
REQUIRE(spdlog::level::from_str("WARN") == spdlog::level::warn);
|
||||
REQUIRE(spdlog::level::from_str("ERROR") == spdlog::level::err);
|
||||
REQUIRE(spdlog::level::from_str("ERR") == spdlog::level::err);
|
||||
REQUIRE(spdlog::level::from_str("CRITICAL") == spdlog::level::critical);
|
||||
REQUIRE(spdlog::level::from_str("OFF") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level::from_str("TrAcE") == spdlog::level::trace);
|
||||
REQUIRE(spdlog::level::from_str("DeBuG") == spdlog::level::debug);
|
||||
}
|
||||
|
||||
TEST_CASE("periodic flush", "[periodic_flush]") {
|
||||
|
||||
Reference in New Issue
Block a user