|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Standalone async worker with jthread compatibility. More...
#include <async_worker.h>

Public Member Functions | |
| async_worker (std::size_t queue_size=8192) | |
| Constructor with configurable queue size. | |
| ~async_worker () | |
| Destructor - ensures graceful shutdown. | |
| async_worker (const async_worker &)=delete | |
| async_worker & | operator= (const async_worker &)=delete |
| async_worker (async_worker &&)=delete | |
| async_worker & | operator= (async_worker &&)=delete |
| void | start () |
| Start the worker thread. | |
| void | stop () |
| Stop the worker thread gracefully. | |
| bool | enqueue (task_type task) |
| Enqueue a task for async processing. | |
| void | flush () |
| Flush all pending tasks. | |
| bool | is_running () const noexcept |
| Check if the worker is currently running. | |
| std::size_t | pending_count () const |
| Get the current queue size. | |
| std::size_t | capacity () const noexcept |
| Get the queue capacity. | |
| std::uint64_t | dropped_count () const noexcept |
| Get the number of dropped tasks due to queue overflow. | |
Private Member Functions | |
| void | worker_loop (simple_stop_source &stop) |
| Worker thread main loop (fallback version) | |
| void | drain_queue () |
| Process all remaining tasks in the queue. | |
Private Attributes | |
| const std::size_t | queue_size_ |
| Maximum queue capacity. | |
| std::queue< task_type > | queue_ |
| Task queue. | |
| std::mutex | queue_mutex_ |
| Protects queue_ access. | |
| std::condition_variable | queue_cv_ |
| Signals new tasks or stop (fallback) | |
| compat_jthread | worker_thread_ |
| Background worker thread. | |
| std::atomic< bool > | running_ {false} |
| Worker state flag. | |
| std::atomic< std::uint64_t > | dropped_count_ {0} |
| Overflow counter. | |
Standalone async worker with jthread compatibility.
This worker provides a background thread for processing logging tasks asynchronously. It uses std::jthread's cooperative cancellation mechanism via std::stop_token for graceful shutdown when available, or falls back to manual stop mechanism for environments without jthread support.
Thread Safety:
Performance Characteristics:
Usage:
Definition at line 82 of file async_worker.h.
|
explicit |
Constructor with configurable queue size.
| queue_size | Maximum number of pending tasks (default: 8192) |
The queue size should be chosen based on expected logging throughput:
Definition at line 12 of file async_worker.cpp.
| kcenon::logger::async::async_worker::~async_worker | ( | ) |
Destructor - ensures graceful shutdown.
Automatically calls stop() if the worker is still running. The destructor will block until all pending tasks are processed and the worker thread has terminated.
Definition at line 17 of file async_worker.cpp.
References stop().

|
delete |
|
delete |
|
nodiscardnoexcept |
Get the queue capacity.
Definition at line 122 of file async_worker.cpp.
References queue_size_.
|
private |
Process all remaining tasks in the queue.
Called during shutdown to ensure no tasks are lost.
Definition at line 209 of file async_worker.cpp.
References queue_, and queue_mutex_.
Referenced by flush(), and stop().

|
nodiscardnoexcept |
Get the number of dropped tasks due to queue overflow.
Definition at line 126 of file async_worker.cpp.
References dropped_count_.
| bool kcenon::logger::async::async_worker::enqueue | ( | task_type | task | ) |
Enqueue a task for async processing.
| task | The task to execute (callable with no arguments) |
If the queue is full, the task is dropped and false is returned. The caller should handle this case (e.g., log synchronously or retry).
Thread-safe: Multiple threads can call enqueue() concurrently.
Definition at line 62 of file async_worker.cpp.
References dropped_count_, queue_, queue_cv_, queue_mutex_, queue_size_, and running_.
| void kcenon::logger::async::async_worker::flush | ( | ) |
Flush all pending tasks.
Blocks until all tasks currently in the queue have been processed. New tasks enqueued during flush() may or may not be included.
Thread-safe: Can be called from any thread.
Definition at line 92 of file async_worker.cpp.
References drain_queue(), queue_, queue_mutex_, and running_.

|
nodiscardnoexcept |
Check if the worker is currently running.
Definition at line 113 of file async_worker.cpp.
References running_.
|
delete |
|
delete |
|
nodiscard |
Get the current queue size.
Definition at line 117 of file async_worker.cpp.
References queue_, and queue_mutex_.
| void kcenon::logger::async::async_worker::start | ( | ) |
Start the worker thread.
Creates a background thread that processes tasks from the queue. The thread will run until stop() is called or the worker is destroyed.
Thread-safe: Can be called from any thread. Idempotent: Multiple calls are safe (subsequent calls are no-ops).
Definition at line 21 of file async_worker.cpp.
References running_, stop(), worker_loop(), and worker_thread_.

| void kcenon::logger::async::async_worker::stop | ( | ) |
Stop the worker thread gracefully.
Signals the worker thread to stop and waits for it to complete processing remaining tasks. This method:
Thread-safe: Can be called from any thread. Idempotent: Multiple calls are safe (subsequent calls are no-ops).
Definition at line 43 of file async_worker.cpp.
References drain_queue(), kcenon::logger::async::compat_jthread::join(), queue_cv_, kcenon::logger::async::compat_jthread::request_stop(), running_, and worker_thread_.
Referenced by start(), worker_loop(), and ~async_worker().


|
private |
Worker thread main loop (fallback version)
| stop | Stop source for manual cancellation |
Processes tasks from the queue until stop is requested. Uses condition variable for efficient waiting.
Definition at line 170 of file async_worker.cpp.
References queue_, queue_cv_, queue_mutex_, and stop().
Referenced by start().


|
private |
Overflow counter.
Definition at line 238 of file async_worker.h.
Referenced by dropped_count(), and enqueue().
|
private |
Task queue.
Definition at line 227 of file async_worker.h.
Referenced by drain_queue(), enqueue(), flush(), pending_count(), and worker_loop().
|
private |
Signals new tasks or stop (fallback)
Definition at line 233 of file async_worker.h.
Referenced by enqueue(), stop(), and worker_loop().
|
mutableprivate |
Protects queue_ access.
Definition at line 228 of file async_worker.h.
Referenced by drain_queue(), enqueue(), flush(), pending_count(), and worker_loop().
|
private |
Maximum queue capacity.
Definition at line 226 of file async_worker.h.
Referenced by capacity(), and enqueue().
|
private |
Worker state flag.
Definition at line 237 of file async_worker.h.
Referenced by enqueue(), flush(), is_running(), start(), and stop().
|
private |
Background worker thread.
Definition at line 236 of file async_worker.h.