mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Store level names as string_views
This commit is contained in:
@@ -107,13 +107,13 @@ enum level_enum
|
||||
"trace", "debug", "info", "warning", "error", "critical", "off" \
|
||||
}
|
||||
#endif
|
||||
static const char *level_names[] SPDLOG_LEVEL_NAMES;
|
||||
|
||||
static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
|
||||
static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
|
||||
|
||||
inline const char *to_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
||||
inline string_view_t& to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
||||
{
|
||||
return level_names[l];
|
||||
return level_string_views[l];
|
||||
}
|
||||
|
||||
inline const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
||||
@@ -123,17 +123,16 @@ inline const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
||||
|
||||
inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
|
||||
{
|
||||
static std::unordered_map<std::string, level_enum> name_to_level = // map string->level
|
||||
{{level_names[0], level::trace}, // trace
|
||||
{level_names[1], level::debug}, // debug
|
||||
{level_names[2], level::info}, // info
|
||||
{level_names[3], level::warn}, // warn
|
||||
{level_names[4], level::err}, // err
|
||||
{level_names[5], level::critical}, // critical
|
||||
{level_names[6], level::off}}; // off
|
||||
|
||||
auto lvl_it = name_to_level.find(name);
|
||||
return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
|
||||
int level = 0;
|
||||
for(const auto &level_str : level_string_views)
|
||||
{
|
||||
if(level_str == name)
|
||||
{
|
||||
return static_cast<level::level_enum>(level);
|
||||
}
|
||||
level++;
|
||||
}
|
||||
return level::off;
|
||||
}
|
||||
|
||||
using level_hasher = std::hash<int>;
|
||||
|
@@ -159,7 +159,7 @@ public:
|
||||
|
||||
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||
{
|
||||
string_view_t level_name{level::to_c_str(msg.level)};
|
||||
string_view_t &level_name = level::to_string_view(msg.level);
|
||||
if (padinfo_.enabled())
|
||||
{
|
||||
scoped_pad p(level_name, padinfo_, dest);
|
||||
@@ -969,7 +969,7 @@ public:
|
||||
// wrap the level name with color
|
||||
msg.color_range_start = dest.size();
|
||||
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
|
||||
fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
|
||||
fmt_helper::append_string_view(level::to_string_view(msg.level), dest);
|
||||
msg.color_range_end = dest.size();
|
||||
dest.push_back(']');
|
||||
dest.push_back(' ');
|
||||
|
Reference in New Issue
Block a user