LogIt++
Loading...
Searching...
No Matches
logit::ConsoleLogger Class Reference

Outputs log messages to the console with optional ANSI color support. More...

#include <ConsoleLogger.hpp>

Inheritance diagram for logit::ConsoleLogger:
logit::ILogger

Classes

struct  Config
 Configuration for the console logger. More...
 

Public Member Functions

 ConsoleLogger ()
 Default constructor that uses default configuration.
 
 ConsoleLogger (const Config &config)
 Constructor with custom configuration.
 
 ConsoleLogger (const bool async)
 Constructor with asynchronous flag.
 
virtual ~ConsoleLogger ()=default
 
void set_config (const Config &config)
 Sets the logger configuration. This method sets the logger's configuration and ensures thread safety with a mutex lock.
 
Config get_config ()
 Gets the current logger configuration. Returns the logger's configuration with thread safety ensured.
 
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 &param) const override
 Retrieves a string parameter from the logger.
 
int64_t get_int_param (const LoggerParam &param) const override
 Retrieves an integer parameter from the logger.
 
double get_float_param (const LoggerParam &param) const override
 Retrieves a floating-point parameter from the logger.
 
void wait () override
 Waits for all asynchronous tasks to complete. If asynchronous logging is enabled, waits for all pending log messages to be written.
 
- Public Member Functions inherited from logit::ILogger
virtual ~ILogger ()=default
 

Private Types

enum class  WinColor {
  Black = 0 , DarkBlue = 1 , DarkGreen = 2 , DarkCyan = 3 ,
  DarkRed = 4 , DarkMagenta = 5 , DarkYellow = 6 , Gray = 7 ,
  DarkGray = 8 , Blue = 9 , Green = 10 , Cyan = 11 ,
  Red = 12 , Magenta = 13 , Yellow = 14 , White = 15
}
 

Private Member Functions

void handle_ansi_colors_windows (const std::string &message) const
 Handle ANSI color codes in the message for Windows console.
 
void apply_color_from_ansi_code (const std::string &ansi_code, HANDLE handle_stdout) const
 Apply color based on ANSI code for Windows console.
 
WinColor text_color_to_win_color (const TextColor &color) const
 Convert TextColor to WinColor.
 
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.
 

Private Attributes

std::mutex m_mutex
 Mutex to protect console output.
 
Config m_config
 Configuration for the console logger.
 
std::atomic< int64_t > m_last_log_ts = ATOMIC_VAR_INIT(0)
 

Detailed Description

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:

  • Cross-platform color support (ANSI on Linux/macOS, Windows-specific handling).
  • Thread-safe logging.
  • Synchronous or asynchronous operation.

Definition at line 28 of file ConsoleLogger.hpp.

Member Enumeration Documentation

◆ WinColor

enum class logit::ConsoleLogger::WinColor
strongprivate
Enumerator
Black 
DarkBlue 
DarkGreen 
DarkCyan 
DarkRed 
DarkMagenta 
DarkYellow 
Gray 
DarkGray 
Blue 
Green 
Cyan 
Red 
Magenta 
Yellow 
White 

Definition at line 167 of file ConsoleLogger.hpp.

Constructor & Destructor Documentation

◆ ConsoleLogger() [1/3]

logit::ConsoleLogger::ConsoleLogger ( )
inline

Default constructor that uses default configuration.

Definition at line 39 of file ConsoleLogger.hpp.

◆ ConsoleLogger() [2/3]

logit::ConsoleLogger::ConsoleLogger ( const Config & config)
inline

Constructor with custom configuration.

Parameters
configThe configuration for the logger.

Definition at line 45 of file ConsoleLogger.hpp.

◆ ConsoleLogger() [3/3]

logit::ConsoleLogger::ConsoleLogger ( const bool async)
inline

Constructor with asynchronous flag.

Parameters
asyncBoolean flag for asynchronous logging.

Definition at line 51 of file ConsoleLogger.hpp.

◆ ~ConsoleLogger()

virtual logit::ConsoleLogger::~ConsoleLogger ( )
virtualdefault

Member Function Documentation

◆ apply_color_from_ansi_code()

void logit::ConsoleLogger::apply_color_from_ansi_code ( const std::string & ansi_code,
HANDLE handle_stdout ) const
inlineprivate

