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

Outputs log messages to console (stdout or stderr) More...

#include <console_sink.h>

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

Public Member Functions

 console_sink (bool use_stderr=false, bool auto_flush=false)
 Construct a console sink.
 
 ~console_sink () override
 Destructor - ensures all data is flushed.
 
common::VoidResult write_raw (std::string_view message) override
 Write a pre-formatted message to console.
 
common::VoidResult flush () override
 Flush buffered output to console.
 
bool is_healthy () const override
 Check if console is healthy.
 
std::string get_name () const override
 Get the name of this sink.
 
std::string get_info () const override
 Get sink information.
 
void set_use_stderr (bool use_stderr)
 Set whether to use stderr.
 
void set_auto_flush (bool auto_flush)
 Set whether to auto-flush after every write.
 
- Public Member Functions inherited from kcenon::logger::output_sink_interface
virtual ~output_sink_interface ()=default
 

Private Attributes

bool use_stderr_
 Write to stderr instead of stdout.
 
bool auto_flush_
 Flush after every write.
 
std::mutex mutex_
 Protects console output.
 

Detailed Description

Outputs log messages to console (stdout or stderr)

Thread-safe console output sink. Accepts pre-formatted messages and writes them to stdout or stderr without any modification.

Features:

  • Thread-safe output (mutex-protected)
  • Configurable output stream (stdout vs stderr)
  • Automatic flushing support
  • Always healthy (console is always available)
Note
This sink does NOT add timestamps, colors, or any formatting. All formatting must be done by a formatter before calling write_raw().
Thread Safety:
All methods are thread-safe.
Examples
composite_writer_example.cpp.

Definition at line 52 of file console_sink.h.

Constructor & Destructor Documentation

◆ console_sink()

kcenon::logger::console_sink::console_sink ( bool use_stderr = false,
bool auto_flush = false )
inlineexplicit

Construct a console sink.

Parameters
use_stderrIf true, write to stderr; if false, write to stdout
auto_flushIf true, flush after every write

Creates a console sink that writes to the specified output stream.

Note
stderr is typically used for error messages, stdout for normal output. Auto-flushing ensures immediate output but may reduce performance.
Since
1.3.0

Definition at line 66 of file console_sink.h.

67 : use_stderr_(use_stderr)
68 , auto_flush_(auto_flush)
69 {}
bool auto_flush_
Flush after every write.
bool use_stderr_
Write to stderr instead of stdout.

◆ ~console_sink()

kcenon::logger::console_sink::~console_sink ( )
inlineoverride

Destructor - ensures all data is flushed.

Since
1.3.0

Definition at line 76 of file console_sink.h.

76 {
77 flush(); // Ensure all data is written
78 }
common::VoidResult flush() override
Flush buffered output to console.

References flush().

Here is the call graph for this function:

Member Function Documentation

◆ flush()

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

Flush buffered output to console.

Returns
common::VoidResult Success

Forces any buffered data to be written to the console immediately.

Note
This method is thread-safe.
Since
1.3.0

Implements kcenon::logger::output_sink_interface.

Definition at line 123 of file console_sink.h.

123 {
124 std::lock_guard<std::mutex> lock(mutex_);
125
126 if (use_stderr_) {
127 std::cerr.flush();
128 } else {
129 std::cout.flush();
130 }
131
132 return common::ok(); // Success
133 }
std::mutex mutex_
Protects console output.
VoidResult ok()

References mutex_, kcenon::common::ok(), and use_stderr_.

Referenced by ~console_sink().

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

◆ get_info()

std::string kcenon::logger::console_sink::get_info ( ) const
inlineoverridevirtual

Get sink information.

Returns
Diagnostic string

Reimplemented from kcenon::logger::output_sink_interface.

Definition at line 168 of file console_sink.h.

168 {
169 std::string info = use_stderr_ ? "stderr" : "stdout";
170 if (auto_flush_) {
171 info += ", auto-flush";
172 }
173 return info;
174 }

References auto_flush_, and use_stderr_.

◆ get_name()

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

Get the name of this sink.

Returns
"console"
Since
1.3.0

Implements kcenon::logger::output_sink_interface.

Definition at line 156 of file console_sink.h.

156 {
157 return "console";
158 }

◆ is_healthy()

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

Check if console is healthy.

Returns
true (console is always available)

Console output is always available, so this always returns true.

Note
In rare cases (redirected to closed pipe), writes might fail, but the sink itself is still considered healthy.
Since
1.3.0

Implements kcenon::logger::output_sink_interface.

Definition at line 146 of file console_sink.h.

146 {
147 return true; // Console is always available
148 }

◆ set_auto_flush()

void kcenon::logger::console_sink::set_auto_flush ( bool auto_flush)
inline

Set whether to auto-flush after every write.

Parameters
auto_flushIf true, flush after every write
Note
This method is thread-safe.
Since
1.3.0

Definition at line 197 of file console_sink.h.

197 {
198 std::lock_guard<std::mutex> lock(mutex_);
199 auto_flush_ = auto_flush;
200 }

References auto_flush_, and mutex_.

◆ set_use_stderr()

void kcenon::logger::console_sink::set_use_stderr ( bool use_stderr)
inline

Set whether to use stderr.

Parameters
use_stderrIf true, write to stderr; if false, write to stdout
Note
This method is thread-safe.
Since
1.3.0

Definition at line 184 of file console_sink.h.

184 {
185 std::lock_guard<std::mutex> lock(mutex_);
186 use_stderr_ = use_stderr;
187 }

References mutex_, and use_stderr_.

◆ write_raw()

common::VoidResult kcenon::logger::console_sink::write_raw ( std::string_view message)
inlineoverridevirtual

Write a pre-formatted message to console.

Parameters
messagePre-formatted message (must include newline if desired)
Returns
common::VoidResult Success (console writes rarely fail)

Writes the message to stdout or stderr without modification. The message should already be fully formatted with timestamps, colors, etc.

Note
This method is thread-safe.
Warning
If the message doesn't end with '
', the next message will continue on the same line. Formatters should include newlines.
Since
1.3.0

Implements kcenon::logger::output_sink_interface.

Definition at line 95 of file console_sink.h.

95 {
96 std::lock_guard<std::mutex> lock(mutex_);
97
98 if (use_stderr_) {
99 std::cerr << message;
100 if (auto_flush_) {
101 std::cerr.flush();
102 }
103 } else {
104 std::cout << message;
105 if (auto_flush_) {
106 std::cout.flush();
107 }
108 }
109
110 return common::ok(); // Success
111 }

References auto_flush_, mutex_, kcenon::common::ok(), and use_stderr_.

Here is the call graph for this function:

Member Data Documentation

◆ auto_flush_

bool kcenon::logger::console_sink::auto_flush_
private

Flush after every write.

Definition at line 204 of file console_sink.h.

Referenced by get_info(), set_auto_flush(), and write_raw().

◆ mutex_

std::mutex kcenon::logger::console_sink::mutex_
mutableprivate

Protects console output.

Definition at line 205 of file console_sink.h.

Referenced by flush(), set_auto_flush(), set_use_stderr(), and write_raw().

◆ use_stderr_

bool kcenon::logger::console_sink::use_stderr_
private

Write to stderr instead of stdout.

Definition at line 203 of file console_sink.h.

Referenced by flush(), get_info(), set_use_stderr(), and write_raw().


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