Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::async::high_performance_async_writer Class Reference

High-performance asynchronous writer. More...

#include <high_performance_async_writer.h>

Inheritance diagram for kcenon::logger::async::high_performance_async_writer:
Inheritance graph
Collaboration diagram for kcenon::logger::async::high_performance_async_writer:
Collaboration graph

Classes

struct  config
 Configuration for the high-performance async writer. More...
 
struct  performance_stats
 Performance statistics. More...
 
struct  queued_log_entry
 Log entry optimized for high-performance queuing. More...
 

Public Member Functions

 high_performance_async_writer (log_writer_ptr wrapped_writer, const config &cfg=config{})
 Constructor.
 
 ~high_performance_async_writer () override
 Destructor.
 
bool start ()
 Start the async writer.
 
void stop (bool flush_remaining=true)
 Stop the async writer.
 
common::VoidResult write (const log_entry &entry) override
 Write a log entry asynchronously.
 
common::VoidResult flush () override
 Flush all pending messages.
 
bool is_healthy () const override
 Check if the writer is healthy.
 
std::string get_name () const override
 Get the name of this writer.
 
void set_use_color (bool use_color) override
 Set whether to use color output.
 
const performance_statsget_stats () const
 Get performance statistics.
 
void reset_stats ()
 Reset performance statistics.
 
double get_queue_utilization () const
 Get current queue utilization.
 
const batch_processor::processing_statsget_batch_stats () const
 Get batch processor statistics if enabled.
 
- Public Member Functions inherited from kcenon::logger::base_writer
 base_writer (std::unique_ptr< log_formatter_interface > formatter=nullptr)
 Constructor with optional formatter.
 
virtual ~base_writer ()=default
 
bool use_color () const
 Get current color output setting.
 
log_formatter_interfaceget_formatter () const
 Get the current formatter.
 
- 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 Member Functions

common::VoidResult write_direct (const log_entry &entry)
 Write directly to the underlying writer (fallback mode)
 
void update_stats (bool success, std::chrono::nanoseconds latency)
 Update performance statistics.
 
batch_processor::batch_entry to_batch_entry (const queued_log_entry &entry) const
 Convert to batch entry format.
 

Private Attributes

config config_
 
log_writer_ptr wrapped_writer_
 
std::unique_ptr< batch_processorbatch_processor_
 
std::unique_ptr< memory::object_pool< memory::log_entry_pool::pooled_log_entry > > memory_pool_
 
std::atomic< bool > running_ {false}
 
performance_stats stats_
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::logger::base_writer
std::string format_log_entry (const log_entry &entry) const
 Format a log entry using the current formatter.
 

Detailed Description

High-performance asynchronous writer.

This writer combines lock-free queuing, memory pooling, and batch processing to achieve maximum throughput while maintaining low latency.

Definition at line 36 of file high_performance_async_writer.h.

Constructor & Destructor Documentation

◆ high_performance_async_writer()

kcenon::logger::async::high_performance_async_writer::high_performance_async_writer ( log_writer_ptr wrapped_writer,
const config & cfg = config{} )
explicit

Constructor.

Parameters
wrapped_writerThe underlying writer to wrap
cfgConfiguration

Definition at line 10 of file high_performance_async_writer.cpp.

13 : config_(cfg)
14 , wrapped_writer_(std::move(wrapped_writer)) {
15
16 if (!wrapped_writer_) {
17 throw std::invalid_argument("Wrapped writer cannot be null");
18 }
19
20 // Initialize memory pool if enabled
22 memory_pool_ = std::make_unique<memory::object_pool<memory::log_entry_pool::pooled_log_entry>>(
24 }
25
26 // Initialize batch processor if enabled
31 }
32}
std::unique_ptr< memory::object_pool< memory::log_entry_pool::pooled_log_entry > > memory_pool_
std::unique_ptr< batch_processor > make_batch_processor(log_writer_ptr writer, const batch_processor::config &cfg)
Factory function to create a batch processor.
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
batch_processor::config batch_config
Batch processor configuration.
memory::object_pool< memory::log_entry_pool::pooled_log_entry >::config pool_config