Apply color based on ANSI code for Windows console.

Parameters
ansi_codeThe ANSI code string.
handle_stdoutThe console handle.

Definition at line 227 of file ConsoleLogger.hpp.

◆ get_config()

Config logit::ConsoleLogger::get_config ( )
inline

Gets the current logger configuration. Returns the logger's configuration with thread safety ensured.

Returns
The current configuration.

Definition at line 69 of file ConsoleLogger.hpp.

◆ get_float_param()

double logit::ConsoleLogger::get_float_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieves a floating-point parameter from the logger.

Parameters
paramThe parameter type to retrieve.
Returns
A double representing the requested parameter, or 0.0 if the parameter is unsupported.

Implements logit::ILogger.

Definition at line 140 of file ConsoleLogger.hpp.

◆ get_int_param()

int64_t logit::ConsoleLogger::get_int_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieves an integer parameter from the logger.

Parameters
paramThe parameter type to retrieve.
Returns
An integer representing the requested parameter, or 0 if the parameter is unsupported.

Implements logit::ILogger.

Definition at line 127 of file ConsoleLogger.hpp.

◆ get_last_log_ts()

int64_t logit::ConsoleLogger::get_last_log_ts ( ) const
inlineprivate

Retrieves the timestamp of the last log.

Returns
The last log timestamp.

Definition at line 292 of file ConsoleLogger.hpp.

◆ get_string_param()

std::string logit::ConsoleLogger::get_string_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieves a string parameter from the logger.

This function does not return parameters related to file-based loggers, such as LastFileName and LastFilePath.

Parameters
paramThe parameter type to retrieve.
Returns
A string representing the requested parameter, or an empty string if the parameter is unsupported.

Implements logit::ILogger.

Definition at line 114 of file ConsoleLogger.hpp.

◆ get_time_since_last_log()

int64_t logit::ConsoleLogger::get_time_since_last_log ( ) const
inlineprivate

Retrieves the time since the last log.

Returns
The time in milliseconds since the last log.

Definition at line 298 of file ConsoleLogger.hpp.

◆ handle_ansi_colors_windows()

void logit::ConsoleLogger::handle_ansi_colors_windows ( const std::string & message) const
inlineprivate

Handle ANSI color codes in the message for Windows console.

Parameters
messageThe message containing ANSI color codes.

Definition at line 188 of file ConsoleLogger.hpp.

◆ log()

void logit::ConsoleLogger::log ( const LogRecord & record,
const std::string & message )
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.

Parameters
recordThe log record containing log information.
messageThe formatted log message.

Implements logit::ILogger.

Definition at line 81 of file ConsoleLogger.hpp.

◆ reset_color()

void logit::ConsoleLogger::reset_color ( )
inlineprivate

Resets the console text color to the default.

Definition at line 282 of file ConsoleLogger.hpp.

◆ set_config()

void logit::ConsoleLogger::set_config ( const Config & config)
inline

Sets the logger configuration. This method sets the logger's configuration and ensures thread safety with a mutex lock.

Parameters
configThe new configuration.

Definition at line 61 of file ConsoleLogger.hpp.

◆ text_color_to_win_color()

WinColor logit::ConsoleLogger::text_color_to_win_color ( const TextColor & color) const
inlineprivate

Convert TextColor to WinColor.

Parameters
colorThe TextColor value.
Returns
Corresponding WinColor value.

Definition at line 258 of file ConsoleLogger.hpp.

◆ wait()

void logit::ConsoleLogger::wait ( )
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 152 of file ConsoleLogger.hpp.

Member Data Documentation

◆ m_config

Config logit::ConsoleLogger::m_config
private

Configuration for the console logger.

Definition at line 161 of file ConsoleLogger.hpp.

◆ m_last_log_ts

std::atomic<int64_t> logit::ConsoleLogger::m_last_log_ts = ATOMIC_VAR_INIT(0)
private

Definition at line 162 of file ConsoleLogger.hpp.

◆ m_mutex

std::mutex logit::ConsoleLogger::m_mutex
mutableprivate

Mutex to protect console output.

Definition at line 160 of file ConsoleLogger.hpp.


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