18#include <condition_variable>
47 explicit async_writer(std::unique_ptr<log_writer_interface> wrapped_writer,
48 std::size_t queue_size = 10000,
50 :
base_type(std::move(wrapped_writer), queue_size,
"async")
67 bool expected =
false;
68 if (!
running_.compare_exchange_strong(expected,
true)) {
77 }
catch (
const std::exception&) {
88 void stop(
bool force_flush =
true) {
96 size_t remaining_count =
queue_.size();
97 if (remaining_count > 0) {
98 std::cerr <<
"[async_writer] Info: Processing " << remaining_count
99 <<
" remaining messages before shutdown.\n";
116 std::cerr <<
"[async_writer] Warning: " <<
queue_.size()
117 <<
" messages were not processed during shutdown.\n";
154 "Flush operation timed out after " +
210 auto entry = std::move(
queue_.front());
230 auto entry = std::move(
queue_.front());
Asynchronous wrapper for log writers.
~async_writer() override
Destructor.
void on_entry_enqueued() override
Called after entry is enqueued - notifies worker thread.
std::string get_name() const override
Get the name of this writer.
bool is_healthy() const override
Check if the writer is healthy.
void stop(bool force_flush=true)
Stop the async writer thread.
std::atomic< bool > running_
std::thread worker_thread_
void start()
Start the async writer thread.
void process_messages()
Process messages from the queue.
common::VoidResult flush() override
Flush all pending messages.
std::chrono::seconds flush_timeout_
std::condition_variable flush_cv_
std::condition_variable queue_cv_
async_writer(std::unique_ptr< log_writer_interface > wrapped_writer, std::size_t queue_size=10000, std::chrono::seconds flush_timeout=std::chrono::seconds(5))
Constructor accepting log_writer_interface (Decorator pattern)
common::VoidResult write(const log_entry &entry) override
Write a log entry asynchronously.
common::VoidResult handle_overflow(const log_entry &) override
Handle queue overflow.
void flush_remaining()
Flush any remaining messages after stopping.
log_writer_interface & wrapped() noexcept
Access the wrapped writer (non-const)
virtual std::string get_name() const =0
virtual bool is_healthy() const =0
Check if the writer is healthy.
virtual common::VoidResult flush()=0
Flush any buffered data.
virtual common::VoidResult write(const log_entry &entry)=0
Write a log entry.
Abstract base class for queue-based log writers.
std::queue< log_entry > queue_
common::VoidResult try_enqueue(const log_entry &entry)
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
Base template for queue-based log writers.
Represents a single log entry with all associated metadata.