References config_, kcenon::logger::async::high_performance_async_writer::config::enable_batch_processing, kcenon::logger::async::high_performance_async_writer::config::enable_memory_pooling, and wrapped_writer_.

◆ ~high_performance_async_writer()

kcenon::logger::async::high_performance_async_writer::~high_performance_async_writer ( )
override

Destructor.

Definition at line 34 of file high_performance_async_writer.cpp.

34 {
35 stop(true);
36}
void stop(bool flush_remaining=true)
Stop the async writer.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ flush()

common::VoidResult kcenon::logger::async::high_performance_async_writer::flush ( )
overridevirtual

Flush all pending messages.

Returns
common::VoidResult indicating success or error

Implements kcenon::logger::base_writer.

Definition at line 103 of file high_performance_async_writer.cpp.

103 {
104 if (batch_processor_) {
105 batch_processor_->flush();
106 } else if (wrapped_writer_) {
107 return wrapped_writer_->flush();
108 }
109 return common::ok();
110}
VoidResult ok()

References batch_processor_, kcenon::common::ok(), and wrapped_writer_.

Here is the call graph for this function:

◆ get_batch_stats()

const batch_processor::processing_stats * kcenon::logger::async::high_performance_async_writer::get_batch_stats ( ) const

Get batch processor statistics if enabled.

Returns
Pointer to batch processor stats, or nullptr if not enabled

Definition at line 144 of file high_performance_async_writer.cpp.

144 {
145 if (batch_processor_) {
146 return &batch_processor_->get_stats();
147 }
148 return nullptr;
149}

References batch_processor_.

◆ get_name()

std::string kcenon::logger::async::high_performance_async_writer::get_name ( ) const
overridevirtual

Get the name of this writer.

Returns
Writer name

Implements kcenon::logger::base_writer.

Definition at line 124 of file high_performance_async_writer.cpp.

124 {
125 const std::string base_name = wrapped_writer_ ? wrapped_writer_->get_name() : "unknown";
126 return "high_perf_async_" + base_name;
127}

References wrapped_writer_.

◆ get_queue_utilization()

double kcenon::logger::async::high_performance_async_writer::get_queue_utilization ( ) const

Get current queue utilization.

Returns
Queue utilization as percentage (0.0 to 1.0)

Definition at line 135 of file high_performance_async_writer.cpp.

135 {
136 if (batch_processor_) {
137 const auto queue_size = batch_processor_->get_queue_size();
138 const auto max_size = config_.queue_size;
139 return static_cast<double>(queue_size) / max_size;
140 }
141 return 0.0;
142}

References batch_processor_, config_, and kcenon::logger::async::high_performance_async_writer::config::queue_size.

◆ get_stats()

const performance_stats & kcenon::logger::async::high_performance_async_writer::get_stats ( ) const
inline

Get performance statistics.

Returns
Reference to performance statistics

Definition at line 165 of file high_performance_async_writer.h.

References stats_.

◆ is_healthy()

bool kcenon::logger::async::high_performance_async_writer::is_healthy ( ) const
overridevirtual

Check if the writer is healthy.

Returns
true if healthy, false otherwise

Reimplemented from kcenon::logger::base_writer.

Definition at line 112 of file high_performance_async_writer.cpp.

112 {
113 if (!running_.load(std::memory_order_relaxed)) {
114 return false;
115 }
116
117 if (batch_processor_) {
118 return batch_processor_->is_healthy();
119 }
120
121 return wrapped_writer_ && wrapped_writer_->is_healthy();
122}

References batch_processor_, running_, and wrapped_writer_.

◆ reset_stats()

void kcenon::logger::async::high_performance_async_writer::reset_stats ( )
inline

◆ set_use_color()

void kcenon::logger::async::high_performance_async_writer::set_use_color ( bool use_color)
overridevirtual

