|
|
|
@ -32,11 +32,11 @@
|
|
|
|
|
|
|
|
|
|
namespace spdlog
|
|
|
|
|
{
|
|
|
|
|
namespace details
|
|
|
|
|
{
|
|
|
|
|
namespace details
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class async_log_helper
|
|
|
|
|
{
|
|
|
|
|
class async_log_helper
|
|
|
|
|
{
|
|
|
|
|
// Async msg to move to/from the queue
|
|
|
|
|
// Movable only. should never be copied
|
|
|
|
|
enum class async_msg_type
|
|
|
|
@ -58,7 +58,7 @@ class async_log_helper
|
|
|
|
|
~async_msg() = default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|
|
|
|
async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|
|
|
|
logger_name(std::move(other.logger_name)),
|
|
|
|
|
level(std::move(other.level)),
|
|
|
|
|
time(std::move(other.time)),
|
|
|
|
@ -66,7 +66,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|
|
|
|
msg_type(std::move(other.msg_type))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
async_msg(async_msg_type m_type) :msg_type(m_type)
|
|
|
|
|
async_msg(async_msg_type m_type):msg_type(m_type)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT
|
|
|
|
@ -85,7 +85,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|
|
|
|
async_msg& operator=(const async_msg& other) = delete;
|
|
|
|
|
|
|
|
|
|
// construct from log_msg
|
|
|
|
|
async_msg(const details::log_msg& m) :
|
|
|
|
|
async_msg(const details::log_msg& m):
|
|
|
|
|
level(m.level),
|
|
|
|
|
time(m.time),
|
|
|
|
|
thread_id(m.thread_id),
|
|
|
|
@ -109,7 +109,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
using item_type = async_msg;
|
|
|
|
|
using q_type = details::mpmc_bounded_queue<item_type>;
|
|
|
|
@ -135,8 +135,9 @@ public:
|
|
|
|
|
|
|
|
|
|
void flush(bool wait_for_q);
|
|
|
|
|
|
|
|
|
|
void set_error_handler(spdlog::log_err_handler err_handler);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
private:
|
|
|
|
|
formatter_ptr _formatter;
|
|
|
|
|
std::vector<std::shared_ptr<sinks::sink>> _sinks;
|
|
|
|
|
|
|
|
|
@ -182,8 +183,8 @@ private:
|
|
|
|
|
// wait until the queue is empty
|
|
|
|
|
void wait_empty_q();
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
@ -215,13 +216,13 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
|
|
|
|
// and wait for it to finish gracefully
|
|
|
|
|
inline spdlog::details::async_log_helper::~async_log_helper()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
push_msg(async_msg(async_msg_type::terminate));
|
|
|
|
|
_worker_thread.join();
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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 now = last_op_time;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
do {
|
|
|
|
|
now = details::os::now();
|
|
|
|
|
sleep_or_yield(now, last_op_time);
|
|
|
|
|
}
|
|
|
|
|
while (!_q.enqueue(std::move(new_msg)));
|
|
|
|
|
} while (!_q.enqueue(std::move(new_msg)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -250,28 +248,30 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe
|
|
|
|
|
inline void spdlog::details::async_log_helper::flush(bool wait_for_q)
|
|
|
|
|
{
|
|
|
|
|
push_msg(async_msg(async_msg_type::flush));
|
|
|
|
|
if(wait_for_q)
|
|
|
|
|
if (wait_for_q)
|
|
|
|
|
wait_empty_q(); //return only make after the above flush message was processed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
|
auto last_pop = details::os::now();
|
|
|
|
|
auto last_flush = last_pop;
|
|
|
|
|
while(process_next_msg(last_pop, last_flush));
|
|
|
|
|
if (_worker_teardown_cb) _worker_teardown_cb();
|
|
|
|
|
auto active = true;
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
catch (...) {
|
|
|
|
|
_err_handler("Unknown exception");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_worker_teardown_cb) _worker_teardown_cb();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process next message in the queue
|
|
|
|
@ -280,11 +280,9 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
|
{
|
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
|
|
|
|
|
|
if (_q.dequeue(incoming_async_msg))
|
|
|
|
|
{
|
|
|
|
|
if (_q.dequeue(incoming_async_msg)) {
|
|
|
|
|
last_pop = details::os::now();
|
|
|
|
|
switch (incoming_async_msg.msg_type)
|
|
|
|
|
{
|
|
|
|
|
switch (incoming_async_msg.msg_type) {
|
|
|
|
|
case async_msg_type::flush:
|
|
|
|
|
_flush_requested = true;
|
|
|
|
|
break;
|
|
|
|
@ -298,10 +296,8 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|
|
|
|
log_msg incoming_log_msg;
|
|
|
|
|
incoming_async_msg.fill_log_msg(incoming_log_msg);
|
|
|
|
|
_formatter->format(incoming_log_msg);
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
|
{
|
|
|
|
|
if(s->should_log( incoming_log_msg.level))
|
|
|
|
|
{
|
|
|
|
|
for (auto &s : _sinks) {
|
|
|
|
|
if (s->should_log(incoming_log_msg.level)) {
|
|
|
|
|
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..
|
|
|
|
|
// 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();
|
|
|
|
|
handle_flush_interval(now, last_flush);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
s->flush();
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
auto last_op = details::os::now();
|
|
|
|
|
while (_q.approx_size() > 0)
|
|
|
|
|
{
|
|
|
|
|
while (_q.approx_size() > 0) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|