|
|
@ -135,6 +135,7 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
void flush(bool wait_for_q);
|
|
|
|
void flush(bool wait_for_q);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void set_error_handler(spdlog::log_err_handler err_handler);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
formatter_ptr _formatter;
|
|
|
|
formatter_ptr _formatter;
|
|
|
@ -215,13 +216,13 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
|
|
|
// 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()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try {
|
|
|
|
{
|
|
|
|
|
|
|
|
push_msg(async_msg(async_msg_type::terminate));
|
|
|
|
push_msg(async_msg(async_msg_type::terminate));
|
|
|
|
_worker_thread.join();
|
|
|
|
_worker_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) // don't crash in destructor
|
|
|
|
catch (...) // don't crash in destructor
|
|
|
|
{}
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -233,16 +234,13 @@ 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)) && _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;
|
|
|
|
do
|
|
|
|
do {
|
|
|
|
{
|
|
|
|
|
|
|
|
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)));
|
|
|
|
while (!_q.enqueue(std::move(new_msg)));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -255,24 +253,26 @@ inline void spdlog::details::async_log_helper::flush(bool wait_for_q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
if (_worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
auto last_pop = details::os::now();
|
|
|
|
auto last_pop = details::os::now();
|
|
|
|
auto last_flush = last_pop;
|
|
|
|
auto last_flush = last_pop;
|
|
|
|
while(process_next_msg(last_pop, last_flush));
|
|
|
|
auto active = true;
|
|
|
|
if (_worker_teardown_cb) _worker_teardown_cb();
|
|
|
|
while (active) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
active = process_next_msg(last_pop, last_flush);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception &ex)
|
|
|
|
catch (const std::exception &ex) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_err_handler(ex.what());
|
|
|
|
_err_handler(ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
catch (...) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_err_handler("Unknown exception");
|
|
|
|
_err_handler("Unknown exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_worker_teardown_cb) _worker_teardown_cb();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// process next message in the queue
|
|
|
|
// process next message in the queue
|
|
|
|
// return true if this thread should still be active (while no terminate msg was received)
|
|
|
|
// return true if this thread should still be active (while no terminate msg was received)
|
|
|
@ -280,11 +280,9 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
{
|
|
|
|
{
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
|
|
|
|
|
|
|
|
if (_q.dequeue(incoming_async_msg))
|
|
|
|
if (_q.dequeue(incoming_async_msg)) {
|
|
|
|
{
|
|
|
|
|
|
|
|
last_pop = details::os::now();
|
|
|
|
last_pop = details::os::now();
|
|
|
|
switch (incoming_async_msg.msg_type)
|
|
|
|
switch (incoming_async_msg.msg_type) {
|
|
|
|
{
|
|
|
|
|
|
|
|
case async_msg_type::flush:
|
|
|
|
case async_msg_type::flush:
|
|
|
|
_flush_requested = true;
|
|
|
|
_flush_requested = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -298,10 +296,8 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
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);
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
for (auto &s : _sinks) {
|
|
|
|
{
|
|
|
|
if (s->should_log(incoming_log_msg.level)) {
|
|
|
|
if(s->should_log( incoming_log_msg.level))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
s->log(incoming_log_msg);
|
|
|
|
s->log(incoming_log_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -311,8 +307,7 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
|
|
|
|
|
|
|
|
// Handle empty queue..
|
|
|
|
// Handle empty queue..
|
|
|
|
// This is the only place where the queue can terminate or flush to avoid losing messages already in the queue
|
|
|
|
// This is the only place where the queue can terminate or flush to avoid losing messages already in the queue
|
|
|
|
else
|
|
|
|
else {
|
|
|
|
{
|
|
|
|
|
|
|
|
auto now = details::os::now();
|
|
|
|
auto now = details::os::now();
|
|
|
|
handle_flush_interval(now, last_flush);
|
|
|
|
handle_flush_interval(now, last_flush);
|
|
|
|
sleep_or_yield(now, last_pop);
|
|
|
|
sleep_or_yield(now, last_pop);
|
|
|
@ -324,8 +319,7 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
inline void spdlog::details::async_log_helper::handle_flush_interval(log_clock::time_point& now, log_clock::time_point& last_flush)
|
|
|
|
inline void spdlog::details::async_log_helper::handle_flush_interval(log_clock::time_point& now, log_clock::time_point& last_flush)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto should_flush = _flush_requested || (_flush_interval_ms != std::chrono::milliseconds::zero() && now - last_flush >= _flush_interval_ms);
|
|
|
|
auto should_flush = _flush_requested || (_flush_interval_ms != std::chrono::milliseconds::zero() && now - last_flush >= _flush_interval_ms);
|
|
|
|
if (should_flush)
|
|
|
|
if (should_flush) {
|
|
|
|
{
|
|
|
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
s->flush();
|
|
|
|
s->flush();
|
|
|
|
now = last_flush = details::os::now();
|
|
|
|
now = last_flush = details::os::now();
|
|
|
@ -368,11 +362,15 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
|
|
|
|
inline void spdlog::details::async_log_helper::wait_empty_q()
|
|
|
|
inline void spdlog::details::async_log_helper::wait_empty_q()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto last_op = details::os::now();
|
|
|
|
auto last_op = details::os::now();
|
|
|
|
while (_q.approx_size() > 0)
|
|
|
|
while (_q.approx_size() > 0) {
|
|
|
|
{
|
|
|
|
|
|
|
|
sleep_or_yield(details::os::now(), last_op);
|
|
|
|
sleep_or_yield(details::os::now(), last_op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::set_error_handler(spdlog::log_err_handler err_handler)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_err_handler = err_handler;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|