Set whether to use color output.

Parameters
use_colorEnable/disable color output

Reimplemented from kcenon::logger::base_writer.

Definition at line 129 of file high_performance_async_writer.cpp.

129 {
130 // Color setting is now handled by formatter, not writer interface
131 // This method is kept for backward compatibility but is deprecated
132 (void)use_color; // Suppress unused parameter warning
133}
bool use_color() const
Get current color output setting.

References kcenon::logger::base_writer::use_color().

Here is the call graph for this function:

◆ start()

bool kcenon::logger::async::high_performance_async_writer::start ( )

Start the async writer.

Returns
true if started successfully

Definition at line 38 of file high_performance_async_writer.cpp.

38 {
39 bool expected = false;
40 if (!running_.compare_exchange_strong(expected, true)) {
41 return false; // Already running
42 }
43
44 if (batch_processor_) {
45 return batch_processor_->start();
46 }
47
48 return true;
49}

References batch_processor_, and running_.

◆ stop()

void kcenon::logger::async::high_performance_async_writer::stop ( bool flush_remaining = true)

Stop the async writer.

Parameters
flush_remainingWhether to flush remaining messages

Definition at line 51 of file high_performance_async_writer.cpp.

51 {
52 if (!running_.exchange(false)) {
53 return; // Already stopped
54 }
55
56 if (batch_processor_) {
57 batch_processor_->stop(flush_remaining);
58 } else if (flush_remaining && wrapped_writer_) {
59 wrapped_writer_->flush();
60 }
61}

References batch_processor_, running_, and wrapped_writer_.

Referenced by ~high_performance_async_writer().

Here is the caller graph for this function:

◆ to_batch_entry()

◆ update_stats()

void kcenon::logger::async::high_performance_async_writer::update_stats ( bool success,
std::chrono::nanoseconds latency )
private

Update performance statistics.

Definition at line 167 of file high_performance_async_writer.cpp.

167 {
168 if (success) {
169 stats_.successful_writes.fetch_add(1, std::memory_order_relaxed);
170 }
171
172 // Update average latency using exponential moving average
173 const double latency_us = std::chrono::duration_cast<std::chrono::microseconds>(latency).count();
174 const double alpha = 0.1; // Smoothing factor
175 const double current_avg = stats_.average_latency_us.load(std::memory_order_relaxed);
176 const double new_avg = alpha * latency_us + (1.0 - alpha) * current_avg;
177 stats_.average_latency_us.store(new_avg, std::memory_order_relaxed);
178
179 // Update throughput
180 const auto now = std::chrono::steady_clock::now();
181 const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - stats_.start_time).count();
182 if (elapsed > 0) {
183 const auto total_writes = stats_.total_writes.load(std::memory_order_relaxed);
184 const double throughput = static_cast<double>(total_writes) / elapsed;
185 stats_.throughput_per_second.store(throughput, std::memory_order_relaxed);
186 }
187}

References kcenon::logger::async::high_performance_async_writer::performance_stats::average_latency_us, kcenon::logger::async::high_performance_async_writer::performance_stats::start_time, stats_, kcenon::logger::success, kcenon::logger::async::high_performance_async_writer::performance_stats::successful_writes, kcenon::logger::async::high_performance_async_writer::performance_stats::throughput_per_second, and kcenon::logger::async::high_performance_async_writer::performance_stats::total_writes.

Referenced by write(), and write_direct().

Here is the caller graph for this function:

◆ write()

common::VoidResult kcenon::logger::async::high_performance_async_writer::write ( const log_entry & entry)
overridevirtual

Write a log entry asynchronously.

Parameters
entryThe log entry to write
Returns
common::VoidResult indicating success or error
Since
3.5.0 Changed to use log_entry directly

Implements kcenon::logger::base_writer.

Definition at line 63 of file high_performance_async_writer.cpp.

