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

A console writer that counts messages per log level. More...

Inheritance diagram for counted_console_writer:
Inheritance graph
Collaboration diagram for counted_console_writer:
Collaboration graph

Public Member Functions

 counted_console_writer ()
 
std::string get_name () const override
 
size_t get_count (kcenon::common::interfaces::log_level level) const
 Get count for a specific log level.
 
size_t total_count () const
 Get total message count.
 
void print_stats () const
 Print statistics summary.
 
- Public Member Functions inherited from kcenon::logger::thread_safe_writer
 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 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

kcenon::common::VoidResult write_entry_impl (const log_entry &entry) override
 Implementation of structured write operation (override in derived classes)
 
kcenon::common::VoidResult flush_impl () override
 Implementation of flush operation (override in derived classes)
 
- Protected Member Functions inherited from kcenon::logger::thread_safe_writer
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::map< kcenon::common::interfaces::log_level, size_t > counts_
 

Detailed Description

A console writer that counts messages per log level.

Demonstrates extending thread_safe_writer for console output with statistics.

Examples
custom_writer_example.cpp.

Definition at line 121 of file custom_writer_example.cpp.

Constructor & Destructor Documentation

◆ counted_console_writer()

counted_console_writer::counted_console_writer ( )
inline
Examples
custom_writer_example.cpp.

Definition at line 123 of file custom_writer_example.cpp.

123 {
124 // Initialize counters for each log level
125 for (int i = 0; i <= static_cast<int>(kcenon::common::interfaces::log_level::trace); ++i) {
126 counts_[static_cast<kcenon::common::interfaces::log_level>(i)] = 0;
127 }
128 }
std::map< kcenon::common::interfaces::log_level, size_t > counts_

References counts_.

Member Function Documentation

◆ flush_impl()

kcenon::common::VoidResult counted_console_writer::flush_impl ( )
inlineoverrideprotectedvirtual

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

Implements kcenon::logger::thread_safe_writer.

Examples
custom_writer_example.cpp.

Definition at line 206 of file custom_writer_example.cpp.

206 {
207 std::cout.flush();
208 std::cerr.flush();
209 return kcenon::common::ok();
210 }
VoidResult ok()

References kcenon::common::ok().

Here is the call graph for this function:

◆ get_count()

size_t counted_console_writer::get_count ( kcenon::common::interfaces::log_level level) const
inline

Get count for a specific log level.

Examples
custom_writer_example.cpp.

Definition at line 137 of file custom_writer_example.cpp.

137 {
138 std::lock_guard<std::mutex> lock(get_mutex());
139 auto it = counts_.find(level);
140 return (it != counts_.end()) ? it->second : 0;
141 }
std::mutex & get_mutex() const
Access the writer mutex for extended operations.

References counts_, and kcenon::logger::thread_safe_writer::get_mutex().

Here is the call graph for this function:

◆ get_name()

std::string counted_console_writer::get_name ( ) const
inlineoverridevirtual

Implements kcenon::logger::base_writer.

Examples
custom_writer_example.cpp.

Definition at line 130 of file custom_writer_example.cpp.

130 {
131 return "counted_console";
132 }

◆ print_stats()

void counted_console_writer::print_stats ( ) const
inline

Print statistics summary.

Examples
custom_writer_example.cpp.

Definition at line 158 of file custom_writer_example.cpp.

158 {
159 std::lock_guard<std::mutex> lock(get_mutex());
160 std::cout << "\n=== Log Statistics ===" << std::endl;
161 std::cout << "Fatal: " << counts_.at(kcenon::common::interfaces::log_level::fatal) << std::endl;
162 std::cout << "Error: " << counts_.at(kcenon::common::interfaces::log_level::error) << std::endl;
163 std::cout << "Warning: " << counts_.at(kcenon::common::interfaces::log_level::warning) << std::endl;
164 std::cout << "Info: " << counts_.at(kcenon::common::interfaces::log_level::info) << std::endl;
165 std::cout << "Debug: " << counts_.at(kcenon::common::interfaces::log_level::debug) << std::endl;
166 std::cout << "Trace: " << counts_.at(kcenon::common::interfaces::log_level::trace) << std::endl;
167 }

References counts_, and kcenon::logger::thread_safe_writer::get_mutex().

Here is the call graph for this function:

◆ total_count()

size_t counted_console_writer::total_count ( ) const
inline

Get total message count.

Examples
custom_writer_example.cpp.

Definition at line 146 of file custom_writer_example.cpp.

146 {
147 std::lock_guard<std::mutex> lock(get_mutex());
148 size_t total = 0;
149 for (const auto& [level, count] : counts_) {
150 total += count;
151 }
152 return total;
153 }

References counts_, and kcenon::logger::thread_safe_writer::get_mutex().

Here is the call graph for this function:

◆ write_entry_impl()

kcenon::common::VoidResult counted_console_writer::write_entry_impl ( const log_entry & entry)
inlineoverrideprotectedvirtual

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

Implements kcenon::logger::thread_safe_writer.

Examples
custom_writer_example.cpp.

Definition at line 170 of file custom_writer_example.cpp.

171 {
172 // Convert log_level from logger_system to common::interfaces
173 auto level = static_cast<kcenon::common::interfaces::log_level>(static_cast<int>(entry.level));
174
175 // Increment counter for this level
176 counts_[level]++;
177
178 // Format log entry
179 std::string formatted = format_log_entry(entry);
180
181 // Output to console with color based on level
182 if (use_color()) {
183 switch (level) {
184 case kcenon::common::interfaces::log_level::fatal:
185 case kcenon::common::interfaces::log_level::error:
186 std::cerr << "\033[31m" << formatted << "\033[0m" << std::endl;
187 break;
188 case kcenon::common::interfaces::log_level::warning:
189 std::cout << "\033[33m" << formatted << "\033[0m" << std::endl;
190 break;
191 default:
192 std::cout << formatted << std::endl;
193 break;
194 }
195 } else {
196 if (level <= kcenon::common::interfaces::log_level::error) {
197 std::cerr << formatted << std::endl;
198 } else {
199 std::cout << formatted << std::endl;
200 }
201 }
202
203 return kcenon::common::ok();
204 }
std::string format_log_entry(const log_entry &entry) const
Format a log entry using the current formatter.
bool use_color() const
Get current color output setting.
log_level level
Severity level of the log message.
Definition log_entry.h:162

References counts_, kcenon::logger::base_writer::format_log_entry(), kcenon::logger::log_entry::level, kcenon::common::ok(), and kcenon::logger::base_writer::use_color().

Here is the call graph for this function:

Member Data Documentation

◆ counts_

std::map<kcenon::common::interfaces::log_level, size_t> counted_console_writer::counts_
mutableprivate

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