From 709948ff4ab979113ba5a2de885ed68a79e3ee53 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 12 Oct 2017 19:48:04 +0300 Subject: [PATCH] Fixed issue #527 --- include/spdlog/details/async_logger_impl.h | 2 +- include/spdlog/details/logger_impl.h | 7 ++++++- include/spdlog/details/pattern_formatter_impl.h | 11 +++++++++-- include/spdlog/logger.h | 3 +++ include/spdlog/tweakme.h | 4 ++-- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 33486c28..9bb851ad 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -88,7 +88,7 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg) try { #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) - msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed); + _incr_msg_counter(msg); #endif _async_log_helper->log(msg); if (_should_flush_on(msg)) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index d0f4a9d5..f693b517 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -512,7 +512,7 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons inline void spdlog::logger::_sink_it(details::log_msg& msg) { #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) - msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed); + _incr_msg_counter(msg); #endif _formatter->format(msg); for (auto &sink : _sinks) @@ -562,6 +562,11 @@ inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg) return (msg.level >= flush_level) && (msg.level != level::off); } +inline void spdlog::logger::_incr_msg_counter(details::log_msg &msg) +{ + msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed); +} + inline const std::vector& spdlog::logger::sinks() const { return _sinks; diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 05993e6c..bd546d08 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -384,6 +384,14 @@ class pid_formatter SPDLOG_FINAL:public flag_formatter } }; +// message counter formatter +class i_formatter SPDLOG_FINAL :public flag_formatter +{ + void format(details::log_msg& msg, const std::tm& tm_time) override + { + msg.formatted << fmt::pad(msg.msg_id, 6, '0'); + } +}; class v_formatter SPDLOG_FINAL:public flag_formatter { @@ -641,11 +649,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) _formatters.push_back(std::unique_ptr(new details::pid_formatter())); break; -#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) + case ('i'): _formatters.push_back(std::unique_ptr(new details::i_formatter())); break; -#endif default: //Unknown flag appears as is _formatters.push_back(std::unique_ptr(new details::ch_formatter('%'))); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 642208eb..d864d0dd 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -118,6 +118,9 @@ protected: // return true if the given message level should trigger a flush bool _should_flush_on(const details::log_msg&); + // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)) + void _incr_msg_counter(details::log_msg &msg); + const std::string _name; std::vector _sinks; formatter_ptr _formatter; diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 8c424497..354fcebc 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -135,8 +135,8 @@ /////////////////////////////////////////////////////////////////////////////// -// Uncomment to enable message counting feature. Adds %i logger pattern that -// prints log message sequence id. +// Uncomment to enable message counting feature. +// Use the %i in the logger pattern to display log message sequence id. // // #define SPDLOG_ENABLE_MESSAGE_COUNTER ///////////////////////////////////////////////////////////////////////////////