Skip to content

Commit

Permalink
added spdlog::level::set_string_view to enable alternate log level na…
Browse files Browse the repository at this point in the history
…mes without changing the build via SPDLOG_LEVEL_NAMES
  • Loading branch information
stevenlunt committed Mar 24, 2021
1 parent 53e1c9a commit 2a16d1d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/spdlog/common-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) S
return level_string_views[l];
}

SPDLOG_INLINE void set_string_view(spdlog::level::level_enum l, const string_view_t &s) SPDLOG_NOEXCEPT
{
level_string_views[l] = s;
}

SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
{
return short_level_names[l];
Expand Down
1 change: 1 addition & 0 deletions include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ enum level_enum
#endif

SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
SPDLOG_API void set_string_view(spdlog::level::level_enum l, const string_view_t &s) SPDLOG_NOEXCEPT;
SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;

Expand Down
8 changes: 8 additions & 0 deletions tests/test_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ TEST_CASE("level_to_string_view", "[convert_to_string_view")
REQUIRE(spdlog::level::to_string_view(spdlog::level::off) == "off");
}

TEST_CASE("set_level_to_string_view", "[set_string_view")
{
spdlog::level::set_string_view(spdlog::level::info, "INF");
REQUIRE(spdlog::level::to_string_view(spdlog::level::info) == "INF");
spdlog::level::set_string_view(spdlog::level::info, "info"); // set it back
REQUIRE(spdlog::level::to_string_view(spdlog::level::info) == "info");
}

TEST_CASE("to_short_c_str", "[convert_to_short_c_str]")
{
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::trace)) == "T");
Expand Down

0 comments on commit 2a16d1d

Please sign in to comment.