Skip to content

Asynchronous logging

Gabi Melman edited this page Apr 11, 2015 · 16 revisions

Asynchronous logging works be using the spdlog::async_logger. Upon it creation it create a queue with pre allocated slots and a worker thread. When the use logs a message the following happen:

  1. The next empty slot in the queue is used to store the log message. If the queue is full (exceeded it's max_size), the call blocks until there is room again.
  2. The worker thread pops the message from the queue and log the message using the logger's sinks.
  3. If an exception happens in the worker thread (e.g. failed logging to file) an exception will be re-thrown when the user calls the next log message. This way the user can be notified on errors happening in the worker thread.

Full queue policy

User can decide what to do when the queue is full:

  • Block the call until there is more room (default behaviour)
  • Discard the message and never block - by setting the async_overflow_policy:
spdlog::set_async_mode(q_size, async_overflow_policy::discard_log_msg)
Clone this wiki locally