|
|
|
@ -120,7 +120,7 @@ public:
|
|
|
|
|
size_t queue_size,
|
|
|
|
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
|
|
|
|
const std::function<void()>& worker_warmup_cb = nullptr,
|
|
|
|
|
const std::chrono::milliseconds auto_flush_millis = std::chrono::milliseconds::zero());
|
|
|
|
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
|
|
|
|
|
|
|
|
|
void log(const details::log_msg& msg);
|
|
|
|
|
|
|
|
|
@ -147,7 +147,7 @@ private:
|
|
|
|
|
const std::function<void()> _worker_warmup_cb;
|
|
|
|
|
|
|
|
|
|
// auto periodic sink flush parameter
|
|
|
|
|
const std::chrono::milliseconds _auto_flush_millis;
|
|
|
|
|
const std::chrono::milliseconds _flush_interval_ms;
|
|
|
|
|
|
|
|
|
|
// worker thread
|
|
|
|
|
std::thread _worker_thread;
|
|
|
|
@ -160,10 +160,10 @@ private:
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
bool process_next_msg(log_clock::time_point& last_pop, log_clock::time_point& last_flush);
|
|
|
|
|
|
|
|
|
|
// sleep,yield or return immediatly using the time passed since last message as a hint
|
|
|
|
|
static std::chrono::nanoseconds sleep_or_yield(const clock::time_point& last_op_time);
|
|
|
|
|
static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
@ -172,13 +172,13 @@ 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, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds auto_flush_millis):
|
|
|
|
|
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms):
|
|
|
|
|
_formatter(formatter),
|
|
|
|
|
_sinks(sinks),
|
|
|
|
|
_q(queue_size),
|
|
|
|
|
_overflow_policy(overflow_policy),
|
|
|
|
|
_worker_warmup_cb(worker_warmup_cb),
|
|
|
|
|
_auto_flush_millis(auto_flush_millis),
|
|
|
|
|
_flush_interval_ms(flush_interval_ms),
|
|
|
|
|
_worker_thread(&async_log_helper::worker_loop, this)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
@ -204,10 +204,12 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
|
|
|
|
|
async_msg new_msg(msg);
|
|
|
|
|
if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_overflow_policy::discard_log_msg)
|
|
|
|
|
{
|
|
|
|
|
auto last_op_time = clock::now();
|
|
|
|
|
auto last_op_time = log_clock::now();
|
|
|
|
|
auto now = last_op_time;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
sleep_or_yield(last_op_time);
|
|
|
|
|
now = log_clock::now();
|
|
|
|
|
sleep_or_yield(now, last_op_time);
|
|
|
|
|
}
|
|
|
|
|
while (!_q.enqueue(std::move(new_msg)));
|
|
|
|
|
}
|
|
|
|
@ -219,8 +221,9 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
|
clock::time_point last_pop = clock::now();
|
|
|
|
|
while(process_next_msg(last_pop));
|
|
|
|
|
auto last_pop = log_clock::now();
|
|
|
|
|
auto last_flush = last_pop;
|
|
|
|
|
while(process_next_msg(last_pop, last_flush));
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
|
{
|
|
|
|
@ -234,7 +237,7 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
|
|
|
|
|
|
// process next message in the queue
|
|
|
|
|
// return true if this thread should still be active (no msg with level::off was received)
|
|
|
|
|
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(log_clock::time_point& last_pop, log_clock::time_point& last_flush)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
@ -242,7 +245,7 @@ inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_poin
|
|
|
|
|
|
|
|
|
|
if (_q.dequeue(incoming_async_msg))
|
|
|
|
|
{
|
|
|
|
|
last_pop = clock::now();
|
|
|
|
|
last_pop = log_clock::now();
|
|
|
|
|
|
|
|
|
|
if(incoming_async_msg.level == level::off)
|
|
|
|
|
return false;
|
|
|
|
@ -254,12 +257,18 @@ inline bool spdlog::details::async_log_helper::process_next_msg(clock::time_poin
|
|
|
|
|
}
|
|
|
|
|
else //empty queue
|
|
|
|
|
{
|
|
|
|
|
auto time_since_op = sleep_or_yield(last_pop);
|
|
|
|
|
if (_auto_flush_millis > std::chrono::milliseconds::zero() && time_since_op > _auto_flush_millis)
|
|
|
|
|
auto now = log_clock::now();
|
|
|
|
|
if (_flush_interval_ms > std::chrono::milliseconds::zero())
|
|
|
|
|
{
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
|
s->flush();
|
|
|
|
|
auto time_since_flush = now - last_flush;
|
|
|
|
|
if (time_since_flush >= _flush_interval_ms)
|
|
|
|
|
{
|
|
|
|
|
last_flush = now;
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
|
s->flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sleep_or_yield(now, last_pop);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
@ -271,34 +280,27 @@ inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sleep,yield or return immediatly using the time passed since last message as a hint
|
|
|
|
|
inline std::chrono::nanoseconds spdlog::details::async_log_helper::sleep_or_yield(const clock::time_point& last_op_time)
|
|
|
|
|
inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_clock::time_point& now, const spdlog::log_clock::time_point& last_op_time)
|
|
|
|
|
{
|
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
|
using namespace std::this_thread;
|
|
|
|
|
|
|
|
|
|
auto time_since_op = clock::now() - last_op_time;
|
|
|
|
|
auto time_since_op = now - last_op_time;
|
|
|
|
|
|
|
|
|
|
// spin upto 1 ms
|
|
|
|
|
if (time_since_op <= milliseconds(1))
|
|
|
|
|
return time_since_op;
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// yield upto 10ms
|
|
|
|
|
if (time_since_op <= milliseconds(10))
|
|
|
|
|
{
|
|
|
|
|
yield();
|
|
|
|
|
return time_since_op;
|
|
|
|
|
}
|
|
|
|
|
return yield();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sleep for half of duration since last op
|
|
|
|
|
if (time_since_op <= milliseconds(100))
|
|
|
|
|
{
|
|
|
|
|
sleep_for(time_since_op / 2);
|
|
|
|
|
return time_since_op;
|
|
|
|
|
}
|
|
|
|
|
return sleep_for(time_since_op / 2);
|
|
|
|
|
|
|
|
|
|
sleep_for(milliseconds(100));
|
|
|
|
|
return time_since_op;
|
|
|
|
|
return sleep_for(milliseconds(100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// throw if the worker thread threw an exception or not active
|
|
|
|
|