![]() |
LogIt++
Header-only, macro-first C++ logging library
|
Outputs log messages to the console with optional ANSI color support. More...
#include <ConsoleLogger.hpp>
Classes | |
| struct | Config |
| Configuration for the console logger. More... | |
| class | PendingEnqueue |
Public Member Functions | |
| ConsoleLogger () | |
| Default constructor that uses default configuration and std::cout. | |
| ConsoleLogger (const Config &config) | |
| Constructor with custom configuration and the default std::cout stream. | |
| ConsoleLogger (std::ostream &stream) | |
| Constructor with a custom primary output stream and default config. | |
| ConsoleLogger (std::ostream &stream, const Config &config) | |
| Constructor with a custom primary output stream and configuration. | |
| ConsoleLogger (const bool async) | |
| Constructor with asynchronous flag and std::cout. | |
| ConsoleLogger (std::ostream &stream, const bool async) | |
| Constructor with asynchronous flag and a custom primary output stream. | |
| ConsoleLogger (bool async, bool use_dedicated_executor, std::size_t queue_capacity=0, QueuePolicy queue_policy=QueuePolicy::Block) | |
| Constructor with asynchronous and dedicated executor options. | |
| ConsoleLogger (std::ostream &stream, bool async, bool use_dedicated_executor, std::size_t queue_capacity=0, QueuePolicy queue_policy=QueuePolicy::Block) | |
| Constructor with a custom primary output stream, async mode, and executor options. | |
| virtual | ~ConsoleLogger () |
| void | set_config (const Config &config) |
| Sets the logger configuration. | |
| Config | get_config () const |
| Gets the current logger configuration. | |
| void | log (const LogRecord &record, const std::string &message) override |
| Logs a message to the console with thread safety. | |
| std::string | get_string_param (const LoggerParam ¶m) const override |
| Retrieves a string parameter from the logger. | |
| int64_t | get_int_param (const LoggerParam ¶m) const override |
| Retrieves an integer parameter from the logger. | |
| double | get_float_param (const LoggerParam ¶m) const override |
| Retrieves a floating-point parameter from the logger. | |
| void | set_log_level (LogLevel level) override |
| Sets the minimal log level for this logger. | |
| LogLevel | get_log_level () const override |
| Gets the minimal log level for this logger. | |
| void | wait () override |
| Waits for all asynchronous tasks to complete. | |
| void | shutdown () override |
| Stops the dedicated executor after draining pending messages. | |
Public Member Functions inherited from logit::ILogger | |
| virtual | ~ILogger ()=default |
| virtual std::vector< std::string > | get_buffered_strings () const |
| Returns buffered formatted messages in chronological order. | |
| virtual std::vector< BufferedLogEntry > | get_buffered_entries () const |
| Returns buffered structured entries in chronological order. | |
| virtual std::vector< LogFileInfo > | list_log_files () const |
| Lists persisted log files owned by this backend. | |
| virtual LogFileReadResult | read_log_file (const std::string &path) const |
| Reads one persisted log file owned by this backend. | |
| virtual std::vector< LogFileReadResult > | read_log_files (const std::vector< std::string > &paths) const |
| Reads several persisted log files owned by this backend. | |
| virtual LogClearResult | clear_logs (const LogClearOptions &options=LogClearOptions()) |
| Clears logger-owned buffered or persisted records when supported. | |
Private Member Functions | |
| std::ostream * | select_stream_for (LogLevel level) const |
| Selects the target stream for a given log level. | |
| void | write_colored_message (std::ostream &stream, const std::string &message) const |
| Writes one formatted message to a stream, applying colors when supported. | |
| void | wait_for_pending_enqueues (std::unique_lock< std::mutex > &lock) |
| void | finish_pending_enqueue () |
| void | reset_color () |
| Resets the console text color to the default. | |
| int64_t | get_last_log_ts () const |
| Retrieves the timestamp of the last log. | |
| int64_t | get_time_since_last_log () const |
| Retrieves the time since the last log. | |
Static Private Member Functions | |
| static bool | is_standard_console_stream (const std::ostream &stream) |
| Returns true for streams whose console handle we may drive (std::cout, std::cerr). | |
| static void | configure_executor (const std::shared_ptr< detail::SingleThreadExecutor > &executor, const Config &config) |
| static Config | make_config (bool async, bool use_dedicated_executor, std::size_t queue_capacity, QueuePolicy queue_policy) |
Private Attributes | |
| std::mutex | m_mutex |
| Mutex to protect console output. | |
| Config | m_config |
| Configuration for the console logger. | |
| std::ostream * | m_stream |
| Primary output stream; lifetime owned by the caller. | |
| std::atomic< int64_t > | m_last_log_ts = ATOMIC_VAR_INIT(0) |
| std::atomic< int > | m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE)) |
| std::atomic< bool > | m_shutdown = ATOMIC_VAR_INIT(false) |
| std::shared_ptr< detail::SingleThreadExecutor > | m_executor |
| std::condition_variable | m_enqueue_cv |
| std::size_t | m_pending_enqueues = 0 |
Outputs log messages to the console with optional ANSI color support.
This logger supports synchronous and asynchronous logging. It also handles platform-specific differences in handling colored console output.
Key Features:
std::cout; can be redirected to any std::ostream& (e.g. std::cerr, a file stream). The lifetime of the target stream is owned by the caller.Config::routes: maps log level ranges to specific streams so diagnostics can be split from regular output. When routes is empty, every record is written to the primary stream. ANSI/Windows color support is preserved for std::cout and std::cerr; for any other stream, colors are disabled. Definition at line 72 of file ConsoleLogger.hpp.
|
inline |
Default constructor that uses default configuration and std::cout.
Definition at line 95 of file ConsoleLogger.hpp.
|
inline |
Constructor with custom configuration and the default std::cout stream.
| config | The configuration for the logger. |
Definition at line 101 of file ConsoleLogger.hpp.
|
inlineexplicit |
Constructor with a custom primary output stream and default config.
| stream | Output stream to write records to. Lifetime is owned by the caller. |
Definition at line 106 of file ConsoleLogger.hpp.
|
inline |
Constructor with a custom primary output stream and configuration.
| stream | Output stream to write records to. Lifetime is owned by the caller. |
| config | The configuration for the logger. |
Definition at line 114 of file ConsoleLogger.hpp.
|
inline |
Constructor with asynchronous flag and std::cout.
| async | Boolean flag for asynchronous logging. |
Definition at line 125 of file ConsoleLogger.hpp.
|
inline |
Constructor with asynchronous flag and a custom primary output stream.
| stream | Output stream to write records to. Lifetime is owned by the caller. |
| async | Boolean flag for asynchronous logging. |
Definition at line 133 of file ConsoleLogger.hpp.
|
inline |
Constructor with asynchronous and dedicated executor options.
Definition at line 139 of file ConsoleLogger.hpp.
|
inline |
Constructor with a custom primary output stream, async mode, and executor options.
Definition at line 151 of file ConsoleLogger.hpp.
|
inlinevirtual |
Definition at line 163 of file ConsoleLogger.hpp.
|
inlinestaticprivate |
Definition at line 420 of file ConsoleLogger.hpp.
|
inlineprivate |
Definition at line 460 of file ConsoleLogger.hpp.
|
inline |
Gets the current logger configuration.
Returns the logger's configuration with thread safety ensured.
Definition at line 195 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Retrieves a floating-point parameter from the logger.
| param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 308 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Retrieves an integer parameter from the logger.
| param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 295 of file ConsoleLogger.hpp.
|
inlineprivate |
Retrieves the timestamp of the last log.
Definition at line 633 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Gets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 326 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Retrieves a string parameter from the logger.
This function does not return parameters related to file-based loggers, such as LastFileName and LastFilePath.
| param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 282 of file ConsoleLogger.hpp.
|
inlineprivate |
Retrieves the time since the last log.
Definition at line 639 of file ConsoleLogger.hpp.
|
inlinestaticprivate |
Returns true for streams whose console handle we may drive (std::cout, std::cerr).
Custom streams skip ANSI/Windows coloring.
Definition at line 399 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Logs a message to the console with thread safety.
If asynchronous logging is enabled, the message is added to the task queue; otherwise, it is logged directly.
| record | The log record containing log information. |
| message | The formatted log message. |
Implements logit::ILogger.
Definition at line 207 of file ConsoleLogger.hpp.
|
inlinestaticprivate |
Definition at line 428 of file ConsoleLogger.hpp.
|
inlineprivate |
Resets the console text color to the default.
Only writes color escape codes for standard console streams (std::cout / std::cerr); custom streams stay plain.
Definition at line 619 of file ConsoleLogger.hpp.
|
inlineprivate |
Selects the target stream for a given log level.
Uses Config::routes when non-empty; falls back to the primary stream. Routes with kind == Custom and a null custom_stream are skipped, so the next route or the primary stream wins. Must be called under m_mutex.
Definition at line 380 of file ConsoleLogger.hpp.
|
inline |
Sets the logger configuration.
This method sets the logger's configuration and ensures thread safety with a mutex lock.
| config | The new configuration. |
Definition at line 170 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Sets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 321 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Stops the dedicated executor after draining pending messages.
Implements logit::ILogger.
Definition at line 346 of file ConsoleLogger.hpp.
|
inlineoverridevirtual |
Waits for all asynchronous tasks to complete.
If asynchronous logging is enabled, waits for all pending log messages to be written.
Implements logit::ILogger.
Definition at line 332 of file ConsoleLogger.hpp.
|
inlineprivate |
Definition at line 456 of file ConsoleLogger.hpp.
|
inlineprivate |
Writes one formatted message to a stream, applying colors when supported.
Must be called under m_mutex.
Definition at line 405 of file ConsoleLogger.hpp.
|
private |
Configuration for the console logger.
Definition at line 366 of file ConsoleLogger.hpp.
|
private |
Definition at line 372 of file ConsoleLogger.hpp.
|
private |
Definition at line 371 of file ConsoleLogger.hpp.
|
private |
Definition at line 368 of file ConsoleLogger.hpp.
|
private |
Definition at line 369 of file ConsoleLogger.hpp.
|
mutableprivate |
Mutex to protect console output.
Definition at line 365 of file ConsoleLogger.hpp.
|
private |
Definition at line 373 of file ConsoleLogger.hpp.
|
private |
Definition at line 370 of file ConsoleLogger.hpp.
|
private |
Primary output stream; lifetime owned by the caller.
Definition at line 367 of file ConsoleLogger.hpp.