Changed function name to level::from_str

This commit is contained in:
gabime 2018-03-09 15:24:37 +02:00
parent d741f1b654
commit 7f1f7b6232
2 changed files with 57 additions and 76 deletions

View File

@ -9,13 +9,13 @@
#include "tweakme.h" #include "tweakme.h"
#include <string>
#include <initializer_list>
#include <chrono>
#include <memory>
#include <atomic> #include <atomic>
#include <chrono>
#include <exception> #include <exception>
#include <functional> #include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <unordered_map> #include <unordered_map>
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
@ -51,13 +51,11 @@
#include "fmt/fmt.h" #include "fmt/fmt.h"
namespace spdlog namespace spdlog {
{
class formatter; class formatter;
namespace sinks namespace sinks {
{
class sink; class sink;
} }
@ -74,8 +72,7 @@ using level_t = std::atomic<int>;
using log_err_handler = std::function<void(const std::string &err_msg)>; using log_err_handler = std::function<void(const std::string &err_msg)>;
// Log level enum // Log level enum
namespace level namespace level {
{
enum level_enum enum level_enum
{ {
trace = 0, trace = 0,
@ -87,9 +84,11 @@ enum level_enum
off = 6 off = 6
}; };
#if !defined(SPDLOG_LEVEL_NAMES) #if !defined(SPDLOG_LEVEL_NAMES)
#define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off" } #define SPDLOG_LEVEL_NAMES \
{ \
"trace", "debug", "info", "warning", "error", "critical", "off" \
}
#endif #endif
static const char *level_names[] SPDLOG_LEVEL_NAMES; static const char *level_names[] SPDLOG_LEVEL_NAMES;
@ -106,28 +105,21 @@ inline const char* to_short_str(spdlog::level::level_enum l)
} }
inline spdlog::level::level_enum to_level_enum(const std::string &name) inline spdlog::level::level_enum to_level_enum(const std::string &name)
{ {
static std::unordered_map<std::string, level_enum> name_to_level = { static std::unordered_map<std::string, level_enum> name_to_level = // map string->level
{ level_names[0], level::trace }, {{level_names[0], level::trace}, // trace
{ level_names[1], level::debug }, {level_names[1], level::debug}, // debug
{ level_names[2], level::info }, {level_names[2], level::info}, // info
{ level_names[3], level::warn }, {level_names[3], level::warn}, // warn
{ level_names[4], level::err }, {level_names[4], level::err}, // err
{ level_names[5], level::critical }, {level_names[5], level::critical}, // critical
{ level_names[6], level::off } {level_names[6], level::off}}; // off
};
auto ci = name_to_level.find(name); auto lvl_it = name_to_level.find(name);
if (ci != name_to_level.end()) return lvl_it != name_to_level.end() ? lvl_it->second : level::off
{
return ci->second;
}
else
{
return level::off;
}
} }
using level_hasher = std::hash<int>; using level_hasher = std::hash<int>;
} //level } // namespace level
// //
// Async overflow policy - block by default. // Async overflow policy - block by default.
@ -151,18 +143,16 @@ enum class pattern_time_type
// //
// Log exception // Log exception
// //
namespace details namespace details { namespace os {
{
namespace os
{
std::string errno_str(int err_num); std::string errno_str(int err_num);
} }} // namespace details::os
}
class spdlog_ex : public std::exception class spdlog_ex : public std::exception
{ {
public: public:
explicit spdlog_ex(std::string msg) : _msg(std::move(msg)) explicit spdlog_ex(std::string msg)
{} : _msg(std::move(msg))
{
}
spdlog_ex(const std::string &msg, int last_errno) spdlog_ex(const std::string &msg, int last_errno)
{ {
@ -187,4 +177,4 @@ using filename_t = std::wstring;
using filename_t = std::string; using filename_t = std::string;
#endif #endif
} //spdlog } // namespace spdlog

View File

@ -1,7 +1,6 @@
#include "includes.h" #include "includes.h"
template<class T> template <class T> std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
std::string log_info(const T& what, spdlog::level::level_enum logger_level = spdlog::level::info)
{ {
std::ostringstream oss; std::ostringstream oss;
@ -66,20 +65,12 @@ TEST_CASE("to_short_str", "[convert_to_short_str]")
TEST_CASE("to_level_enum", "[convert_to_level_enum]") TEST_CASE("to_level_enum", "[convert_to_level_enum]")
{ {
REQUIRE(spdlog::level::to_level_enum("trace") == spdlog::level::trace); REQUIRE(spdlog::level::from_str("trace") == spdlog::level::trace);
REQUIRE(spdlog::level::to_level_enum("debug") == spdlog::level::debug); REQUIRE(spdlog::level::from_str("debug") == spdlog::level::debug);
REQUIRE(spdlog::level::to_level_enum("info") == spdlog::level::info); REQUIRE(spdlog::level::from_str("info") == spdlog::level::info);
REQUIRE(spdlog::level::to_level_enum("warning") == spdlog::level::warn); REQUIRE(spdlog::level::from_str("warning") == spdlog::level::warn);
REQUIRE(spdlog::level::to_level_enum("error") == spdlog::level::err); REQUIRE(spdlog::level::from_str("error") == spdlog::level::err);
REQUIRE(spdlog::level::to_level_enum("critical") == spdlog::level::critical); REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
REQUIRE(spdlog::level::to_level_enum("off") == spdlog::level::off); REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
REQUIRE(spdlog::level::to_level_enum("null") == spdlog::level::off); REQUIRE(spdlog::level::from_str("null") == spdlog::level::off);
} }