spdlog/include/spdlog/common.h

183 lines
4.5 KiB
C
Raw Normal View History

2016-08-22 13:54:18 -04:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#define SPDLOG_VERSION "0.16.4-rc"
2018-02-23 07:39:41 -05:00
#include "tweakme.h"
2016-08-22 13:54:18 -04:00
#include <atomic>
#include <chrono>
2016-08-22 13:54:18 -04:00
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <unordered_map>
2016-08-22 13:54:18 -04:00
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
#include <codecvt>
#include <locale>
#endif
#include "details/null_mutex.h"
2016-08-22 13:54:18 -04:00
// visual studio upto 2013 does not support noexcept nor constexpr
2016-08-22 13:54:18 -04:00
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define SPDLOG_NOEXCEPT throw()
#define SPDLOG_CONSTEXPR
#else
#define SPDLOG_NOEXCEPT noexcept
#define SPDLOG_CONSTEXPR constexpr
#endif
// final keyword support. On by default. See tweakme.h
#if defined(SPDLOG_NO_FINAL)
#define SPDLOG_FINAL
#else
#define SPDLOG_FINAL final
#endif
#if defined(__GNUC__) || defined(__clang__)
2016-11-14 07:58:10 -05:00
#define SPDLOG_DEPRECATED __attribute__((deprecated))
2016-08-22 13:54:18 -04:00
#elif defined(_MSC_VER)
2016-11-14 07:58:10 -05:00
#define SPDLOG_DEPRECATED __declspec(deprecated)
2016-08-22 13:54:18 -04:00
#else
2016-11-14 07:58:10 -05:00
#define SPDLOG_DEPRECATED
2016-08-22 13:54:18 -04:00
#endif
#include "fmt/fmt.h"
2016-08-22 13:54:18 -04:00
namespace spdlog {
2016-08-22 13:54:18 -04:00
class formatter;
namespace sinks {
2016-08-22 13:54:18 -04:00
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>;
2016-08-22 13:54:18 -04:00
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
2016-10-08 18:55:47 -04:00
using level_t = std::atomic<int>;
2016-08-22 13:54:18 -04:00
#endif
using log_err_handler = std::function<void(const std::string &err_msg)>;
// Log level enum
namespace level {
2018-02-24 16:35:09 -05:00
enum level_enum
2016-08-22 13:54:18 -04:00
{
trace = 0,
debug = 1,
info = 2,
warn = 3,
err = 4,
critical = 5,
off = 6
2018-02-24 16:35:09 -05:00
};
2016-08-22 13:54:18 -04:00
#if !defined(SPDLOG_LEVEL_NAMES)
#define SPDLOG_LEVEL_NAMES \
{ \
"trace", "debug", "info", "warning", "error", "critical", "off" \
}
#endif
static const char *level_names[] SPDLOG_LEVEL_NAMES;
2016-08-22 13:54:18 -04:00
static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
2016-08-22 13:54:18 -04:00
inline const char *to_str(spdlog::level::level_enum l)
2016-08-22 13:54:18 -04:00
{
return level_names[l];
}
inline const char *to_short_str(spdlog::level::level_enum l)
2016-08-22 13:54:18 -04:00
{
return short_level_names[l];
}
inline spdlog::level::level_enum from_str(const std::string &name)
{
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);
2018-03-09 08:26:00 -05:00
return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
}
2018-02-05 05:20:57 -05:00
using level_hasher = std::hash<int>;
} // namespace level
2016-08-22 13:54:18 -04:00
//
// Async overflow policy - block by default.
//
enum class async_overflow_policy
{
block_retry, // Block / yield / sleep until message can be enqueued
2016-08-22 13:54:18 -04:00
discard_log_msg // Discard the message it enqueue fails
};
//
// Pattern time - specific time getting to use for pattern_formatter.
// local time by default
//
enum class pattern_time_type
{
2017-05-31 20:42:10 -04:00
local, // log localtime
utc // log utc
};
2016-08-22 13:54:18 -04:00
//
// Log exception
//
2018-03-17 06:47:46 -04:00
namespace details {
namespace os {
2016-08-22 13:54:18 -04:00
std::string errno_str(int err_num);
2018-03-17 06:47:46 -04:00
}
} // namespace details
2018-02-24 17:56:56 -05:00
class spdlog_ex : public std::exception
2016-08-22 13:54:18 -04:00
{
public:
explicit spdlog_ex(std::string msg)
: _msg(std::move(msg))
{
}
2018-02-24 19:40:46 -05:00
spdlog_ex(const std::string &msg, int last_errno)
2016-08-22 13:54:18 -04:00
{
_msg = msg + ": " + details::os::errno_str(last_errno);
}
2018-02-24 19:40:46 -05:00
const char *what() const SPDLOG_NOEXCEPT override
2016-08-22 13:54:18 -04:00
{
return _msg.c_str();
}
2018-02-24 17:56:56 -05:00
2016-08-22 13:54:18 -04:00
private:
std::string _msg;
};
//
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
//
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
using filename_t = std::wstring;
#else
using filename_t = std::string;
#endif
} // namespace spdlog