spdlog/include/spdlog/common.h

99 lines
1.8 KiB
C
Raw Normal View History

2015-11-29 00:24:20 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
2014-11-01 09:20:54 +08:00
#pragma once
2015-01-15 17:46:51 +08:00
#include <string>
#include <initializer_list>
#include <chrono>
#include <memory>
#include <exception>
2014-12-20 22:24:16 +08:00
//visual studio does not support noexcept yet
#ifndef _MSC_VER
#define SPDLOG_NOEXCEPT noexcept
#else
#define SPDLOG_NOEXCEPT throw()
2014-12-20 22:24:16 +08:00
#endif
2014-10-31 07:13:27 +08:00
namespace spdlog
{
class formatter;
2014-12-21 08:42:37 +08:00
namespace sinks
{
2014-12-20 22:24:16 +08:00
class sink;
}
// Common types across the lib
using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr < sinks::sink >;
using sinks_init_list = std::initializer_list < sink_ptr >;
2014-10-31 07:13:27 +08:00
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
//Log level enum
namespace level
{
typedef enum
{
trace = 0,
debug = 1,
info = 2,
notice = 3,
warn = 4,
err = 5,
2014-12-21 07:30:39 +08:00
critical = 6,
alert = 7,
emerg = 8,
off = 9
} level_enum;
2014-12-18 16:07:21 +08:00
static const char* level_names[] { "trace", "debug", "info", "notice", "warning", "error", "critical", "alert", "emerg", "off"};
2015-01-27 22:31:50 +08:00
static const char* short_level_names[] { "T", "D", "I", "N", "W", "E", "C", "A", "M", "O"};
2014-10-31 07:13:27 +08:00
inline const char* to_str(spdlog::level::level_enum l)
{
2014-12-21 08:47:04 +08:00
return level_names[l];
}
2015-01-27 22:31:50 +08:00
inline const char* to_short_str(spdlog::level::level_enum l)
{
return short_level_names[l];
}
} //level
2014-10-26 07:29:50 +08:00
//
2015-01-15 17:01:54 +08:00
// Async overflow policy - block by default.
//
enum class async_overflow_policy
{
block_retry, // Block / yield / sleep until message can be enqueued
discard_log_msg // Discard the message it enqueue fails
};
2014-10-26 07:29:50 +08:00
//
// Log exception
//
class spdlog_ex : public std::exception
2014-10-26 07:29:50 +08:00
{
public:
2014-12-21 08:47:04 +08:00
spdlog_ex(const std::string& msg) :_msg(msg) {}
const char* what() const SPDLOG_NOEXCEPT override
{
return _msg.c_str();
}
2014-10-26 07:29:50 +08:00
private:
2014-12-21 08:47:04 +08:00
std::string _msg;
2014-10-26 07:29:50 +08:00
};
} //spdlog