Commit 709948ff authored by gabime's avatar gabime

Fixed issue #527

parent 96886899
...@@ -88,7 +88,7 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg) ...@@ -88,7 +88,7 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
try try
{ {
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed); _incr_msg_counter(msg);
#endif #endif
_async_log_helper->log(msg); _async_log_helper->log(msg);
if (_should_flush_on(msg)) if (_should_flush_on(msg))
......
...@@ -512,7 +512,7 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons ...@@ -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) inline void spdlog::logger::_sink_it(details::log_msg& msg)
{ {
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed); _incr_msg_counter(msg);
#endif #endif
_formatter->format(msg); _formatter->format(msg);
for (auto &sink : _sinks) for (auto &sink : _sinks)
...@@ -562,6 +562,11 @@ inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg) ...@@ -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); 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::sink_ptr>& spdlog::logger::sinks() const inline const std::vector<spdlog::sink_ptr>& spdlog::logger::sinks() const
{ {
return _sinks; return _sinks;
......
...@@ -384,6 +384,14 @@ class pid_formatter SPDLOG_FINAL:public flag_formatter ...@@ -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 class v_formatter SPDLOG_FINAL:public flag_formatter
{ {
...@@ -641,11 +649,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) ...@@ -641,11 +649,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::pid_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::pid_formatter()));
break; break;
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
case ('i'): case ('i'):
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::i_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::i_formatter()));
break; break;
#endif
default: //Unknown flag appears as is default: //Unknown flag appears as is
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%')));
......
...@@ -118,6 +118,9 @@ protected: ...@@ -118,6 +118,9 @@ protected:
// return true if the given message level should trigger a flush // return true if the given message level should trigger a flush
bool _should_flush_on(const details::log_msg&); 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; const std::string _name;
std::vector<sink_ptr> _sinks; std::vector<sink_ptr> _sinks;
formatter_ptr _formatter; formatter_ptr _formatter;
......
...@@ -135,8 +135,8 @@ ...@@ -135,8 +135,8 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable message counting feature. Adds %i logger pattern that // Uncomment to enable message counting feature.
// prints log message sequence id. // Use the %i in the logger pattern to display log message sequence id.
// //
// #define SPDLOG_ENABLE_MESSAGE_COUNTER // #define SPDLOG_ENABLE_MESSAGE_COUNTER
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment