Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
high_performance_async_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
5#pragma once
6
15#include "lockfree_queue.h"
16#include "batch_processor.h"
22#include <memory>
23#include <thread>
24#include <atomic>
25#include <chrono>
26#include <functional> // Added for std::function usage
27
28namespace kcenon::logger::async {
29
37public:
41 struct config {
42 // Queue configuration
43 size_t queue_size{8192};
44
45 // Batch processing configuration
47
48 // Memory pool configuration
50
51 // Performance tuning
54
55 std::chrono::microseconds flush_timeout{1000};
56
58 // Optimize batch config for high performance
62 batch_config.max_wait_time = std::chrono::milliseconds{100};
65
66 // Optimize pool config
67 pool_config.initial_size = 200;
68 pool_config.max_size = 2000;
69 pool_config.allow_growth = true;
70 }
71 };
72
77 std::atomic<uint64_t> total_writes{0};
78 std::atomic<uint64_t> successful_writes{0};
79 std::atomic<uint64_t> dropped_writes{0};
80 std::atomic<uint64_t> queue_full_events{0};
81
82 std::atomic<double> average_latency_us{0.0};
83 std::atomic<double> throughput_per_second{0.0};
84
85 std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
86
87 double success_ratio() const {
88 const auto total = total_writes.load();
89 return total > 0 ? static_cast<double>(successful_writes.load()) / total : 0.0;
90 }
91
92 void reset() {
93 total_writes = 0;
99 start_time = std::chrono::steady_clock::now();
100 }
101 };
102
109 log_writer_ptr wrapped_writer,
110 const config& cfg = config{});
111
116
121 bool start();
122
127 void stop(bool flush_remaining = true);
128
135 common::VoidResult write(const log_entry& entry) override;
136
141 common::VoidResult flush() override;
142
147 bool is_healthy() const override;
148
153 std::string get_name() const override;
154
159 void set_use_color(bool use_color) override;
160
165 const performance_stats& get_stats() const { return stats_; }
166
171
176 double get_queue_utilization() const;
177
183
184private:
189 common::interfaces::log_level level;
190 std::string message;
191 std::string file;
192 int line;
193 std::string function;
194 std::chrono::system_clock::time_point timestamp;
195 std::chrono::steady_clock::time_point enqueue_time;
196
197 queued_log_entry() = default;
198
199 queued_log_entry(common::interfaces::log_level lvl,
200 std::string msg,
201 std::string f,
202 int l,
203 std::string func,
204 std::chrono::system_clock::time_point ts)
205 : level(lvl)
206 , message(std::move(msg))
207 , file(std::move(f))
208 , line(l)
209 , function(std::move(func))
210 , timestamp(ts)
211 , enqueue_time(std::chrono::steady_clock::now()) {}
212 };
213
218
222 void update_stats(bool success, std::chrono::nanoseconds latency);
223
227 batch_processor::batch_entry to_batch_entry(const queued_log_entry& entry) const;
228
229 // Configuration
231
232 // Underlying writer
234
235 // High-performance components
236 std::unique_ptr<batch_processor> batch_processor_;
237 std::unique_ptr<memory::object_pool<memory::log_entry_pool::pooled_log_entry>> memory_pool_;
238
239 // State management
240 std::atomic<bool> running_{false};
241
242 // Performance tracking
244};
245
252std::unique_ptr<high_performance_async_writer> make_high_performance_async_writer(
253 log_writer_ptr writer,
255
256} // namespace kcenon::logger::async
Abstract base class for all log output writers kcenon.
Optimized batch processing engine for log entries.
std::unique_ptr< memory::object_pool< memory::log_entry_pool::pooled_log_entry > > memory_pool_
void set_use_color(bool use_color) override
Set whether to use color output.
double get_queue_utilization() const
Get current queue utilization.
std::string get_name() const override
Get the name of this writer.
high_performance_async_writer(log_writer_ptr wrapped_writer, const config &cfg=config{})
Constructor.
bool is_healthy() const override
Check if the writer is healthy.
void update_stats(bool success, std::chrono::nanoseconds latency)
Update performance statistics.
common::VoidResult flush() override
Flush all pending messages.
const performance_stats & get_stats() const
Get performance statistics.
const batch_processor::processing_stats * get_batch_stats() const
Get batch processor statistics if enabled.
common::VoidResult write(const log_entry &entry) override
Write a log entry asynchronously.
void stop(bool flush_remaining=true)
Stop the async writer.
batch_processor::batch_entry to_batch_entry(const queued_log_entry &entry) const
Convert to batch entry format.
common::VoidResult write_direct(const log_entry &entry)
Write directly to the underlying writer (fallback mode)
Abstract base class for all log output writers.
Definition base_writer.h:95
bool use_color() const
Get current color output setting.
Error codes specific to the logger system.
High-performance lock-free queue implementation.
Data structures for representing log entries and source locations kcenon.
Log entry pool implementation for high-performance memory management.
std::unique_ptr< high_performance_async_writer > make_high_performance_async_writer(log_writer_ptr writer, const high_performance_async_writer::config &cfg)
Factory function to create a high-performance async writer.
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
Object pool implementation for high-performance memory management.
Batch entry structure.
Configuration for batch processor.
std::chrono::milliseconds max_wait_time
Maximum wait time.
bool enable_back_pressure
Enable back-pressure handling.
bool enable_dynamic_sizing
Enable dynamic batch sizing.
Configuration for the high-performance async writer.
batch_processor::config batch_config
Batch processor configuration.
memory::object_pool< memory::log_entry_pool::pooled_log_entry >::config pool_config
Log entry optimized for high-performance queuing.
std::string function
std::chrono::system_clock::time_point timestamp
queued_log_entry()=default
std::string file
int line
queued_log_entry(common::interfaces::log_level lvl, std::string msg, std::string f, int l, std::string func, std::chrono::system_clock::time_point ts)
std::chrono::steady_clock::time_point enqueue_time
For latency tracking.
common::interfaces::log_level level
std::string message
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Configuration for object pool.
Definition object_pool.h:34