spdlog/include/spdlog/common.h

203 lines
5.3 KiB
C
Raw Normal View History

2016-08-23 01:54:18 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
2018-04-29 06:31:09 +08:00
#include "spdlog/tweakme.h"
2016-08-23 01:54:18 +08:00
#include <atomic>
#include <chrono>
#include <functional>
#include <initializer_list>
#include <memory>
2018-06-14 01:19:13 +08:00
#include <stdexcept>
#include <string>
#include <type_traits>
2018-09-27 04:48:59 +08:00
#include <unordered_map>
2016-08-23 01:54:18 +08:00
2018-07-10 22:01:49 +08:00
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
2016-08-23 01:54:18 +08:00
#include <codecvt>
#include <locale>
#endif
2018-04-29 06:31:09 +08:00
#include "spdlog/details/null_mutex.h"
2016-08-23 01:54:18 +08:00
// visual studio upto 2013 does not support noexcept nor constexpr
2016-08-23 01:54:18 +08: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
#if defined(__GNUC__) || defined(__clang__)
2016-11-14 20:58:10 +08:00
#define SPDLOG_DEPRECATED __attribute__((deprecated))
2016-08-23 01:54:18 +08:00
#elif defined(_MSC_VER)
2016-11-14 20:58:10 +08:00
#define SPDLOG_DEPRECATED __declspec(deprecated)
2016-08-23 01:54:18 +08:00
#else
2016-11-14 20:58:10 +08:00
#define SPDLOG_DEPRECATED
2016-08-23 01:54:18 +08:00
#endif
// disable thread local on msvc 2013
#ifndef SPDLOG_NO_TLS
#if (defined(_MSC_VER) && (_MSC_VER < 1900))
#define SPDLOG_NO_TLS 1
#endif
#endif
2018-06-24 06:32:39 +08:00
#include "spdlog/fmt/fmt.h"
2016-08-23 01:54:18 +08:00
namespace spdlog {
2016-08-23 01:54:18 +08:00
class formatter;
namespace sinks {
2016-08-23 01:54:18 +08: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>;
2018-07-19 20:07:54 +08:00
using log_err_handler = std::function<void(const std::string &err_msg)>;
2018-07-08 05:53:50 +08:00
2018-10-21 17:46:58 +08:00
// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
using string_view_t = std::string_view;
2018-10-21 17:46:58 +08:00
#else
using string_view_t = fmt::string_view;
2018-10-21 17:46:58 +08:00
#endif
2016-08-23 01:54:18 +08:00
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
2016-10-09 06:55:47 +08:00
using level_t = std::atomic<int>;
2016-08-23 01:54:18 +08:00
#endif
#define SPDLOG_LEVEL_TRACE 0
#define SPDLOG_LEVEL_DEBUG 1
#define SPDLOG_LEVEL_INFO 2
#define SPDLOG_LEVEL_WARN 3
#define SPDLOG_LEVEL_ERROR 4
#define SPDLOG_LEVEL_CRITICAL 5
#define SPDLOG_LEVEL_OFF 6
#if !defined(SPDLOG_ACTIVE_LEVEL)
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#endif
// Log level enum
namespace level {
2018-02-25 05:35:09 +08:00
enum level_enum
2016-08-23 01:54:18 +08:00
{
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
warn = SPDLOG_LEVEL_WARN,
err = SPDLOG_LEVEL_ERROR,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
2018-02-25 05:35:09 +08:00
};
2016-08-23 01:54:18 +08: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-23 01:54:18 +08:00
static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
2016-08-23 01:54:18 +08:00
2018-10-10 06:01:37 +08:00
inline const char *to_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
2016-08-23 01:54:18 +08:00
{
return level_names[l];
}
2018-10-10 06:01:37 +08:00
inline const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
2016-08-23 01:54:18 +08:00
{
return short_level_names[l];
}
2018-08-16 00:01:44 +08:00
2018-10-10 06:01:37 +08:00
inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
{
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 21:26:00 +08:00
return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
}
2018-02-05 18:20:57 +08:00
using level_hasher = std::hash<int>;
} // namespace level
2016-08-23 01:54:18 +08:00
//
// Pattern time - specific time getting to use for pattern_formatter.
// local time by default
//
enum class pattern_time_type
{
2017-06-01 08:42:10 +08:00
local, // log localtime
utc // log utc
};
2016-08-23 01:54:18 +08:00
//
// Log exception
//
2018-08-16 00:01:44 +08:00
class spdlog_ex : public std::exception
2016-08-23 01:54:18 +08:00
{
public:
2018-09-27 05:59:28 +08:00
explicit spdlog_ex(std::string msg)
: msg_(std::move(msg))
2018-06-14 01:19:13 +08:00
{
}
2018-08-16 00:01:44 +08:00
spdlog_ex(const std::string &msg, int last_errno)
2018-06-14 01:19:13 +08:00
{
2018-08-16 00:01:44 +08:00
fmt::memory_buffer outbuf;
fmt::format_system_error(outbuf, last_errno, msg);
msg_ = fmt::to_string(outbuf);
2018-06-14 01:19:13 +08:00
}
2018-08-16 00:01:44 +08:00
const char *what() const SPDLOG_NOEXCEPT override
2016-08-23 01:54:18 +08:00
{
2018-08-16 00:01:44 +08:00
return msg_.c_str();
2016-08-23 01:54:18 +08:00
}
2018-06-14 01:16:31 +08:00
2016-08-23 01:54:18 +08:00
private:
2018-08-16 00:01:44 +08:00
std::string msg_;
2016-08-23 01:54:18 +08:00
};
//
// 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 details {
// make_unique support for pre c++14
2018-09-02 02:30:05 +08:00
#if __cplusplus >= 201402L // C++14 and beyond
using std::make_unique;
#else
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args)
{
2018-09-27 04:50:47 +08:00
static_assert(!std::is_array<T>::value, "arrays not supported");
2018-09-02 02:30:05 +08:00
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
} // namespace details
} // namespace spdlog