spdlog/include/spdlog/common.h

254 lines
6.3 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
2018-04-28 18:31:09 -04:00
#include "spdlog/tweakme.h"
2016-08-22 13:54:18 -04:00
#include <atomic>
#include <chrono>
#include <initializer_list>
#include <memory>
2018-06-13 13:19:13 -04:00
#include <stdexcept>
#include <string>
2016-08-22 13:54:18 -04:00
2018-07-10 10:01:49 -04:00
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
2016-08-22 13:54:18 -04:00
#include <codecvt>
#include <locale>
#endif
#include "spdlog/fmt/fmt.h"
2019-04-05 09:44:17 -04:00
#ifdef SPDLOG_HEADER_ONLY
#define SPDLOG_INLINE inline
#else
#define SPDLOG_INLINE
#endif
// 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
#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
// disable thread local on msvc 2013
#ifndef SPDLOG_NO_TLS
2019-01-08 10:35:34 -05:00
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
#define SPDLOG_NO_TLS 1
#endif
#endif
// Get the basename of __FILE__ (at compile time if possible)
#if FMT_HAS_FEATURE(__builtin_strrchr)
#define SPDLOG_STRRCHR(str, sep) __builtin_strrchr(str, sep)
#else
#define SPDLOG_STRRCHR(str, sep) strrchr(str, sep)
#endif //__builtin_strrchr not defined
#ifdef _WIN32
#define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("\\" file, '\\') + 1
#else
#define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("/" file, '/') + 1
#endif
2016-08-22 13:54:18 -04:00
#ifndef SPDLOG_FUNCTION
#define SPDLOG_FUNCTION __FUNCTION__
#endif
2019-04-05 16:05:46 -04:00
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
2019-04-05 18:37:27 -04:00
using filename_t = std::wstring;
2019-04-05 16:05:46 -04:00
#define SPDLOG_FILENAME_T(s) L##s
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
return c.to_bytes(filename);
}
#else
2019-04-05 18:37:27 -04:00
using filename_t = std::string;
2019-04-05 16:05:46 -04:00
#define SPDLOG_FILENAME_T(s) s
#endif
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>;
2018-07-07 17:53:50 -04:00
2018-10-21 05:46:58 -04: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 05:46:58 -04:00
#else
using string_view_t = fmt::string_view;
2018-10-21 05:46:58 -04:00
#endif
2016-08-22 13:54:18 -04:00
#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
#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-24 16:35:09 -05:00
enum level_enum
2016-08-22 13:54:18 -04: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-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
2018-11-24 17:36:14 -05:00
static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
2019-03-23 10:39:32 -04:00
#define SPDLOG_SHORT_LEVEL_NAMES \
{ \
"T", "D", "I", "W", "E", "C", "O" \
}
#endif
static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES;
2016-08-22 13:54:18 -04:00
2018-11-24 17:44:51 -05:00
inline string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
2016-08-22 13:54:18 -04:00
{
2018-11-24 17:36:14 -05:00
return level_string_views[l];
2016-08-22 13:54:18 -04:00
}
2018-10-09 18:01:37 -04:00
inline const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
2016-08-22 13:54:18 -04:00
{
return short_level_names[l];
}
2018-08-15 12:01:44 -04:00
2018-10-09 18:01:37 -04:00
inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
{
2018-11-24 17:36:14 -05:00
int level = 0;
2018-11-24 17:44:51 -05:00
for (const auto &level_str : level_string_views)
2018-11-24 17:36:14 -05:00
{
2018-11-24 17:44:51 -05:00
if (level_str == name)
2018-11-24 17:36:14 -05:00
{
return static_cast<level::level_enum>(level);
}
level++;
}
return 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
//
// 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-08-15 12:01:44 -04:00
class spdlog_ex : public std::exception
2016-08-22 13:54:18 -04:00
{
public:
2018-09-26 17:59:28 -04:00
explicit spdlog_ex(std::string msg)
: msg_(std::move(msg))
2018-06-13 13:19:13 -04:00
{
}
2018-08-15 12:01:44 -04:00
spdlog_ex(const std::string &msg, int last_errno)
2018-06-13 13:19:13 -04:00
{
2018-08-15 12:01:44 -04:00
fmt::memory_buffer outbuf;
fmt::format_system_error(outbuf, last_errno, msg);
msg_ = fmt::to_string(outbuf);
2018-06-13 13:19:13 -04:00
}
2018-08-15 12:01:44 -04:00
const char *what() const SPDLOG_NOEXCEPT override
2016-08-22 13:54:18 -04:00
{
2018-08-15 12:01:44 -04:00
return msg_.c_str();
2016-08-22 13:54:18 -04:00
}
2018-06-13 13:16:31 -04:00
2016-08-22 13:54:18 -04:00
private:
2018-08-15 12:01:44 -04:00
std::string msg_;
2016-08-22 13:54:18 -04:00
};
2018-11-22 11:47:50 -05:00
struct source_loc
{
2018-11-22 11:49:14 -05:00
SPDLOG_CONSTEXPR source_loc()
2018-11-22 11:50:56 -05:00
: filename{""}
, line{0}
, funcname{""}
2018-11-22 11:49:14 -05:00
{
}
SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
: filename{filename_in}
, line{static_cast<uint32_t>(line_in)}
, funcname{funcname_in}
2018-11-22 11:49:14 -05:00
{
}
2018-11-24 17:44:51 -05:00
SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
{
return line == 0;
}
const char *filename;
uint32_t line;
const char *funcname;
2018-11-22 11:47:50 -05:00
};
namespace details {
// make_unique support for pre c++14
2018-09-01 14:30:05 -04: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-26 16:50:47 -04:00
static_assert(!std::is_array<T>::value, "arrays not supported");
2018-09-01 14:30:05 -04:00
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
} // namespace details
} // namespace spdlog