|
|
|
@ -115,9 +115,6 @@ public:
|
|
|
|
|
//Stop logging and join the back thread
|
|
|
|
|
~async_log_helper();
|
|
|
|
|
void set_formatter(formatter_ptr);
|
|
|
|
|
//Wait to remaining items (if any) in the queue to be written and shutdown
|
|
|
|
|
void shutdown(const log_clock::duration& timeout);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
@ -131,14 +128,16 @@ private:
|
|
|
|
|
std::shared_ptr<spdlog_ex> _last_workerthread_ex;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// will throw last worker thread exception or if worker thread no active
|
|
|
|
|
// throw last worker thread exception or if worker thread is not active
|
|
|
|
|
void throw_if_bad_worker();
|
|
|
|
|
|
|
|
|
|
// worker thread loop
|
|
|
|
|
// worker thread main loop
|
|
|
|
|
void worker_loop();
|
|
|
|
|
|
|
|
|
|
//pop next message from the queue and process it
|
|
|
|
|
//return true if a message was available (queue was not empty), will set the last_pop to the pop time
|
|
|
|
|
bool process_next_msg(clock::time_point& last_pop);
|
|
|
|
|
|
|
|
|
|
// guess how much to sleep if queue is empty/full using last succesful op time as hint
|
|
|
|
|
static void sleep_or_yield(const clock::time_point& last_op_time);
|
|
|
|
|
|
|
|
|
@ -146,7 +145,6 @@ private:
|
|
|
|
|
// clear all remaining messages(if any), stop the _worker_thread and join it
|
|
|
|
|
void join_worker();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -155,7 +153,7 @@ 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):
|
|
|
|
|
_active(false),
|
|
|
|
|
_active(true),
|
|
|
|
|
_formatter(formatter),
|
|
|
|
|
_sinks(sinks),
|
|
|
|
|
_q(queue_size),
|
|
|
|
@ -172,97 +170,92 @@ inline spdlog::details::async_log_helper::~async_log_helper()
|
|
|
|
|
inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
|
|
|
|
|
{
|
|
|
|
|
throw_if_bad_worker();
|
|
|
|
|
|
|
|
|
|
//Only if queue is full, enter wait loop
|
|
|
|
|
//if (!_q.push(std::unique_ptr < async_msg >(new async_msg(msg))))
|
|
|
|
|
//async_msg* as = new async_msg(msg);
|
|
|
|
|
//if (!_q.enqueue(std::unique_ptr<async_msg>(new async_msg(msg))))
|
|
|
|
|
if (!_q.enqueue(std::move(async_msg(msg))))
|
|
|
|
|
async_msg new_msg(msg);
|
|
|
|
|
if (!_q.enqueue(std::move(new_msg)))
|
|
|
|
|
{
|
|
|
|
|
auto last_op_time = clock::now();
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
sleep_or_yield(last_op_time);
|
|
|
|
|
}
|
|
|
|
|
while (!_q.enqueue(std::move(async_msg(msg))));
|
|
|
|
|
while (!_q.enqueue(std::move(new_msg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
|
{
|
|
|
|
|
log_msg popped_log_msg;
|
|
|
|
|
clock::time_point last_pop = clock::now();
|
|
|
|
|
_active = true;
|
|
|
|
|
while (_active)
|
|
|
|
|
{
|
|
|
|
|
q_type::item_type popped_msg;
|
|
|
|
|
//Dont die if there are still messages in the q to process
|
|
|
|
|
while(process_next_msg(last_pop));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_q.dequeue(popped_msg))
|
|
|
|
|
inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_point& last_pop)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
|
log_msg incoming_log_msg;
|
|
|
|
|
|
|
|
|
|
if (_q.dequeue(incoming_async_msg))
|
|
|
|
|
{
|
|
|
|
|
last_pop = clock::now();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
popped_msg.fill_log_msg(popped_log_msg);
|
|
|
|
|
_formatter->format(popped_log_msg);
|
|
|
|
|
incoming_async_msg.fill_log_msg(incoming_log_msg);
|
|
|
|
|
_formatter->format(incoming_log_msg);
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
|
s->log(popped_log_msg);
|
|
|
|
|
s->log(incoming_log_msg);
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
|
{
|
|
|
|
|
_last_workerthread_ex = std::make_shared<spdlog_ex>(ex.what());
|
|
|
|
|
_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>("Unknown exception");
|
|
|
|
|
_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);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
|
|
|
|
|
{
|
|
|
|
|
_formatter = msg_formatter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::shutdown(const log_clock::duration& timeout)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
if (timeout > std::chrono::milliseconds::zero())
|
|
|
|
|
{
|
|
|
|
|
auto until = log_clock::now() + timeout;
|
|
|
|
|
while (_q.approx_size() > 0 && log_clock::now() < until)
|
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
join_worker();
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sleep or yield using the time passed since last message as a hint
|
|
|
|
|
// Sleep,yield or return immediatly using the time passed since last message as a hint
|
|
|
|
|
inline void spdlog::details::async_log_helper::sleep_or_yield(const clock::time_point& last_op_time)
|
|
|
|
|
{
|
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
|
using std::this_thread::sleep_for;
|
|
|
|
|
using std::this_thread::yield;
|
|
|
|
|
using namespace std::this_thread;
|
|
|
|
|
|
|
|
|
|
clock::duration sleep_duration;
|
|
|
|
|
auto time_since_op = clock::now() - last_op_time;
|
|
|
|
|
if (time_since_op > milliseconds(1000))
|
|
|
|
|
sleep_for(milliseconds(500));
|
|
|
|
|
else if (time_since_op > milliseconds(1))
|
|
|
|
|
sleep_for(time_since_op / 2);
|
|
|
|
|
else
|
|
|
|
|
yield();
|
|
|
|
|
|
|
|
|
|
//spin upto 1 ms
|
|
|
|
|
if (time_since_op <= milliseconds(1))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// yield upto 10ms
|
|
|
|
|
if (time_since_op <= milliseconds(10))
|
|
|
|
|
return yield();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sleep for half of duration since last op
|
|
|
|
|
if (time_since_op <= milliseconds(100))
|
|
|
|
|
return sleep_for(time_since_op / 2);
|
|
|
|
|
|
|
|
|
|
return sleep_for(milliseconds(100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//throw if the worker thread threw an exception or not active
|
|
|
|
@ -282,17 +275,13 @@ inline void spdlog::details::async_log_helper::throw_if_bad_worker()
|
|
|
|
|
inline void spdlog::details::async_log_helper::join_worker()
|
|
|
|
|
{
|
|
|
|
|
_active = false;
|
|
|
|
|
if (_worker_thread.joinable())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_worker_thread.join();
|
|
|
|
|
}
|
|
|
|
|
catch (const std::system_error&) //Dont crash if thread not joinable
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|