63 {
64
65 const auto start_time = std::chrono::steady_clock::now();
66
67 if (!running_.load(std::memory_order_relaxed)) {
68 return write_direct(entry);
69 }
70
71 stats_.total_writes.fetch_add(1, std::memory_order_relaxed);
72
73 // Use batch processor if available
74 if (batch_processor_) {
75 // Convert log_level from logger_system to common::interfaces
76 auto level = static_cast<common::interfaces::log_level>(static_cast<int>(entry.level));
77
78 // Construct batch_entry directly with to_string() rvalues to avoid copies
79 batch_processor::batch_entry batch_entry(
80 level,
81 entry.message.to_string(),
82 entry.location ? entry.location->file.to_string() : std::string{},
83 entry.location ? entry.location->line : 0,
84 entry.location ? entry.location->function.to_string() : std::string{},
85 entry.timestamp);
86
87 if (batch_processor_->add_entry(std::move(batch_entry))) {
88 const auto end_time = std::chrono::steady_clock::now();
89 const auto latency = end_time - start_time;
90 update_stats(true, latency);
91 return common::ok();
92 } else {
93 stats_.dropped_writes.fetch_add(1, std::memory_order_relaxed);
94 // Fall back to direct write
95 return write_direct(entry);
96 }
97 }
98
99 // Direct async write (fallback mode)
100 return write_direct(entry);
101}
void update_stats(bool success, std::chrono::nanoseconds latency)
Update performance statistics.
common::VoidResult write_direct(const log_entry &entry)
Write directly to the underlying writer (fallback mode)

References batch_processor_, kcenon::logger::async::high_performance_async_writer::performance_stats::dropped_writes, kcenon::logger::log_entry::level, kcenon::logger::log_entry::location, kcenon::logger::log_entry::message, kcenon::common::ok(), running_, stats_, kcenon::logger::log_entry::timestamp, kcenon::logger::small_string< SSO_SIZE >::to_string(), kcenon::logger::async::high_performance_async_writer::performance_stats::total_writes, update_stats(), and write_direct().

Here is the call graph for this function:

◆ write_direct()

common::VoidResult kcenon::logger::async::high_performance_async_writer::write_direct ( const log_entry & entry)
private

Write directly to the underlying writer (fallback mode)

Definition at line 151 of file high_performance_async_writer.cpp.

151 {
152
153 if (!wrapped_writer_) {
154 return make_logger_void_result(logger_error_code::writer_not_available, "No wrapped writer available");
155 }
156
157 const auto start_time = std::chrono::steady_clock::now();
158 auto result = wrapped_writer_->write(entry);
159 const auto end_time = std::chrono::steady_clock::now();
160
161 const auto latency = end_time - start_time;
162 update_stats(result.is_ok(), latency);
163
164 return result;
165}
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")

References kcenon::logger::make_logger_void_result(), update_stats(), wrapped_writer_, and kcenon::logger::writer_not_available.

Referenced by write().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ batch_processor_

std::unique_ptr<batch_processor> kcenon::logger::async::high_performance_async_writer::batch_processor_
private

◆ config_

config kcenon::logger::async::high_performance_async_writer::config_
private

◆ memory_pool_

std::unique_ptr<memory::object_pool<memory::log_entry_pool::pooled_log_entry> > kcenon::logger::async::high_performance_async_writer::memory_pool_
private

Definition at line 237 of file high_performance_async_writer.h.

◆ running_

std::atomic<bool> kcenon::logger::async::high_performance_async_writer::running_ {false}
private

Definition at line 240 of file high_performance_async_writer.h.

240{false};

Referenced by is_healthy(), start(), stop(), and write().

◆ stats_

performance_stats kcenon::logger::async::high_performance_async_writer::stats_
mutableprivate

Definition at line 243 of file high_performance_async_writer.h.

Referenced by get_stats(), reset_stats(), update_stats(), and write().

◆ wrapped_writer_

log_writer_ptr kcenon::logger::async::high_performance_async_writer::wrapped_writer_
private

The documentation for this class was generated from the following files: