|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Batch writer that accumulates log entries and writes them in batches. More...
#include <batch_writer.h>


Classes | |
| struct | batch_stats |
| Get batch statistics. More... | |
| struct | config |
| Configuration for batch writer. More... | |
Public Member Functions | |
| batch_writer (log_writer_ptr underlying_writer, const config &cfg=config{}) | |
| Construct a batch writer. | |
| ~batch_writer () override | |
| Destructor - ensures all pending entries are flushed. | |
| common::VoidResult | write (const log_entry &entry) override |
| Write a log entry to the batch. | |
| common::VoidResult | flush () override |
| Flush the batch to the underlying writer. | |
| std::string | get_name () const override |
| Get writer name. | |
| bool | is_healthy () const override |
| Check if writer is healthy. | |
| size_t | get_current_batch_size () const |
| Get current batch size. | |
| const batch_stats & | get_stats () const |
| Get batch statistics. | |
| void | reset_stats () |
| Reset batch statistics. | |
Public Member Functions inherited from kcenon::logger::queued_writer_base< std::vector< log_entry > > | |
| queued_writer_base (std::unique_ptr< log_writer_interface > wrapped_writer, std::size_t max_queue_size, std::string_view decorator_name) | |
| Constructor for queued writer base. | |
| ~queued_writer_base () override=default | |
| Virtual destructor. | |
| bool | is_healthy () const override |
| Check if the writer is healthy. | |
| std::size_t | get_queue_size () const |
| Get the current queue size. | |
| std::size_t | get_max_queue_size () const |
| Get maximum queue size. | |
Public Member Functions inherited from kcenon::logger::decorator_writer_base | |
| decorator_writer_base (std::unique_ptr< log_writer_interface > wrapped, std::string_view decorator_name) | |
| Construct a decorator writer base. | |
| ~decorator_writer_base () override=default | |
| Virtual destructor for proper cleanup. | |
| decorator_writer_base (const decorator_writer_base &)=delete | |
| decorator_writer_base & | operator= (const decorator_writer_base &)=delete |
| decorator_writer_base (decorator_writer_base &&) noexcept=default | |
| decorator_writer_base & | operator= (decorator_writer_base &&) noexcept=default |
| common::VoidResult | flush () override |
| Flush the wrapped writer. | |
| std::string | get_name () const override |
| Get the name of this writer. | |
| const log_writer_interface * | get_wrapped_writer () const noexcept |
| Get the wrapped writer (const version) | |
Public Member Functions inherited from kcenon::logger::log_writer_interface | |
| virtual | ~log_writer_interface ()=default |
| virtual common::VoidResult | close () |
| Close the writer and release resources. | |
| virtual auto | is_open () const -> bool |
| Check if writer is open and ready. | |
Private Types | |
| using | base_type = queued_writer_base<std::vector<log_entry>> |
Private Member Functions | |
| common::VoidResult | flush_batch_unsafe () |
| Flush batch without locking (caller must hold lock) | |
| bool | should_flush_by_size () const |
| Check if batch should be flushed based on size. | |
| bool | should_flush_by_time () const |
| Check if batch should be flushed based on time. | |
Private Attributes | |
| config | config_ |
| std::chrono::steady_clock::time_point | last_flush_time_ |
| batch_stats | stats_ |
Additional Inherited Members | |
Static Public Attributes inherited from kcenon::logger::decorator_writer_tag | |
| static constexpr writer_category | category = writer_category::decorator |
Static Public Attributes inherited from kcenon::logger::async_writer_tag | |
| static constexpr writer_category | category = writer_category::asynchronous |
Protected Member Functions inherited from kcenon::logger::queued_writer_base< std::vector< log_entry > > | |
| common::VoidResult | try_enqueue (const log_entry &entry) |
| Try to enqueue an entry with overflow protection. | |
| virtual common::VoidResult | handle_overflow (const log_entry &) |
| Handle queue overflow condition. | |
| virtual void | on_entry_enqueued () |
| Called after an entry is successfully enqueued. | |
Protected Member Functions inherited from kcenon::logger::decorator_writer_base | |
| log_writer_interface & | wrapped () noexcept |
| Access the wrapped writer (non-const) | |
| const log_writer_interface & | wrapped () const noexcept |
| Access the wrapped writer (const) | |
| const std::string & | decorator_name () const noexcept |
| Get the decorator name. | |
Static Protected Member Functions inherited from kcenon::logger::queued_writer_base< std::vector< log_entry > > | |
| static std::size_t | get_container_size (const std::queue< T > &container) |
| Get container size (specialization for std::queue) | |
| static std::size_t | get_container_size (const std::vector< T > &container) |
| Get container size (specialization for std::vector) | |
| static void | enqueue_entry (std::queue< T > &container, const log_entry &entry) |
| Enqueue entry (specialization for std::queue) | |
| static void | enqueue_entry (std::vector< T > &container, const log_entry &entry) |
| Enqueue entry (specialization for std::vector) | |
Protected Attributes inherited from kcenon::logger::queued_writer_base< std::vector< log_entry > > | |
| std::size_t | max_queue_size_ |
| std::mutex | queue_mutex_ |
| std::vector< log_entry > | queue_ |
| std::atomic< bool > | shutting_down_ |
Batch writer that accumulates log entries and writes them in batches.
This writer wraps another writer and accumulates log entries up to a configurable batch size or timeout, then writes them all at once. This reduces the number of I/O operations and improves performance. It extends queued_writer_base to share common queue management logic with async_writer.
Features:
Category: Asynchronous (batched I/O), Decorator (wraps another writer)
Definition at line 43 of file batch_writer.h.
|
private |
Definition at line 44 of file batch_writer.h.
|
explicit |
Construct a batch writer.
| underlying_writer | The actual writer to batch writes for |
| cfg | Batch configuration |
Definition at line 11 of file batch_writer.cpp.
References config_, kcenon::logger::batch_writer::config::max_batch_size, and kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_.
|
override |
Destructor - ensures all pending entries are flushed.
Definition at line 21 of file batch_writer.cpp.
References flush(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_, kcenon::logger::utils::safe_destructor_result_operation(), and kcenon::logger::queued_writer_base< std::vector< log_entry > >::shutting_down_.

|
overridevirtual |
Flush the batch to the underlying writer.
Implements kcenon::logger::log_writer_interface.
Definition at line 62 of file batch_writer.cpp.
References flush_batch_unsafe(), kcenon::logger::batch_writer::batch_stats::manual_flushes, kcenon::common::ok(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_, kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_mutex_, kcenon::logger::queued_writer_base< std::vector< log_entry > >::shutting_down_, and stats_.
Referenced by write(), and ~batch_writer().


|
private |
Flush batch without locking (caller must hold lock)
Definition at line 76 of file batch_writer.cpp.
References kcenon::logger::batch_writer::batch_stats::dropped_entries, kcenon::logger::log_writer_interface::flush(), last_flush_time_, kcenon::common::ok(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_, stats_, kcenon::logger::batch_writer::batch_stats::total_batches, kcenon::logger::decorator_writer_base::wrapped(), and kcenon::logger::log_writer_interface::write().
Referenced by flush().


| size_t kcenon::logger::batch_writer::get_current_batch_size | ( | ) | const |
Get current batch size.
Definition at line 118 of file batch_writer.cpp.
References kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_, and kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_mutex_.
|
overridevirtual |
Get writer name.
Implements kcenon::logger::log_writer_interface.
Definition at line 110 of file batch_writer.cpp.
References kcenon::logger::log_writer_interface::get_name(), and kcenon::logger::decorator_writer_base::wrapped().

|
inline |
Get batch statistics.
Definition at line 127 of file batch_writer.h.
|
overridevirtual |
Check if writer is healthy.
Implements kcenon::logger::log_writer_interface.
Definition at line 114 of file batch_writer.cpp.
References kcenon::logger::log_writer_interface::is_healthy(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::shutting_down_, and kcenon::logger::decorator_writer_base::wrapped().

| void kcenon::logger::batch_writer::reset_stats | ( | ) |
Reset batch statistics.
Definition at line 123 of file batch_writer.cpp.
References kcenon::logger::batch_writer::batch_stats::dropped_entries, kcenon::logger::batch_writer::batch_stats::flush_on_size, kcenon::logger::batch_writer::batch_stats::flush_on_timeout, kcenon::logger::batch_writer::batch_stats::manual_flushes, stats_, kcenon::logger::batch_writer::batch_stats::total_batches, and kcenon::logger::batch_writer::batch_stats::total_entries.
|
private |
Check if batch should be flushed based on size.
Definition at line 132 of file batch_writer.cpp.
References config_, kcenon::logger::batch_writer::config::max_batch_size, and kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_.
Referenced by write().

|
private |
Check if batch should be flushed based on time.
Definition at line 136 of file batch_writer.cpp.
References config_, kcenon::logger::batch_writer::config::flush_interval, and last_flush_time_.
|
overridevirtual |
Write a log entry to the batch.
| entry | The log entry to write |
Implements kcenon::logger::decorator_writer_base.
Definition at line 32 of file batch_writer.cpp.
References kcenon::logger::copy_log_entry(), flush(), kcenon::logger::batch_writer::batch_stats::flush_on_size, kcenon::logger::make_logger_void_result(), kcenon::common::ok(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_, kcenon::logger::queued_writer_base< std::vector< log_entry > >::queue_mutex_, kcenon::logger::queue_stopped, should_flush_by_size(), kcenon::logger::queued_writer_base< std::vector< log_entry > >::shutting_down_, stats_, and kcenon::logger::batch_writer::batch_stats::total_entries.

|
private |
Definition at line 155 of file batch_writer.h.
Referenced by batch_writer(), should_flush_by_size(), and should_flush_by_time().
|
private |
Definition at line 158 of file batch_writer.h.
Referenced by flush_batch_unsafe(), and should_flush_by_time().
|
mutableprivate |
Definition at line 161 of file batch_writer.h.
Referenced by flush(), flush_batch_unsafe(), reset_stats(), and write().