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

Decorator that applies a formatter to a wrapped writer. More...

#include <formatted_writer.h>

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

Public Member Functions

 formatted_writer (std::unique_ptr< log_writer_interface > wrapped, std::unique_ptr< log_formatter_interface > formatter)
 Construct a formatted writer.
 
 ~formatted_writer () override=default
 Destructor.
 
 formatted_writer (const formatted_writer &)=delete
 
formatted_writeroperator= (const formatted_writer &)=delete
 
 formatted_writer (formatted_writer &&) noexcept=default
 
formatted_writeroperator= (formatted_writer &&) noexcept=default
 
common::VoidResult write (const log_entry &entry) override
 Write a log entry after applying the formatter.
 
std::string get_name () const override
 Get the name of this writer.
 
const log_formatter_interfaceget_formatter () const
 Get the current formatter.
 
- Public Member Functions inherited from kcenon::logger::decorator_writer_base
 decorator_writer_base (std::unique_ptr< log_writer_interface > wrapped, std::string_view decorator_name)
 Construct a decorator writer base.
 
 ~decorator_writer_base () override=default
 Virtual destructor for proper cleanup.
 
 decorator_writer_base (const decorator_writer_base &)=delete
 
decorator_writer_baseoperator= (const decorator_writer_base &)=delete
 
 decorator_writer_base (decorator_writer_base &&) noexcept=default
 
decorator_writer_baseoperator= (decorator_writer_base &&) noexcept=default
 
common::VoidResult flush () override
 Flush the wrapped writer.
 
bool is_healthy () const override
 Check if the writer is healthy.
 
const log_writer_interfaceget_wrapped_writer () const noexcept
 Get the wrapped writer (const version)
 
- 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_
 

Additional Inherited Members

- Static Public Attributes inherited from kcenon::logger::decorator_writer_tag
static constexpr writer_category category = writer_category::decorator
 
- Protected Member Functions inherited from kcenon::logger::decorator_writer_base
log_writer_interfacewrapped () noexcept
 Access the wrapped writer (non-const)
 
const log_writer_interfacewrapped () const noexcept
 Access the wrapped writer (const)
 
const std::string & decorator_name () const noexcept
 Get the decorator name.
 

Detailed Description

Decorator that applies a formatter to a wrapped writer.

This class implements the Decorator pattern for log writers. It wraps any log_writer_interface implementation and applies formatting logic before delegating write operations. The formatted message is stored in the log entry before passing it to the wrapped writer.

Key features:

Category: Synchronous (delegates to wrapped writer), Decorator

Note
The formatter is evaluated before each write operation.
If no formatter is provided, entries pass through unchanged.
Since
4.0.0
4.1.0 Migrated to use decorator_writer_base for code reuse

Definition at line 71 of file formatted_writer.h.

Constructor & Destructor Documentation

◆ formatted_writer() [1/3]

kcenon::logger::formatted_writer::formatted_writer ( std::unique_ptr< log_writer_interface > wrapped,
std::unique_ptr< log_formatter_interface > formatter )
explicit

Construct a formatted writer.

Parameters
wrappedThe writer to wrap with formatting
formatterThe formatter to apply before writing
Exceptions
std::invalid_argumentif wrapped is nullptr
Note
formatter can be nullptr, in which case all entries pass through unchanged
Since
4.0.0

Definition at line 13 of file formatted_writer.cpp.

15 : decorator_writer_base(std::move(wrapped), "formatted"), formatter_(std::move(formatter)) {
16 // Base class constructor handles nullptr check for wrapped writer
17}
decorator_writer_base(std::unique_ptr< log_writer_interface > wrapped, std::string_view decorator_name)
Construct a decorator writer base.
log_writer_interface & wrapped() noexcept
Access the wrapped writer (non-const)
std::unique_ptr< log_formatter_interface > formatter_

◆ ~formatted_writer()

kcenon::logger::formatted_writer::~formatted_writer ( )
overridedefault

Destructor.

◆ formatted_writer() [2/3]

kcenon::logger::formatted_writer::formatted_writer ( const formatted_writer & )
delete

◆ formatted_writer() [3/3]

kcenon::logger::formatted_writer::formatted_writer ( formatted_writer && )
defaultnoexcept

Member Function Documentation

◆ get_formatter()

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

Get the current formatter.

Returns
Pointer to the formatter (may be nullptr)
Since
4.0.0

Definition at line 50 of file formatted_writer.cpp.

50 {
51 return formatter_.get();
52}

References formatter_.

◆ get_name()

std::string kcenon::logger::formatted_writer::get_name ( ) const
overridevirtual

Get the name of this writer.

Returns
String in format "formatted_<wrapped_name>" or "formatted(<formatter_name>)_<wrapped_name>" if formatter has a name

Overrides base class to include formatter name when available.

Since
4.0.0

Reimplemented from kcenon::logger::decorator_writer_base.

Definition at line 42 of file formatted_writer.cpp.

42 {
43 if (formatter_) {
44 return "formatted(" + formatter_->get_name() + ")_" + wrapped().get_name();
45 }
46 // Use base class default format when no formatter
48}
std::string get_name() const override
Get the name of this writer.
virtual std::string get_name() const =0

References formatter_, kcenon::logger::decorator_writer_base::get_name(), kcenon::logger::log_writer_interface::get_name(), and kcenon::logger::decorator_writer_base::wrapped().

Here is the call graph for this function:

◆ operator=() [1/2]

formatted_writer & kcenon::logger::formatted_writer::operator= ( const formatted_writer & )
delete

◆ operator=() [2/2]

formatted_writer & kcenon::logger::formatted_writer::operator= ( formatted_writer && )
defaultnoexcept

◆ write()

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

Write a log entry after applying the formatter.

Parameters
entryThe log entry to write
Returns
common::VoidResult Success if written successfully, error if write operation fails

If the formatter is nullptr, the entry is delegated directly to the wrapped writer. Otherwise, the formatter is applied to create a formatted message, which is then stored in a new log entry with the same metadata.

Since
4.0.0

Implements kcenon::logger::decorator_writer_base.

Definition at line 19 of file formatted_writer.cpp.

19 {
20 // If no formatter, pass through all entries unchanged
21 if (!formatter_) {
22 return wrapped().write(entry);
23 }
24
25 // Apply formatter to get formatted message
26 std::string formatted_message = formatter_->format(entry);
27
28 // Create a new log entry with the formatted message
29 log_entry formatted_entry(entry.level, formatted_message, entry.timestamp);
30
31 // Copy optional fields from original entry
32 formatted_entry.location = entry.location;
33 formatted_entry.thread_id = entry.thread_id;
34 formatted_entry.category = entry.category;
35 formatted_entry.otel_ctx = entry.otel_ctx;
36 formatted_entry.fields = entry.fields;
37
38 // Delegate to wrapped writer
39 return wrapped().write(formatted_entry);
40}
virtual common::VoidResult write(const log_entry &entry)=0
Write a log entry.

References kcenon::logger::log_entry::category, kcenon::logger::log_entry::fields, formatter_, kcenon::logger::log_entry::level, kcenon::logger::log_entry::location, kcenon::logger::log_entry::otel_ctx, kcenon::logger::log_entry::thread_id, kcenon::logger::log_entry::timestamp, kcenon::logger::decorator_writer_base::wrapped(), and kcenon::logger::log_writer_interface::write().

Here is the call graph for this function:

Member Data Documentation

◆ formatter_

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

Definition at line 139 of file formatted_writer.h.

Referenced by get_formatter(), get_name(), and write().


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