diff --git a/include/spdlog/common.h b/include/spdlog/common.h index cbdf5f56..cb656386 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -15,7 +15,8 @@ #include #include #include -#include +#include +#include #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) #include @@ -86,6 +87,7 @@ enum level_enum off = 6 }; + #if !defined(SPDLOG_LEVEL_NAMES) #define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off" } #endif @@ -102,8 +104,29 @@ inline const char* to_short_str(spdlog::level::level_enum l) { return short_level_names[l]; } -using level_hasher = std::hash; +inline spdlog::level::level_enum to_level_enum(const std::string& name) +{ + static std::unordered_map name_to_level = { + { level_names[0], level::trace }, + { level_names[1], level::debug }, + { level_names[2], level::info }, + { level_names[3], level::warn }, + { level_names[4], level::err }, + { level_names[5], level::critical }, + { level_names[6], level::off } + }; + auto ci = name_to_level.find(name); + if (ci != name_to_level.end()) + { + return ci->second; + } + else + { + return level::off; + } +} +using level_hasher = std::hash; } //level // diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index 9afaa932..9f97a9d8 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -42,6 +42,39 @@ TEST_CASE("log_levels", "[log_levels]") REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello"); } +TEST_CASE("to_str", "[convert_to_str]") +{ + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::trace)) == "trace"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::debug)) == "debug"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::info)) == "info"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::warn)) == "warning"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::err)) == "error"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::critical)) == "critical"); + REQUIRE(std::string(spdlog::level::to_str(spdlog::level::off)) == "off"); +} + +TEST_CASE("to_short_str", "[convert_to_short_str]") +{ + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::trace)) == "T"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::debug)) == "D"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::info)) == "I"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::warn)) == "W"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::err)) == "E"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::critical)) == "C"); + REQUIRE(std::string(spdlog::level::to_short_str(spdlog::level::off)) == "O"); +} + +TEST_CASE("to_level_enum", "[convert_to_level_enum]") +{ + REQUIRE(spdlog::level::to_level_enum("trace") == spdlog::level::trace); + REQUIRE(spdlog::level::to_level_enum("debug") == spdlog::level::debug); + REQUIRE(spdlog::level::to_level_enum("info") == spdlog::level::info); + REQUIRE(spdlog::level::to_level_enum("warning") == spdlog::level::warn); + REQUIRE(spdlog::level::to_level_enum("error") == spdlog::level::err); + REQUIRE(spdlog::level::to_level_enum("critical") == spdlog::level::critical); + REQUIRE(spdlog::level::to_level_enum("off") == spdlog::level::off); + REQUIRE(spdlog::level::to_level_enum("null") == spdlog::level::off); +}