diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1a8e2fff..490deec7 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -1,143 +1,143 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) -#include -#include -#endif - -#include - -//visual studio upto 2013 does not support noexcept nor constexpr -#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__) -#define DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) -#else -#define DEPRECATED -#endif - - -#include - -namespace spdlog -{ - - class formatter; - - namespace sinks - { - 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 >; - using formatter_ptr = std::shared_ptr; -#if defined(SPDLOG_NO_ATOMIC_LEVELS) - using level_t = details::null_atomic_int; -#else - using level_t = std::atomic_int; -#endif - - using log_err_handler = std::function; - - //Log level enum - namespace level - { - typedef enum - { - trace = 0, - debug = 1, - info = 2, - warn = 3, - err = 4, - critical = 5, - off = 6 - } level_enum; - - 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" }; - - inline const char* to_str(spdlog::level::level_enum l) - { - return level_names[l]; - } - - inline const char* to_short_str(spdlog::level::level_enum l) - { - return short_level_names[l]; - } - } //level - - - // - // Async overflow policy - block by default. - // - enum class async_overflow_policy - { - block_retry, // Block / yield / sleep until message can be enqueued - discard_log_msg // Discard the message it enqueue fails - }; - - - // - // Log exception - // - namespace details - { - namespace os - { - std::string errno_str(int err_num); - } - } - class spdlog_ex: public std::exception - { - public: - spdlog_ex(const std::string& msg):_msg(msg) - {} - spdlog_ex(const std::string& msg, int last_errno) - { - _msg = msg + ": " + details::os::errno_str(last_errno); - } - const char* what() const SPDLOG_NOEXCEPT override - { - return _msg.c_str(); - } - 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 - - -} //spdlog +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) +#include +#include +#endif + +#include + +//visual studio upto 2013 does not support noexcept nor constexpr +#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__) +#define DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED __declspec(deprecated) +#else +#define DEPRECATED +#endif + + +#include + +namespace spdlog +{ + +class formatter; + +namespace sinks +{ +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 >; +using formatter_ptr = std::shared_ptr; +#if defined(SPDLOG_NO_ATOMIC_LEVELS) +using level_t = details::null_atomic_int; +#else +using level_t = std::atomic_int; +#endif + +using log_err_handler = std::function; + +//Log level enum +namespace level +{ +typedef enum +{ + trace = 0, + debug = 1, + info = 2, + warn = 3, + err = 4, + critical = 5, + off = 6 +} level_enum; + +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" }; + +inline const char* to_str(spdlog::level::level_enum l) +{ + return level_names[l]; +} + +inline const char* to_short_str(spdlog::level::level_enum l) +{ + return short_level_names[l]; +} +} //level + + +// +// Async overflow policy - block by default. +// +enum class async_overflow_policy +{ + block_retry, // Block / yield / sleep until message can be enqueued + discard_log_msg // Discard the message it enqueue fails +}; + + +// +// Log exception +// +namespace details +{ +namespace os +{ +std::string errno_str(int err_num); +} +} +class spdlog_ex: public std::exception +{ +public: + spdlog_ex(const std::string& msg):_msg(msg) + {} + spdlog_ex(const std::string& msg, int last_errno) + { + _msg = msg + ": " + details::os::errno_str(last_errno); + } + const char* what() const SPDLOG_NOEXCEPT override + { + return _msg.c_str(); + } +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 + + +} //spdlog diff --git a/include/spdlog/details/mpmc_bounded_q.h b/include/spdlog/details/mpmc_bounded_q.h index 034c3e2b..3a46e8eb 100644 --- a/include/spdlog/details/mpmc_bounded_q.h +++ b/include/spdlog/details/mpmc_bounded_q.h @@ -60,9 +60,9 @@ public: using item_type = T; mpmc_bounded_queue(size_t buffer_size) - :max_size_(buffer_size), - buffer_(new cell_t [buffer_size]), - buffer_mask_(buffer_size - 1) + :max_size_(buffer_size), + buffer_(new cell_t [buffer_size]), + buffer_mask_(buffer_size - 1) { //queue size must be power of two if(!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0))) @@ -133,15 +133,15 @@ public: return true; } - size_t approx_size() - { - size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed); - size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed); - if (last_pos <= first_pos) - return 0; - auto size = last_pos - first_pos; - return size < max_size_ ? size : max_size_; - } + size_t approx_size() + { + size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed); + size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed); + if (last_pos <= first_pos) + return 0; + auto size = last_pos - first_pos; + return size < max_size_ ? size : max_size_; + } private: struct cell_t @@ -150,7 +150,7 @@ private: T data_; }; - size_t const max_size_; + size_t const max_size_; static size_t const cacheline_size = 64; typedef char cacheline_pad_t [cacheline_size]; @@ -165,7 +165,7 @@ private: cacheline_pad_t pad3_; mpmc_bounded_queue(mpmc_bounded_queue const&) = delete; - void operator= (mpmc_bounded_queue const&) = delete; + void operator= (mpmc_bounded_queue const&) = delete; }; } // ns details diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 2ae6078a..eb95e44a 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -250,26 +250,28 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime()) offset -= tzinfo.StandardBias; return offset; #else - + #if defined(sun) || defined(__sun) // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris - struct helper { - static long int calculate_gmt_offset(const std::tm & localtm = details::os::localtime(), const std::tm & gmtm = details::os::gmtime()) { + struct helper + { + static long int calculate_gmt_offset(const std::tm & localtm = details::os::localtime(), const std::tm & gmtm = details::os::gmtime()) + { int local_year = localtm.tm_year + (1900 - 1); int gmt_year = gmtm.tm_year + (1900 - 1); long int days = ( - // difference in day of year - localtm.tm_yday - gmtm.tm_yday + // difference in day of year + localtm.tm_yday - gmtm.tm_yday - // + intervening leap days - + ((local_year >> 2) - (gmt_year >> 2)) - - (local_year / 100 - gmt_year / 100) - + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) + // + intervening leap days + + ((local_year >> 2) - (gmt_year >> 2)) + - (local_year / 100 - gmt_year / 100) + + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) - // + difference in years * 365 */ - + (long int)(local_year - gmt_year) * 365 - ); + // + difference in years * 365 */ + + (long int)(local_year - gmt_year) * 365 + ); long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 1a383f78..3498a742 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -24,7 +24,7 @@ namespace sinks template class dist_sink: public base_sink { -public: +public: explicit dist_sink() :_sinks() {} dist_sink(const dist_sink&) = delete; dist_sink& operator=(const dist_sink&) = delete; diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 4c5b6876..0d8633c5 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -1,81 +1,81 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -#include - -#ifdef SPDLOG_ENABLE_SYSLOG - -#include -#include - -#include -#include -#include - - -namespace spdlog -{ -namespace sinks -{ -/** - * Sink that write to syslog using the `syscall()` library call. - * - * Locking is not needed, as `syslog()` itself is thread-safe. - */ -class syslog_sink : public sink -{ -public: - // - syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER): - _ident(ident) - { - _priorities[static_cast(level::trace)] = LOG_DEBUG; - _priorities[static_cast(level::debug)] = LOG_DEBUG; - _priorities[static_cast(level::info)] = LOG_INFO; - _priorities[static_cast(level::warn)] = LOG_WARNING; - _priorities[static_cast(level::err)] = LOG_ERR; - _priorities[static_cast(level::critical)] = LOG_CRIT; - _priorities[static_cast(level::off)] = LOG_INFO; - - //set ident to be program name if empty - ::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility); - } - ~syslog_sink() - { - ::closelog(); - } - - syslog_sink(const syslog_sink&) = delete; - syslog_sink& operator=(const syslog_sink&) = delete; - - void log(const details::log_msg &msg) override - { - ::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str()); - } - - void flush() override - { - } - - -private: - std::array _priorities; - //must store the ident because the man says openlog might use the pointer as is and not a string copy - const std::string _ident; - - // - // Simply maps spdlog's log level to syslog priority level. - // - int syslog_prio_from_level(const details::log_msg &msg) const - { - return _priorities[static_cast(msg.level)]; - } -}; -} -} - -#endif +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +#include + +#ifdef SPDLOG_ENABLE_SYSLOG + +#include +#include + +#include +#include +#include + + +namespace spdlog +{ +namespace sinks +{ +/** + * Sink that write to syslog using the `syscall()` library call. + * + * Locking is not needed, as `syslog()` itself is thread-safe. + */ +class syslog_sink : public sink +{ +public: + // + syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER): + _ident(ident) + { + _priorities[static_cast(level::trace)] = LOG_DEBUG; + _priorities[static_cast(level::debug)] = LOG_DEBUG; + _priorities[static_cast(level::info)] = LOG_INFO; + _priorities[static_cast(level::warn)] = LOG_WARNING; + _priorities[static_cast(level::err)] = LOG_ERR; + _priorities[static_cast(level::critical)] = LOG_CRIT; + _priorities[static_cast(level::off)] = LOG_INFO; + + //set ident to be program name if empty + ::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility); + } + ~syslog_sink() + { + ::closelog(); + } + + syslog_sink(const syslog_sink&) = delete; + syslog_sink& operator=(const syslog_sink&) = delete; + + void log(const details::log_msg &msg) override + { + ::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str()); + } + + void flush() override + { + } + + +private: + std::array _priorities; + //must store the ident because the man says openlog might use the pointer as is and not a string copy + const std::string _ident; + + // + // Simply maps spdlog's log level to syslog priority level. + // + int syslog_prio_from_level(const details::log_msg &msg) const + { + return _priorities[static_cast(msg.level)]; + } +}; +} +} + +#endif