This commit is contained in:
gabime 2016-08-22 20:54:18 +03:00
parent 20cb73e9da
commit b13735dc22
5 changed files with 253 additions and 251 deletions

View File

@ -43,100 +43,100 @@
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

View File

@ -60,9 +60,9 @@ public:
using item_type = T; using item_type = T;
mpmc_bounded_queue(size_t buffer_size) mpmc_bounded_queue(size_t buffer_size)
:max_size_(buffer_size), :max_size_(buffer_size),
buffer_(new cell_t [buffer_size]), buffer_(new cell_t [buffer_size]),
buffer_mask_(buffer_size - 1) buffer_mask_(buffer_size - 1)
{ {
//queue size must be power of two //queue size must be power of two
if(!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0))) if(!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0)))
@ -133,15 +133,15 @@ public:
return true; return true;
} }
size_t approx_size() size_t approx_size()
{ {
size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed); size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed);
size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed); size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed);
if (last_pos <= first_pos) if (last_pos <= first_pos)
return 0; return 0;
auto size = last_pos - first_pos; auto size = last_pos - first_pos;
return size < max_size_ ? size : max_size_; return size < max_size_ ? size : max_size_;
} }
private: private:
struct cell_t struct cell_t
@ -150,7 +150,7 @@ private:
T data_; T data_;
}; };
size_t const max_size_; size_t const max_size_;
static size_t const cacheline_size = 64; static size_t const cacheline_size = 64;
typedef char cacheline_pad_t [cacheline_size]; typedef char cacheline_pad_t [cacheline_size];

View File

@ -253,23 +253,25 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime())
#if defined(sun) || defined(__sun) #if defined(sun) || defined(__sun)
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
struct helper { struct helper
static long int calculate_gmt_offset(const std::tm & localtm = details::os::localtime(), const std::tm & gmtm = details::os::gmtime()) { {
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 local_year = localtm.tm_year + (1900 - 1);
int gmt_year = gmtm.tm_year + (1900 - 1); int gmt_year = gmtm.tm_year + (1900 - 1);
long int days = ( long int days = (
// difference in day of year // difference in day of year
localtm.tm_yday - gmtm.tm_yday localtm.tm_yday - gmtm.tm_yday
// + intervening leap days // + intervening leap days
+ ((local_year >> 2) - (gmt_year >> 2)) + ((local_year >> 2) - (gmt_year >> 2))
- (local_year / 100 - gmt_year / 100) - (local_year / 100 - gmt_year / 100)
+ ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
// + difference in years * 365 */ // + difference in years * 365 */
+ (long int)(local_year - gmt_year) * 365 + (long int)(local_year - gmt_year) * 365
); );
long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);