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 <string>
#include <initializer_list>
#include <chrono>
#include <memory>
#include <atomic>
#include <chrono>
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <unordered_map>
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
@ -25,7 +25,7 @@
#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)
#define SPDLOG_NOEXCEPT throw()
#define SPDLOG_CONSTEXPR
@ -41,7 +41,7 @@
#define SPDLOG_FINAL final
#endif
#if defined(__GNUC__) || defined(__clang__)
#if defined(__GNUC__) || defined(__clang__)
#define SPDLOG_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define SPDLOG_DEPRECATED __declspec(deprecated)
@ -51,19 +51,17 @@
#include "fmt/fmt.h"
namespace spdlog
{
namespace spdlog {
class formatter;
namespace sinks
{
namespace sinks {
class sink;
}
using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr < sinks::sink >;
using sinks_init_list = std::initializer_list < sink_ptr >;
using sink_ptr = std::shared_ptr<sinks::sink>;
using sinks_init_list = std::initializer_list<sink_ptr>;
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
@ -73,9 +71,8 @@ using level_t = std::atomic<int>;
using log_err_handler = std::function<void(const std::string &err_msg)>;
//Log level enum
namespace level
{
// Log level enum
namespace level {
enum level_enum
{
trace = 0,
@ -87,54 +84,49 @@ enum level_enum
off = 6
};
#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
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];
}
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];
}
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 = {
{ level_names[0], level::trace },
{ level_names[1], level::debug },
{ level_names[2], level::info },
{ level_names[3], level::warn },
{ level_names[4], level::err },
{ level_names[5], level::critical },
{ level_names[6], level::off }
};
auto ci = name_to_level.find(name);
if (ci != name_to_level.end())
{
return ci->second;
}
else
{
return level::off;
}
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
}
using level_hasher = std::hash<int>;
} //level
} // namespace level
//
// Async overflow policy - block by default.
//
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
};
@ -151,25 +143,23 @@ enum class pattern_time_type
//
// Log exception
//
namespace details
{
namespace os
{
namespace details { namespace os {
std::string errno_str(int err_num);
}
}
}} // namespace details::os
class spdlog_ex : public std::exception
{
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);
}
const char* what() const SPDLOG_NOEXCEPT override
const char *what() const SPDLOG_NOEXCEPT override
{
return _msg.c_str();
}
@ -187,4 +177,4 @@ using filename_t = std::wstring;
using filename_t = std::string;
#endif
} //spdlog
} // namespace spdlog

View File

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