mirror of https://github.com/gabime/spdlog.git
astyle
parent
20cb73e9da
commit
b13735dc22
@ -1,143 +1,143 @@
|
|||||||
//
|
//
|
||||||
// Copyright(c) 2015 Gabi Melman.
|
// Copyright(c) 2015 Gabi Melman.
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
//
|
//
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include<functional>
|
#include<functional>
|
||||||
|
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <spdlog/details/null_mutex.h>
|
#include <spdlog/details/null_mutex.h>
|
||||||
|
|
||||||
//visual studio upto 2013 does not support noexcept nor constexpr
|
//visual studio upto 2013 does not support noexcept nor constexpr
|
||||||
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
||||||
#define SPDLOG_NOEXCEPT throw()
|
#define SPDLOG_NOEXCEPT throw()
|
||||||
#define SPDLOG_CONSTEXPR
|
#define SPDLOG_CONSTEXPR
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_NOEXCEPT noexcept
|
#define SPDLOG_NOEXCEPT noexcept
|
||||||
#define SPDLOG_CONSTEXPR constexpr
|
#define SPDLOG_CONSTEXPR constexpr
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(__clang__)
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
#define DEPRECATED __attribute__((deprecated))
|
#define DEPRECATED __attribute__((deprecated))
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
#define DEPRECATED __declspec(deprecated)
|
#define DEPRECATED __declspec(deprecated)
|
||||||
#else
|
#else
|
||||||
#define DEPRECATED
|
#define DEPRECATED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#include <spdlog/fmt/fmt.h>
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
|
|
||||||
class formatter;
|
class formatter;
|
||||||
|
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
class sink;
|
class sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
using log_clock = std::chrono::system_clock;
|
using log_clock = std::chrono::system_clock;
|
||||||
using sink_ptr = std::shared_ptr < sinks::sink >;
|
using sink_ptr = std::shared_ptr < sinks::sink >;
|
||||||
using sinks_init_list = std::initializer_list < sink_ptr >;
|
using sinks_init_list = std::initializer_list < sink_ptr >;
|
||||||
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
|
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
|
||||||
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
||||||
using level_t = details::null_atomic_int;
|
using level_t = details::null_atomic_int;
|
||||||
#else
|
#else
|
||||||
using level_t = std::atomic_int;
|
using level_t = std::atomic_int;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using log_err_handler = std::function<void(const std::string &err_msg)>;
|
using log_err_handler = std::function<void(const std::string &err_msg)>;
|
||||||
|
|
||||||
//Log level enum
|
//Log level enum
|
||||||
namespace level
|
namespace level
|
||||||
{
|
{
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
trace = 0,
|
trace = 0,
|
||||||
debug = 1,
|
debug = 1,
|
||||||
info = 2,
|
info = 2,
|
||||||
warn = 3,
|
warn = 3,
|
||||||
err = 4,
|
err = 4,
|
||||||
critical = 5,
|
critical = 5,
|
||||||
off = 6
|
off = 6
|
||||||
} level_enum;
|
} level_enum;
|
||||||
|
|
||||||
static const char* level_names[]{ "trace", "debug", "info", "warning", "error", "critical", "off" };
|
static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "off" };
|
||||||
|
|
||||||
static const char* short_level_names[]{ "T", "D", "I", "W", "E", "C", "O" };
|
static const char* short_level_names[] { "T", "D", "I", "W", "E", "C", "O" };
|
||||||
|
|
||||||
inline const char* to_str(spdlog::level::level_enum l)
|
inline const char* to_str(spdlog::level::level_enum l)
|
||||||
{
|
{
|
||||||
return level_names[l];
|
return level_names[l];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const char* to_short_str(spdlog::level::level_enum l)
|
inline const char* to_short_str(spdlog::level::level_enum l)
|
||||||
{
|
{
|
||||||
return short_level_names[l];
|
return short_level_names[l];
|
||||||
}
|
}
|
||||||
} //level
|
} //level
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Async overflow policy - block by default.
|
// Async overflow policy - block by default.
|
||||||
//
|
//
|
||||||
enum class async_overflow_policy
|
enum class async_overflow_policy
|
||||||
{
|
{
|
||||||
block_retry, // Block / yield / sleep until message can be enqueued
|
block_retry, // Block / yield / sleep until message can be enqueued
|
||||||
discard_log_msg // Discard the message it enqueue fails
|
discard_log_msg // Discard the message it enqueue fails
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Log exception
|
// Log exception
|
||||||
//
|
//
|
||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
namespace os
|
namespace os
|
||||||
{
|
{
|
||||||
std::string errno_str(int err_num);
|
std::string errno_str(int err_num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class spdlog_ex: public std::exception
|
class spdlog_ex: public std::exception
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
spdlog_ex(const std::string& msg):_msg(msg)
|
spdlog_ex(const std::string& msg):_msg(msg)
|
||||||
{}
|
{}
|
||||||
spdlog_ex(const std::string& msg, int last_errno)
|
spdlog_ex(const std::string& msg, int last_errno)
|
||||||
{
|
{
|
||||||
_msg = msg + ": " + details::os::errno_str(last_errno);
|
_msg = msg + ": " + details::os::errno_str(last_errno);
|
||||||
}
|
}
|
||||||
const char* what() const SPDLOG_NOEXCEPT override
|
const char* what() const SPDLOG_NOEXCEPT override
|
||||||
{
|
{
|
||||||
return _msg.c_str();
|
return _msg.c_str();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::string _msg;
|
std::string _msg;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
||||||
//
|
//
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
using filename_t = std::wstring;
|
using filename_t = std::wstring;
|
||||||
#else
|
#else
|
||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
} //spdlog
|
} //spdlog
|
||||||
|
@ -1,81 +1,81 @@
|
|||||||
//
|
//
|
||||||
// Copyright(c) 2015 Gabi Melman.
|
// Copyright(c) 2015 Gabi Melman.
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
//
|
//
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
#ifdef SPDLOG_ENABLE_SYSLOG
|
#ifdef SPDLOG_ENABLE_SYSLOG
|
||||||
|
|
||||||
#include <spdlog/sinks/sink.h>
|
#include <spdlog/sinks/sink.h>
|
||||||
#include <spdlog/details/log_msg.h>
|
#include <spdlog/details/log_msg.h>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Sink that write to syslog using the `syscall()` library call.
|
* Sink that write to syslog using the `syscall()` library call.
|
||||||
*
|
*
|
||||||
* Locking is not needed, as `syslog()` itself is thread-safe.
|
* Locking is not needed, as `syslog()` itself is thread-safe.
|
||||||
*/
|
*/
|
||||||
class syslog_sink : public sink
|
class syslog_sink : public sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//
|
//
|
||||||
syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
|
syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER):
|
||||||
_ident(ident)
|
_ident(ident)
|
||||||
{
|
{
|
||||||
_priorities[static_cast<int>(level::trace)] = LOG_DEBUG;
|
_priorities[static_cast<int>(level::trace)] = LOG_DEBUG;
|
||||||
_priorities[static_cast<int>(level::debug)] = LOG_DEBUG;
|
_priorities[static_cast<int>(level::debug)] = LOG_DEBUG;
|
||||||
_priorities[static_cast<int>(level::info)] = LOG_INFO;
|
_priorities[static_cast<int>(level::info)] = LOG_INFO;
|
||||||
_priorities[static_cast<int>(level::warn)] = LOG_WARNING;
|
_priorities[static_cast<int>(level::warn)] = LOG_WARNING;
|
||||||
_priorities[static_cast<int>(level::err)] = LOG_ERR;
|
_priorities[static_cast<int>(level::err)] = LOG_ERR;
|
||||||
_priorities[static_cast<int>(level::critical)] = LOG_CRIT;
|
_priorities[static_cast<int>(level::critical)] = LOG_CRIT;
|
||||||
_priorities[static_cast<int>(level::off)] = LOG_INFO;
|
_priorities[static_cast<int>(level::off)] = LOG_INFO;
|
||||||
|
|
||||||
//set ident to be program name if empty
|
//set ident to be program name if empty
|
||||||
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
|
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
|
||||||
}
|
}
|
||||||
~syslog_sink()
|
~syslog_sink()
|
||||||
{
|
{
|
||||||
::closelog();
|
::closelog();
|
||||||
}
|
}
|
||||||
|
|
||||||
syslog_sink(const syslog_sink&) = delete;
|
syslog_sink(const syslog_sink&) = delete;
|
||||||
syslog_sink& operator=(const syslog_sink&) = delete;
|
syslog_sink& operator=(const syslog_sink&) = delete;
|
||||||
|
|
||||||
void log(const details::log_msg &msg) override
|
void log(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str());
|
::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush() override
|
void flush() override
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<int, 7> _priorities;
|
std::array<int, 7> _priorities;
|
||||||
//must store the ident because the man says openlog might use the pointer as is and not a string copy
|
//must store the ident because the man says openlog might use the pointer as is and not a string copy
|
||||||
const std::string _ident;
|
const std::string _ident;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Simply maps spdlog's log level to syslog priority level.
|
// Simply maps spdlog's log level to syslog priority level.
|
||||||
//
|
//
|
||||||
int syslog_prio_from_level(const details::log_msg &msg) const
|
int syslog_prio_from_level(const details::log_msg &msg) const
|
||||||
{
|
{
|
||||||
return _priorities[static_cast<int>(msg.level)];
|
return _priorities[static_cast<int>(msg.level)];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue