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

Abstract base class for all log output writers. More...

#include <base_writer.h>

Inheritance diagram for kcenon::logger::base_writer:
Inheritance graph
Collaboration diagram for kcenon::logger::base_writer:
Collaboration graph

Public Member Functions

 base_writer (std::unique_ptr< log_formatter_interface > formatter=nullptr)
 Constructor with optional formatter.
 
virtual ~base_writer ()=default
 
common::VoidResult write (const log_entry &entry) override=0
 Write a log entry using the modern interface.
 
common::VoidResult flush () override=0
 Flush any buffered data.
 
virtual void set_use_color (bool use_color)
 Set whether to use color output (if supported)
 
bool use_color () const
 Get current color output setting.
 
virtual std::string get_name () const override=0
 
virtual bool is_healthy () const override
 Check if the writer is healthy and operational.
 
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.
 

Protected Member Functions

std::string format_log_entry (const log_entry &entry) const
 Format a log entry using the current formatter.
 

Private Attributes

std::unique_ptr< log_formatter_interfaceformatter_
 
bool use_color_ = true
 

Detailed Description

Abstract base class for all log output writers.

Writers are responsible for outputting log messages to various destinations. This class provides a compatibility layer between the old API and the new interface-based approach, implementing log_writer_interface and IMonitorable for observability (Phase 2.2.5).

Key responsibilities:

  • Format log messages for output
  • Handle destination-specific output logic
  • Manage buffering and flushing
  • Provide health status information
Note
All derived classes must implement the pure virtual methods.
For monitoring integration, use performance_monitor_adapter (Phase 3.3)
Warning
Writers used in async mode must be thread-safe.
Since
1.0.0

Definition at line 95 of file base_writer.h.

Constructor & Destructor Documentation

◆ base_writer()

kcenon::logger::base_writer::base_writer ( std::unique_ptr< log_formatter_interface > formatter = nullptr)
explicit

Constructor with optional formatter.

Parameters
formatterCustom formatter (default: timestamp_formatter)

If no formatter is provided, a default timestamp_formatter is created. This allows writers to use custom formatting strategies while maintaining backward compatibility.

Note
The formatter is immutable after construction for thread safety. To use a different formatter, create a new writer instance.
Since
1.2.0 (Phase 3)

Definition at line 18 of file base_writer.cpp.

19 : formatter_(std::move(formatter)) {
20 if (!formatter_) {
21 // Default to timestamp_formatter if none provided
22 formatter_ = std::make_unique<timestamp_formatter>();
23 }
24
25 // Apply color setting from legacy API to formatter
26 auto opts = formatter_->get_options();
27 opts.use_colors = use_color_;
28 formatter_->set_options(opts);
29}
std::unique_ptr< log_formatter_interface > formatter_

References formatter_, and use_color_.

◆ ~base_writer()

virtual kcenon::logger::base_writer::~base_writer ( )
virtualdefault

Member Function Documentation

◆ flush()

common::VoidResult kcenon::logger::base_writer::flush ( )
overridepure virtual

Flush any buffered data.

Returns
common::VoidResult indicating success or failure

Forces any buffered log entries to be written to the underlying output destination immediately. This method should be called before program termination or when immediate persistence is required.

Note
Some writers may perform synchronous I/O in this method, which could block for an extended period.

Implements kcenon::logger::log_writer_interface.

Implemented in kcenon::logger::async::high_performance_async_writer, kcenon::logger::critical_writer, kcenon::logger::hybrid_writer, kcenon::logger::network_writer, kcenon::logger::otlp_writer, and kcenon::logger::thread_safe_writer.

◆ format_log_entry()

std::string kcenon::logger::base_writer::format_log_entry ( const log_entry & entry) const
protected

Format a log entry using the current formatter.

Parameters
entryThe log entry to format
Returns
Formatted string ready for output

Uses the configured formatter to convert the log entry to a string. This is the recommended method for formatting in derived classes.

Note
Thread-safe if the formatter is thread-safe.
Since
1.2.0 (Phase 3)
Examples
custom_writer_example.cpp.

Definition at line 35 of file base_writer.cpp.

