simplified worker thread in async logger

This commit is contained in:
gabime 2014-12-21 02:26:53 +02:00
parent fd7650a84e
commit 616cce5b71

View File

@ -118,7 +118,6 @@ public:
private: private:
std::atomic<bool> _active;
formatter_ptr _formatter; formatter_ptr _formatter;
std::vector<std::shared_ptr<sinks::sink>> _sinks; std::vector<std::shared_ptr<sinks::sink>> _sinks;
q_type _q; q_type _q;
@ -149,7 +148,6 @@ private:
// async_sink class implementation // 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): inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size):
_active(true),
_formatter(formatter), _formatter(formatter),
_sinks(sinks), _sinks(sinks),
_q(queue_size), _q(queue_size),
@ -160,12 +158,13 @@ inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatt
// and wait for it to finish gracefully // and wait for it to finish gracefully
inline spdlog::details::async_log_helper::~async_log_helper() inline spdlog::details::async_log_helper::~async_log_helper()
{ {
log(log_msg(level::off));
try try
{ {
log(log_msg(level::off));
_worker_thread.join(); _worker_thread.join();
} }
catch (const std::system_error&) //Dont crash if thread not joinable catch (...) //Dont crash if thread not joinable
{} {}
} }
@ -189,17 +188,23 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
inline void spdlog::details::async_log_helper::worker_loop() inline void spdlog::details::async_log_helper::worker_loop()
{ {
clock::time_point last_pop = clock::now(); try
while (_active) {
{ clock::time_point last_pop = clock::now();
//Dont die if there are still messages in the q to process while(process_next_msg(last_pop));
while(process_next_msg(last_pop));
} }
catch (const std::exception& ex)
{
_last_workerthread_ex = std::make_shared<spdlog_ex>(std::string("async_logger worker thread exception: ") + ex.what());
}
catch (...)
{
_last_workerthread_ex = std::make_shared<spdlog_ex>("async_logger worker thread exception");
}
} }
//Process next message in the queue // Process next message in the queue
//Return true if message was processed in the queue, or false if queue was empty // Return true if this thread should still be active (no msg with level::off was received)
//Will set _active to be false upon receiving log_msg with level==off (idicating the worker need to die)
inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_point& last_pop) inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_point& last_pop)
{ {
@ -208,35 +213,21 @@ inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_poin
if (_q.dequeue(incoming_async_msg)) if (_q.dequeue(incoming_async_msg))
{ {
last_pop = clock::now(); last_pop = clock::now();
try
{ if(incoming_async_msg.level == level::off)
incoming_async_msg.fill_log_msg(incoming_log_msg); return false;
if(incoming_log_msg.level == level::off)
{ incoming_async_msg.fill_log_msg(incoming_log_msg);
_active = false; _formatter->format(incoming_log_msg);
return false; for (auto &s : _sinks)
} s->log(incoming_log_msg);
_formatter->format(incoming_log_msg); }
for (auto &s : _sinks) else //empty queue
s->log(incoming_log_msg);
}
catch (const std::exception& ex)
{
_last_workerthread_ex = std::make_shared<spdlog_ex>(std::string("async_logger worker thread exception: ") + ex.what());
}
catch (...)
{
_last_workerthread_ex = std::make_shared<spdlog_ex>("async_logger worker thread exception");
}
return true;
}
// sleep or yield if queue is empty.
else
{ {
sleep_or_yield(last_pop); sleep_or_yield(last_pop);
return false;
} }
return true;
} }
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter) inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
@ -277,8 +268,6 @@ inline void spdlog::details::async_log_helper::throw_if_bad_worker()
auto ex = std::move(_last_workerthread_ex); auto ex = std::move(_last_workerthread_ex);
throw *ex; throw *ex;
} }
if (!_active)
throw(spdlog_ex("async logger is not active"));
} }