Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
batch_writer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include "queued_writer_base.h"
15
17
18#include <vector>
19#include <chrono>
20
21namespace kcenon::logger {
22
43class LOGGER_SYSTEM_API batch_writer : public queued_writer_base<std::vector<log_entry>> {
45
46public:
50 struct config {
51 size_t max_batch_size; // Maximum entries before auto-flush
52 std::chrono::milliseconds flush_interval; // Auto-flush interval
53 bool preserve_order; // Maintain strict ordering
54
56 : max_batch_size(100)
57 , flush_interval(1000)
58 , preserve_order(true) {}
59 };
60
66 explicit batch_writer(log_writer_ptr underlying_writer,
67 const config& cfg = config{});
68
72 ~batch_writer() override;
73
80 common::VoidResult write(const log_entry& entry) override;
81
86 common::VoidResult flush() override;
87
92 std::string get_name() const override;
93
98 bool is_healthy() const override;
99
104 size_t get_current_batch_size() const;
105
109 struct batch_stats {
110 std::atomic<uint64_t> total_batches{0};
111 std::atomic<uint64_t> total_entries{0};
112 std::atomic<uint64_t> dropped_entries{0};
113 std::atomic<uint64_t> flush_on_size{0};
114 std::atomic<uint64_t> flush_on_timeout{0};
115 std::atomic<uint64_t> manual_flushes{0};
116
117 double average_batch_size() const {
118 return total_batches > 0 ?
119 static_cast<double>(total_entries) / total_batches : 0.0;
120 }
121 };
122
127 const batch_stats& get_stats() const { return stats_; }
128
132 void reset_stats();
133
134private:
135
140 common::VoidResult flush_batch_unsafe();
141
146 bool should_flush_by_size() const;
147
152 bool should_flush_by_time() const;
153
154 // Configuration
156
157 // Timing
158 std::chrono::steady_clock::time_point last_flush_time_;
159
160 // Statistics
162};
163
171std::unique_ptr<batch_writer> make_batch_writer(
172 log_writer_ptr writer,
173 size_t batch_size = 100,
174 std::chrono::milliseconds flush_interval = std::chrono::milliseconds{1000});
175
176} // namespace kcenon::logger
Batch writer that accumulates log entries and writes them in batches.
std::chrono::steady_clock::time_point last_flush_time_
const batch_stats & get_stats() const
Get batch statistics.
Abstract base class for queue-based log writers.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
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.
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
Base template for queue-based log writers.
Configuration for batch writer.
std::chrono::milliseconds flush_interval