-
Notifications
You must be signed in to change notification settings - Fork 5k
Asynchronous logging
Asynchronous logging works be using the spdlog::async_logger.
To turn async mode on call spdlog::set_async_mode(q_size) //size must be power of 2 to tell spdlog to create async loggers from now on.
Each such logger has a queue with pre allocated slots and a worker thread. When the user logs a message to an async logger the following happen:
-
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.
-
The worker thread pops the message from the queue and log the message using the logger's sinks.
-
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.
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)©gabime 2023-2024 spdlog. All Rights Reserved.