discard policies on queue overflow

This commit is contained in:
Denis Ivaykin
2015-01-14 15:35:01 -06:00
parent 0f3f65e07c
commit 365d895482
6 changed files with 31 additions and 19 deletions

View File

@@ -109,7 +109,7 @@ public:
using clock = std::chrono::steady_clock;
async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
void log(const details::log_msg& msg);
//Stop logging and join the back thread
@@ -125,6 +125,9 @@ private:
// last exception thrown from the worker thread
std::shared_ptr<spdlog_ex> _last_workerthread_ex;
// overflow policy
const async_queue_overflow_policy _overflow_policy;
// worker thread warmup callback - one can set thread priority, affinity, etc
const std::function<void()> _worker_warmup_cb;
@@ -150,10 +153,11 @@ private:
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb):
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb):
_formatter(formatter),
_sinks(sinks),
_q(queue_size),
_overflow_policy(overflow_policy),
_worker_warmup_cb(worker_warmup_cb),
_worker_thread(&async_log_helper::worker_loop, this)
{}
@@ -178,7 +182,7 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
{
throw_if_bad_worker();
async_msg new_msg(msg);
if (!_q.enqueue(std::move(new_msg)))
if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_queue_overflow_policy::discard_log_msg)
{
auto last_op_time = clock::now();
do