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

Base class providing automatic thread-safety for writer implementations. More...

#include <thread_safe_writer.h>

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

Public Member Functions

 thread_safe_writer (std::unique_ptr< log_formatter_interface > formatter=nullptr)
 Constructor with optional formatter.
 
 ~thread_safe_writer () override=default
 Virtual destructor.
 
 thread_safe_writer (const thread_safe_writer &)=delete
 
thread_safe_writeroperator= (const thread_safe_writer &)=delete
 
 thread_safe_writer (thread_safe_writer &&)=delete
 
thread_safe_writeroperator= (thread_safe_writer &&)=delete
 
common::VoidResult write (const log_entry &entry) final
 Thread-safe write operation for structured log entries.
 
common::VoidResult flush () final
 Thread-safe flush operation.
 
- 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
 
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

virtual common::VoidResult write_entry_impl (const log_entry &entry)=0
 Implementation of structured write operation (override in derived classes)
 
virtual common::VoidResult flush_impl ()=0
 Implementation of flush operation (override in derived classes)
 
std::mutex & get_mutex () const
 Access the writer mutex for extended operations.
 
- 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.
 

Private Attributes

std::mutex write_mutex_
 

Detailed Description

Base class providing automatic thread-safety for writer implementations.

This class uses the Template Method pattern to provide automatic mutex-based synchronization. Public methods acquire a lock and delegate to protected virtual *_impl methods that derived classes override.

Benefits:

  • Eliminates mutex boilerplate from derived classes
  • Consistent locking strategy across all writers
  • Derived classes focus only on output-specific logic
  • Thread-safety cannot be accidentally omitted

Thread-safety guarantees:

  • All public write/flush operations are serialized
  • Derived class *_impl methods are called while holding the lock
  • The mutex is always released, even on exceptions (RAII)
Note
Non-copyable and non-movable (contains std::mutex)
For high-performance scenarios requiring lock-free operation, use high_performance_async_writer instead.
Since
1.3.0
Examples
custom_writer_example.cpp.

Definition at line 81 of file thread_safe_writer.h.

Constructor & Destructor Documentation

◆ thread_safe_writer() [1/3]

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

Constructor with optional formatter.

Parameters
formatterCustom formatter (default: timestamp_formatter)
Since
1.3.0

Definition at line 10 of file thread_safe_writer.cpp.

11 : base_writer(std::move(formatter)) {
12}
base_writer(std::unique_ptr< log_formatter_interface > formatter=nullptr)
Constructor with optional formatter.

◆ ~thread_safe_writer()

kcenon::logger::thread_safe_writer::~thread_safe_writer ( )
overridedefault

Virtual destructor.

◆ thread_safe_writer() [2/3]

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

◆ thread_safe_writer() [3/3]

kcenon::logger::thread_safe_writer::thread_safe_writer ( thread_safe_writer && )
delete

Member Function Documentation

◆ flush()

common::VoidResult kcenon::logger::thread_safe_writer::flush ( )
finalvirtual

Thread-safe flush operation.

Returns
common::VoidResult Success or error code

Acquires the mutex and delegates to flush_impl(). This method is final to ensure thread-safety is not bypassed.

Since
1.3.0

Implements kcenon::logger::base_writer.

Definition at line 19 of file thread_safe_writer.cpp.

19 {
20 std::lock_guard<std::mutex> lock(write_mutex_);
21 return flush_impl();
22}
virtual common::VoidResult flush_impl()=0
Implementation of flush operation (override in derived classes)

References flush_impl(), and write_mutex_.

Here is the call graph for this function:

◆ flush_impl()

virtual common::VoidResult kcenon::logger::thread_safe_writer::flush_impl ( )
protectedpure virtual

Implementation of flush operation (override in derived classes)

Returns
common::VoidResult Success or error code

Called by flush() while holding the mutex. Derived classes implement their output-specific flush logic here.

Note
The mutex is held when this method is called.
Do not call public methods on 'this' (would cause deadlock).
Since
1.3.0

Implemented in counted_console_writer, and memory_writer.

Referenced by flush().

Here is the caller graph for this function:

◆ get_mutex()

std::mutex & kcenon::logger::thread_safe_writer::get_mutex ( ) const
inlineprotected

Access the writer mutex for extended operations.

Returns
Reference to the internal mutex

Allows derived classes to use the same mutex for additional thread-safe operations beyond write/flush. Use with caution to avoid deadlocks.

Warning
Only use for operations that cannot be performed within write_impl() or flush_impl().
Since
1.3.0
Examples
custom_writer_example.cpp.

Definition at line 174 of file thread_safe_writer.h.

174{ return write_mutex_; }

Referenced by memory_writer::clear(), counted_console_writer::get_count(), memory_writer::get_entries(), counted_console_writer::print_stats(), memory_writer::size(), and counted_console_writer::total_count().

Here is the caller graph for this function:

◆ operator=() [1/2]

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

◆ operator=() [2/2]

thread_safe_writer & kcenon::logger::thread_safe_writer::operator= ( thread_safe_writer && )
delete

◆ write()

common::VoidResult kcenon::logger::thread_safe_writer::write ( const log_entry & entry)
finalvirtual

Thread-safe write operation for structured log entries.

Parameters
entryThe log entry to write (includes structured fields)
Returns
common::VoidResult Success or error code

Acquires the mutex and delegates to write_entry_impl(). This method preserves all log entry data including structured fields, OpenTelemetry context, and category information.

Note
This is the primary method for writing logs. It preserves all structured logging information.
Since
3.4.0
3.5.0 This is now the only write method (legacy API removed)

Implements kcenon::logger::base_writer.

Definition at line 14 of file thread_safe_writer.cpp.

14 {
15 std::lock_guard<std::mutex> lock(write_mutex_);
16 return write_entry_impl(entry);
17}
virtual common::VoidResult write_entry_impl(const log_entry &entry)=0
Implementation of structured write operation (override in derived classes)

References write_entry_impl(), and write_mutex_.

Here is the call graph for this function:

◆ write_entry_impl()

virtual common::VoidResult kcenon::logger::thread_safe_writer::write_entry_impl ( const log_entry & entry)
protectedpure virtual

Implementation of structured write operation (override in derived classes)

Parameters
entryThe log entry to write (includes structured fields)
Returns
common::VoidResult Success or error code

Called by write(const log_entry&) while holding the mutex. Derived classes must override this method to implement their output logic.

Note
The mutex is held when this method is called.
Do not call public methods on 'this' (would cause deadlock).
Since
3.4.0
3.5.0 Now pure virtual - implementations must override this

Implemented in counted_console_writer, and memory_writer.

Referenced by write().

Here is the caller graph for this function:

Member Data Documentation

◆ write_mutex_

std::mutex kcenon::logger::thread_safe_writer::write_mutex_
mutableprivate

Mutex for thread-safe write/flush operations

Definition at line 178 of file thread_safe_writer.h.

Referenced by flush(), and write().


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