You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/include/spdlog/common.h

191 lines
5.8 KiB
C

9 years ago
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#define SPDLOG_VERSION "0.16.4-rc"
#include "spdlog/tweakme.h"
9 years ago
#include <atomic>
#include <chrono>
7 years ago
#include <stdexcept>
#include <functional>
7 years ago
#include <algorithm>
#include <initializer_list>
#include <memory>
#include <string>
#include <unordered_map>
9 years ago
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
#include <codecvt>
#include <locale>
#endif
#include "spdlog/details/null_mutex.h"
9 years ago
// visual studio upto 2013 does not support noexcept nor constexpr
9 years ago
#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__)
9 years ago
#define SPDLOG_DEPRECATED __attribute__((deprecated))
9 years ago
#elif defined(_MSC_VER)
9 years ago
#define SPDLOG_DEPRECATED __declspec(deprecated)
9 years ago
#else
9 years ago
#define SPDLOG_DEPRECATED
9 years ago
#endif
#include "fmt/fmt.h"
9 years ago
namespace spdlog {
9 years ago
class formatter;
namespace sinks {
9 years ago
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>;
9 years ago
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
9 years ago
using level_t = std::atomic<int>;
9 years ago
#endif
using log_err_handler = std::function<void(const std::string &err_msg)>;
// Log level enum
namespace level {
enum level_enum
9 years ago
{
trace = 0,
debug = 1,
info = 2,
warn = 3,
err = 4,
critical = 5,
off = 6
};
9 years ago
#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;
9 years ago
static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
9 years ago
inline const char *to_str(spdlog::level::level_enum l)
9 years ago
{
return level_names[l];
}
inline const char *to_short_str(spdlog::level::level_enum l)
9 years ago
{
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);
8 years ago
return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
}
8 years ago
using level_hasher = std::hash<int>;
} // namespace level
9 years ago
//
// Async overflow policy - block by default.
//
enum class async_overflow_policy
{
block_retry, // Block / yield / sleep until message can be enqueued
9 years ago
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
{
8 years ago
local, // log localtime
utc // log utc
};
9 years ago
//
// Log exception
//
7 years ago
class spdlog_ex : public std::runtime_error
9 years ago
{
public:
7 years ago
explicit spdlog_ex(const std::string& msg) : runtime_error(msg){}
spdlog_ex(const std::string& msg, int last_errno) : runtime_error(msg), last_errno_(last_errno){}
const char *what() const SPDLOG_NOEXCEPT override
9 years ago
{
7 years ago
if(last_errno_)
{
fmt::memory_buffer buf;
std::string msg(runtime_error::what());
fmt::format_system_error(buf, last_errno_, msg);
return fmt::to_string(buf).c_str();
}
else
{
return runtime_error::what();
}
9 years ago
}
7 years ago
9 years ago
private:
7 years ago
int last_errno_{0};
9 years ago
};
//
// 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
7 years ago
#define SPDLOG_CATCH_AND_HANDLE \
catch (const std::exception &ex) \
{ \
err_handler_(ex.what()); \
7 years ago
} \
catch (...) \
{ \
err_handler_("Unknown exeption in logger"); \
7 years ago
}
} // namespace spdlog