35 {
36 if (!formatter_) {
37 // Fallback if formatter is somehow null
38 return entry.message.to_string();
39 }
40 return formatter_->format(entry);
41}

References formatter_, kcenon::logger::log_entry::message, and kcenon::logger::small_string< SSO_SIZE >::to_string().

Referenced by counted_console_writer::write_entry_impl(), and memory_writer::write_entry_impl().

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

◆ get_formatter()

log_formatter_interface * kcenon::logger::base_writer::get_formatter ( ) const

Get the current formatter.

Returns
Pointer to current formatter (non-owning, read-only access)

The formatter is immutable after construction for thread safety. To use a different formatter, create a new writer instance.

Since
1.2.0 (Phase 3)

Definition at line 31 of file base_writer.cpp.

31 {
32 return formatter_.get();
33}

References formatter_.

◆ get_name()

◆ is_healthy()

virtual bool kcenon::logger::base_writer::is_healthy ( ) const
inlineoverridevirtual

Check if the writer is healthy and operational.

Returns
true if the writer is functioning correctly, false otherwise

Used for health monitoring and automatic failover. A writer might be unhealthy if its destination is unavailable (e.g., disk full, network disconnected).

Note
Default implementation always returns true. Override for writers that can detect failure conditions.
For monitoring integration, use performance_monitor_adapter (Phase 3.3)
Since
1.0.0

Implements kcenon::logger::log_writer_interface.

Reimplemented in kcenon::logger::async::high_performance_async_writer, kcenon::logger::critical_writer, kcenon::logger::hybrid_writer, and kcenon::logger::otlp_writer.

Definition at line 207 of file base_writer.h.

207{ return true; }

◆ set_use_color()

virtual void kcenon::logger::base_writer::set_use_color ( bool use_color)
inlinevirtual

Set whether to use color output (if supported)

Parameters
use_colortrue to enable color output, false to disable

Enables or disables ANSI color codes in formatted output. Only affects writers that output to terminals supporting color.

Note
Has no effect on writers that don't support color (e.g., file writers).
Since
1.0.0

Reimplemented in kcenon::logger::async::high_performance_async_writer, kcenon::logger::critical_writer, and kcenon::logger::hybrid_writer.

Definition at line 166 of file base_writer.h.

166 {
168 }
bool use_color() const
Get current color output setting.

◆ use_color()

bool kcenon::logger::base_writer::use_color ( ) const
inline

Get current color output setting.

Returns
true if color output is enabled, false otherwise
Since
1.0.0
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/critical_writer.h, and custom_writer_example.cpp.

Definition at line 176 of file base_writer.h.

176 {
177 return use_color_;
178 }

Referenced by kcenon::logger::async::high_performance_async_writer::set_use_color(), kcenon::logger::hybrid_writer::set_use_color(), and counted_console_writer::write_entry_impl().

Here is the caller graph for this function:

◆ write()

common::VoidResult kcenon::logger::base_writer::write ( const log_entry & entry)
overridepure virtual

Write a log entry using the modern interface.

Parameters
entryThe log entry to write
Returns
common::VoidResult Success or error code

This is the primary method that derived classes must implement. It receives a complete log_entry structure with all log information including structured fields, OpenTelemetry context, and category info.

Note
Derived classes should implement this method directly for optimal performance. The log_entry structure provides all necessary information.
Warning
This method may be called from multiple threads in async mode. Implementations must be thread-safe.
Since
1.0.0
3.5.0 Changed to pure virtual - implementations must override this

Implements kcenon::logger::log_writer_interface.

Implemented in kcenon::logger::async::high_performance_async_writer, kcenon::logger::critical_writer, kcenon::logger::hybrid_writer, kcenon::logger::network_writer, kcenon::logger::otlp_writer, and kcenon::logger::thread_safe_writer.

Member Data Documentation

◆ formatter_

std::unique_ptr<log_formatter_interface> kcenon::logger::base_writer::formatter_
private

Current formatter (owns the formatter)

Definition at line 237 of file base_writer.h.

Referenced by base_writer(), format_log_entry(), and get_formatter().

◆ use_color_

bool kcenon::logger::base_writer::use_color_ = true
private

Legacy color setting

Definition at line 240 of file base_writer.h.

Referenced by base_writer().


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