Commit 7f1f7b62 authored by gabime's avatar gabime

Changed function name to level::from_str

parent d741f1b6
...@@ -9,13 +9,13 @@ ...@@ -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)
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include "details/null_mutex.h" #include "details/null_mutex.h"
//visual studio upto 2013 does not support noexcept nor constexpr // visual studio upto 2013 does not support noexcept nor constexpr
#if defined(_MSC_VER) && (_MSC_VER < 1900) #if defined(_MSC_VER) && (_MSC_VER < 1900)
#define SPDLOG_NOEXCEPT throw() #define SPDLOG_NOEXCEPT throw()
#define SPDLOG_CONSTEXPR #define SPDLOG_CONSTEXPR
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#define SPDLOG_FINAL final #define SPDLOG_FINAL final
#endif #endif
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
#define SPDLOG_DEPRECATED __attribute__((deprecated)) #define SPDLOG_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#define SPDLOG_DEPRECATED __declspec(deprecated) #define SPDLOG_DEPRECATED __declspec(deprecated)
...@@ -51,19 +51,17 @@ ...@@ -51,19 +51,17 @@
#include "fmt/fmt.h" #include "fmt/fmt.h"
namespace spdlog namespace spdlog {
{
class formatter; class formatter;
namespace sinks namespace sinks {
{
class sink; class sink;
} }
using log_clock = std::chrono::system_clock; using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr < sinks::sink >; using sink_ptr = std::shared_ptr<sinks::sink>;
using sinks_init_list = std::initializer_list < sink_ptr >; using sinks_init_list = std::initializer_list<sink_ptr>;
using formatter_ptr = std::shared_ptr<spdlog::formatter>; using formatter_ptr = std::shared_ptr<spdlog::formatter>;
#if defined(SPDLOG_NO_ATOMIC_LEVELS) #if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int; using level_t = details::null_atomic_int;
...@@ -73,9 +71,8 @@ using level_t = std::atomic<int>; ...@@ -73,9 +71,8 @@ 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,54 +84,49 @@ enum level_enum ...@@ -87,54 +84,49 @@ 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;
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_str(spdlog::level::level_enum l) inline const char *to_str(spdlog::level::level_enum l)
{ {
return level_names[l]; return level_names[l];
} }
inline const char* to_short_str(spdlog::level::level_enum l) inline const char *to_short_str(spdlog::level::level_enum l)
{ {
return short_level_names[l]; return short_level_names[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.
// //
enum class async_overflow_policy enum class async_overflow_policy
{ {
block_retry, // Block / yield / sleep until message can be enqueued block_retry, // Block / yield / sleep until message can be enqueued
discard_log_msg // Discard the message it enqueue fails discard_log_msg // Discard the message it enqueue fails
}; };
...@@ -151,25 +143,23 @@ enum class pattern_time_type ...@@ -151,25 +143,23 @@ 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)
{ {
_msg = msg + ": " + details::os::errno_str(last_errno); _msg = msg + ": " + details::os::errno_str(last_errno);
} }
const char* what() const SPDLOG_NOEXCEPT override const char *what() const SPDLOG_NOEXCEPT override
{ {
return _msg.c_str(); return _msg.c_str();
} }
...@@ -187,4 +177,4 @@ using filename_t = std::wstring; ...@@ -187,4 +177,4 @@ using filename_t = std::wstring;
using filename_t = std::string; using filename_t = std::string;
#endif #endif
} //spdlog } // namespace spdlog
#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;
...@@ -17,20 +16,20 @@ std::string log_info(const T& what, spdlog::level::level_enum logger_level = spd ...@@ -17,20 +16,20 @@ std::string log_info(const T& what, spdlog::level::level_enum logger_level = spd
TEST_CASE("basic_logging ", "[basic_logging]") TEST_CASE("basic_logging ", "[basic_logging]")
{ {
//const char // const char
REQUIRE(log_info("Hello") == "Hello"); REQUIRE(log_info("Hello") == "Hello");
REQUIRE(log_info("") == ""); REQUIRE(log_info("") == "");
//std::string // std::string
REQUIRE(log_info(std::string("Hello")) == "Hello"); REQUIRE(log_info(std::string("Hello")) == "Hello");
REQUIRE(log_info(std::string()) == std::string()); REQUIRE(log_info(std::string()) == std::string());
//Numbers // Numbers
REQUIRE(log_info(5) == "5"); REQUIRE(log_info(5) == "5");
REQUIRE(log_info(5.6) == "5.6"); REQUIRE(log_info(5.6) == "5.6");
//User defined class // User defined class
//REQUIRE(log_info(some_logged_class("some_val")) == "some_val"); // REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
} }
TEST_CASE("log_levels", "[log_levels]") TEST_CASE("log_levels", "[log_levels]")
...@@ -66,20 +65,12 @@ TEST_CASE("to_short_str", "[convert_to_short_str]") ...@@ -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);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment