Store level names as string_views
This commit is contained in:
		
							parent
							
								
									2671b48a6c
								
							
						
					
					
						commit
						0ce670e45a
					
				| @ -107,13 +107,13 @@ enum level_enum | |||||||
|         "trace", "debug", "info", "warning", "error", "critical", "off"                                                                    \ |         "trace", "debug", "info", "warning", "error", "critical", "off"                                                                    \ | ||||||
|     } |     } | ||||||
| #endif | #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"}; | 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 | 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 | 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
 |     int level = 0; | ||||||
|         {{level_names[0], level::trace},                               // trace
 |     for(const auto &level_str : level_string_views) | ||||||
|             {level_names[1], level::debug},                            // debug
 |     { | ||||||
|             {level_names[2], level::info},                             // info
 |         if(level_str == name) | ||||||
|             {level_names[3], level::warn},                             // warn
 |         { | ||||||
|             {level_names[4], level::err},                              // err
 |             return static_cast<level::level_enum>(level); | ||||||
|             {level_names[5], level::critical},                         // critical
 |         } | ||||||
|             {level_names[6], level::off}};                             // off
 |         level++; | ||||||
| 
 |     } | ||||||
|     auto lvl_it = name_to_level.find(name); |     return level::off; | ||||||
|     return lvl_it != name_to_level.end() ? lvl_it->second : level::off; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| using level_hasher = std::hash<int>; | 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 |     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()) |         if (padinfo_.enabled()) | ||||||
|         { |         { | ||||||
|             scoped_pad p(level_name, padinfo_, dest); |             scoped_pad p(level_name, padinfo_, dest); | ||||||
| @ -969,7 +969,7 @@ public: | |||||||
|         // wrap the level name with color
 |         // wrap the level name with color
 | ||||||
|         msg.color_range_start = dest.size(); |         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_c_str(msg.level), dest); |         fmt_helper::append_string_view(level::to_string_view(msg.level), dest); | ||||||
|         msg.color_range_end = dest.size(); |         msg.color_range_end = dest.size(); | ||||||
|         dest.push_back(']'); |         dest.push_back(']'); | ||||||
|         dest.push_back(' '); |         dest.push_back(' '); | ||||||
|  | |||||||
| @ -43,15 +43,15 @@ TEST_CASE("log_levels", "[log_levels]") | |||||||
|     REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello"); |     REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| TEST_CASE("to_c_str", "[convert_to_c_str]") | TEST_CASE("level_to_string_view", "[convert_to_string_view") | ||||||
| { | { | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::trace)) == "trace"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::trace) == "trace"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::debug)) == "debug"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::debug) == "debug"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::info)) == "info"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::info) == "info"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::warn)) == "warning"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::warn) == "warning"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::err)) == "error"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::err) == "error"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::critical)) == "critical"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::critical) == "critical"); | ||||||
|     REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::off)) == "off"); |     REQUIRE(spdlog::level::to_string_view(spdlog::level::off) == "off"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| TEST_CASE("to_short_c_str", "[convert_to_short_c_str]") | TEST_CASE("to_short_c_str", "[convert_to_short_c_str]") | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user