Commit 0aeaf9e2 authored by Alexander Zilberkant's avatar Alexander Zilberkant

add an option to warn about discarded messages

when using async_logger with async_overflow_policy::discard_log_msg each discarded
message will be counted and warning will be printed by the worker thread

this new feature is disabled by default - as it may have a performance hit when discarding messages
parent 47006c4e
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <thread> #include <thread>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <atomic>
namespace spdlog namespace spdlog
{ {
...@@ -185,6 +186,9 @@ private: ...@@ -185,6 +186,9 @@ private:
// wait until the queue is empty // wait until the queue is empty
void wait_empty_q(); void wait_empty_q();
// counter for messages discarded due to queue overflow
std::atomic<unsigned int> _discarded_msg_count;
}; };
} }
} }
...@@ -211,7 +215,8 @@ inline spdlog::details::async_log_helper::async_log_helper( ...@@ -211,7 +215,8 @@ inline spdlog::details::async_log_helper::async_log_helper(
_worker_warmup_cb(worker_warmup_cb), _worker_warmup_cb(worker_warmup_cb),
_flush_interval_ms(flush_interval_ms), _flush_interval_ms(flush_interval_ms),
_worker_teardown_cb(worker_teardown_cb), _worker_teardown_cb(worker_teardown_cb),
_worker_thread(&async_log_helper::worker_loop, this) _worker_thread(&async_log_helper::worker_loop, this),
_discarded_msg_count(0)
{} {}
// Send to the worker thread termination message(level=off) // Send to the worker thread termination message(level=off)
...@@ -237,7 +242,9 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg) ...@@ -237,7 +242,9 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg) inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg)
{ {
if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_overflow_policy::discard_log_msg) if (!_q.enqueue(std::move(new_msg)))
{
if (_overflow_policy != async_overflow_policy::discard_log_msg)
{ {
auto last_op_time = details::os::now(); auto last_op_time = details::os::now();
auto now = last_op_time; auto now = last_op_time;
...@@ -245,8 +252,14 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe ...@@ -245,8 +252,14 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe
{ {
now = details::os::now(); now = details::os::now();
sleep_or_yield(now, last_op_time); sleep_or_yield(now, last_op_time);
} while (!_q.enqueue(std::move(new_msg)));
}
else
{
#if defined(SPDLOG_ASYNC_COUNT_DISCARDED_MSG)
_discarded_msg_count++;
#endif
} }
while (!_q.enqueue(std::move(new_msg)));
} }
} }
...@@ -305,6 +318,19 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_ ...@@ -305,6 +318,19 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
break; break;
default: default:
#if defined(SPDLOG_ASYNC_COUNT_DISCARDED_MSG)
unsigned int num_of_discarded_messages = _discarded_msg_count.exchange(0);
if (num_of_discarded_messages)
{
log_msg discarded_warning_msg(&incoming_async_msg.logger_name, level::warn);
discarded_warning_msg.raw << "Dropped " << num_of_discarded_messages << " messages";
for (auto &s : _sinks)
{
s->log(discarded_warning_msg);
}
}
#endif
log_msg incoming_log_msg; log_msg incoming_log_msg;
incoming_async_msg.fill_log_msg(incoming_log_msg); incoming_async_msg.fill_log_msg(incoming_log_msg);
_formatter->format(incoming_log_msg); _formatter->format(incoming_log_msg);
......
...@@ -114,3 +114,10 @@ ...@@ -114,3 +114,10 @@
// //
// #define SPDLOG_FINAL final // #define SPDLOG_FINAL final
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment count in print warning message about number of dropped messages.
// Only relevant for async_logger with async_overflow_policy::discard_log_msg
//
#define SPDLOG_ASYNC_COUNT_DISCARDED_MSG
///////////////////////////////////////////////////////////////////////////////
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