async flush now waits for queue to be empty before returning

This commit is contained in:
gabime 2016-08-22 22:07:29 +03:00
parent dfa2c7a950
commit 4f52cc4dec
2 changed files with 95 additions and 84 deletions

View File

@ -60,7 +60,8 @@ public:
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
const std::function<void()>& worker_teardown_cb = nullptr);
//Wait for the queue to be empty, and flush synchronously
//Warning: this can potentialy last forever as we wait it to complete
void flush() override;
protected:
void _sink_it(details::log_msg& msg) override;

View File

@ -179,6 +179,9 @@ private:
// sleep,yield or return immediatly using the time passed since last message as a hint
static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
// wait until the queue is empty
void wait_empty_q();
};
}
}
@ -249,12 +252,9 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe
//wait for the queue be empty and request flush from its sinks
inline void spdlog::details::async_log_helper::flush()
{
auto last_op = details::os::now();
while (_q.approx_size() > 0)
{
sleep_or_yield(details::os::now(), last_op);
}
wait_empty_q();
push_msg(async_msg(async_msg_type::flush));
wait_empty_q(); //make sure the above flush message was processed
}
inline void spdlog::details::async_log_helper::worker_loop()
@ -366,6 +366,16 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
return sleep_for(milliseconds(200));
}
// wait for the queue to be empty
inline void spdlog::details::async_log_helper::wait_empty_q()
{
auto last_op = details::os::now();
while (_q.approx_size() > 0) {
sleep_or_yield(details::os::now(), last_op);
}
}