13 :
base_type(std::move(underlying_writer), cfg.max_batch_size,
"batch")
15 , last_flush_time_(std::chrono::steady_clock::now()) {
38 bool should_flush =
false;
84 for (
const auto& entry :
queue_) {
95 if (flush_result.is_err() && last_result.is_ok()) {
96 last_result = flush_result;
137 auto now = std::chrono::steady_clock::now();
138 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
148 std::chrono::milliseconds flush_interval) {
154 return std::make_unique<batch_writer>(std::move(writer), cfg);
Batch writer that accumulates log entries and writes them in batches.
~batch_writer() override
Destructor - ensures all pending entries are flushed.
void reset_stats()
Reset batch statistics.
common::VoidResult flush_batch_unsafe()
Flush batch without locking (caller must hold lock)
batch_writer(log_writer_ptr underlying_writer, const config &cfg=config{})
Construct a batch writer.
bool is_healthy() const override
Check if writer is healthy.
std::chrono::steady_clock::time_point last_flush_time_
common::VoidResult flush() override
Flush the batch to the underlying writer.
std::string get_name() const override
Get writer name.
bool should_flush_by_size() const
Check if batch should be flushed based on size.
size_t get_current_batch_size() const
Get current batch size.
common::VoidResult write(const log_entry &entry) override
Write a log entry to the batch.
bool should_flush_by_time() const
Check if batch should be flushed based on time.
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.
std::vector< log_entry > queue_
std::atomic< bool > shutting_down_
Structured error context for debugging log system failures.
void safe_destructor_result_operation(const std::string &operation_name, F &&operation) noexcept
Safe operation with result for destructors.
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
std::unique_ptr< batch_writer > make_batch_writer(log_writer_ptr writer, size_t batch_size=100, std::chrono::milliseconds flush_interval=std::chrono::milliseconds{1000})
Factory function to create a batch writer.
log_entry copy_log_entry(const log_entry &entry)
Creates a copy of a log_entry for queue storage.
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
std::atomic< uint64_t > total_entries
std::atomic< uint64_t > manual_flushes
std::atomic< uint64_t > dropped_entries
std::atomic< uint64_t > flush_on_size
std::atomic< uint64_t > flush_on_timeout
std::atomic< uint64_t > total_batches
Configuration for batch writer.
std::chrono::milliseconds flush_interval
Represents a single log entry with all associated metadata.