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

Coordinates formatting and output through a pipeline. More...

#include <composite_writer.h>

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

Public Member Functions

 composite_writer (std::unique_ptr< log_formatter_interface > formatter, std::unique_ptr< output_sink_interface > sink)
 Construct a composite writer.
 
 ~composite_writer () override
 Destructor.
 
common::VoidResult write (const log_entry &entry) override
 Write a log entry through the pipeline.
 
common::VoidResult flush () override
 Flush the sink.
 
std::string get_name () const override
 
bool is_healthy () const override
 Check if the writer is healthy.
 
log_formatter_interfaceget_formatter () const
 Get the formatter.
 
output_sink_interfaceget_sink () const
 Get the sink.
 
- 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 Attributes

std::unique_ptr< log_formatter_interfaceformatter_
 Formatting stage.
 
std::unique_ptr< output_sink_interfacesink_
 Output stage.
 

Additional Inherited Members

- Static Public Attributes inherited from kcenon::logger::composite_writer_tag
static constexpr writer_category category = writer_category::composite
 

Detailed Description

Coordinates formatting and output through a pipeline.

Implements the Composite and Pipeline patterns to create a flexible logging architecture. This writer:

  1. Accepts a log_entry
  2. Passes it to the formatter (format stage)
  3. Passes the formatted string to the sink (output stage)

Benefits:

  • Single Responsibility: Each component has one job
  • Open/Closed: Can add new formatters/sinks without modifying this class
  • Dependency Injection: Components are injected via constructor
  • Testability: Each component can be tested independently
  • Flexibility: Mix and match any formatter with any sink

Category: Composite (combines formatter and sink through Pipeline pattern)

Note
This class directly implements log_writer_interface (not base_writer) because it uses the Pipeline pattern which doesn't need legacy API compatibility.

Definition at line 77 of file composite_writer.h.

Constructor & Destructor Documentation

◆ composite_writer()

kcenon::logger::composite_writer::composite_writer ( std::unique_ptr< log_formatter_interface > formatter,
std::unique_ptr< output_sink_interface > sink )
inline

Construct a composite writer.

Parameters
formatterFormatter to use for converting log_entry to string
sinkSink to use for outputting formatted messages

Creates a writer that pipelines log entries through the provided formatter and sink.

Precondition
formatter must not be nullptr
sink must not be nullptr
Exceptions
std::invalid_argumentif formatter or sink is nullptr
Since
1.3.0
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 94 of file composite_writer.h.

96 : formatter_(std::move(formatter))
97 , sink_(std::move(sink))
98 {
99 if (!formatter_) {
100 throw std::invalid_argument("Formatter cannot be null");
101 }
102 if (!sink_) {
103 throw std::invalid_argument("Sink cannot be null");
104 }
105 }
std::unique_ptr< log_formatter_interface > formatter_
Formatting stage.
std::unique_ptr< output_sink_interface > sink_
Output stage.

References formatter_, and sink_.

◆ ~composite_writer()

kcenon::logger::composite_writer::~composite_writer ( )
inlineoverride

Destructor.

Ensures the sink is flushed before destruction.

Since
1.3.0
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 114 of file composite_writer.h.

114 {
115 flush(); // Ensure all data is written
116 }
common::VoidResult flush() override
Flush the sink.

References flush().

Here is the call graph for this function:

Member Function Documentation

◆ flush()

common::VoidResult kcenon::logger::composite_writer::flush ( )
inlineoverridevirtual

Flush the sink.

Returns
common::VoidResult Success or error code

Delegates flushing to the sink.

Since
1.3.0

Implements kcenon::logger::log_writer_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 147 of file composite_writer.h.

147 {
148 return sink_->flush();
149 }

References sink_.

Referenced by ~composite_writer().

Here is the caller graph for this function:

◆ get_formatter()

log_formatter_interface * kcenon::logger::composite_writer::get_formatter ( ) const
inline

Get the formatter.

Returns
Non-owning pointer to the formatter
Note
For inspection/configuration purposes only. Do not delete the returned pointer.
Since
1.3.0
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 185 of file composite_writer.h.

185 {
186 return formatter_.get();
187 }

References formatter_.

◆ get_name()

std::string kcenon::logger::composite_writer::get_name ( ) const
inlineoverridevirtual

Implements kcenon::logger::log_writer_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 159 of file composite_writer.h.

159 {
160 return formatter_->get_name() + "+" + sink_->get_name();
161 }

References formatter_, and sink_.

◆ get_sink()

output_sink_interface * kcenon::logger::composite_writer::get_sink ( ) const
inline

Get the sink.

Returns
Non-owning pointer to the sink
Note
For inspection/configuration purposes only. Do not delete the returned pointer.
Since
1.3.0
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 198 of file composite_writer.h.

198 {
199 return sink_.get();
200 }

References sink_.

◆ is_healthy()

bool kcenon::logger::composite_writer::is_healthy ( ) const
inlineoverridevirtual

Check if the writer is healthy.

Returns
true if the sink is healthy

A composite writer is healthy if its sink is healthy. Formatters are assumed to always be healthy.

Since
1.3.0

Implements kcenon::logger::log_writer_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 172 of file composite_writer.h.

172 {
173 return sink_->is_healthy();
174 }

References sink_.

◆ write()

common::VoidResult kcenon::logger::composite_writer::write ( const log_entry & entry)
inlineoverridevirtual

Write a log entry through the pipeline.

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

Pipeline stages:

  1. Format: entry -> formatter->format(entry) -> formatted_string
  2. Output: formatted_string -> sink->write_raw(formatted_string)
Note
Thread-safety depends on formatter and sink implementations.
Since
1.3.0

Implements kcenon::logger::log_writer_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/writers/composite_writer.h.

Definition at line 131 of file composite_writer.h.

131 {
132 // Stage 1: Format the log entry
133 std::string formatted = formatter_->format(entry);
134
135 // Stage 2: Write to sink
136 return sink_->write_raw(formatted);
137 }

References formatter_, and sink_.

Member Data Documentation

◆ formatter_

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

◆ sink_

std::unique_ptr<output_sink_interface> kcenon::logger::composite_writer::sink_
private

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