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

Singleton class that manages multiple loggers and formatters. More...

#include <Logger.hpp>

Classes

struct  LoggerStrategy
 Structure to hold a logger-formatter pair. More...
 

Public Member Functions

void wait ()
 Waits for all asynchronous loggers to finish processing.
 
void add_logger (std::unique_ptr< ILogger > logger, std::unique_ptr< ILogFormatter > formatter, const bool &single_mode=false)
 Adds a logger and its corresponding formatter.
 
void log (const LogRecord &record)
 Logs a LogRecord using all added loggers and formatters.
 
std::string get_string_param (const int &logger_index, const LoggerParam &param) const
 Retrieves a string parameter from the logger.
 
int64_t get_int_param (const int &logger_index, const LoggerParam &param) const
 Retrieves an integer parameter from the logger.
 
double get_float_param (const int &logger_index, const LoggerParam &param) const
 Retrieves a floating-point parameter from the logger.
 
template<typename... Ts>
auto log_and_return (const LogRecord &record, Ts &&... args) -> decltype(std::forward_as_tuple(std::forward< Ts >(args)...))
 Logs the message and returns a tuple of arguments.
 
template<typename T >
auto log_and_return (const LogRecord &record, T &&arg) -> decltype(arg)
 Logs the message and returns a single argument.
 
auto log_and_return (const LogRecord &record) -> std::tuple<>
 Logs a message without arguments and returns an empty tuple.
 

Static Public Member Functions

static Loggerget_instance ()
 Retrieves the singleton instance of Logger.
 

Private Member Functions

template<typename... Ts>
void print (const LogRecord &record, Ts const &... args)
 Logs a record with the given arguments.
 
 Logger ()=default
 
 ~Logger ()
 
 Logger (const Logger &)=delete
 
Loggeroperator= (const Logger &)=delete
 
 Logger (Logger &&)=delete
 
Loggeroperator= (Logger &&)=delete
 

Private Attributes

std::vector< LoggerStrategym_loggers
 Container for logger-formatter pairs.
 
std::mutex m_mutex
 Mutex for thread safety during logging operations.
 

Detailed Description

Singleton class that manages multiple loggers and formatters.

The Logger class allows adding multiple logger and formatter pairs (strategies) and provides methods to log messages using these strategies. It supports both synchronous and asynchronous logging. The class is thread-safe.

Definition at line 21 of file Logger.hpp.

Constructor & Destructor Documentation

◆ Logger() [1/3]

logit::Logger::Logger ( )
privatedefault

◆ ~Logger()

logit::Logger::~Logger ( )
inlineprivate

Definition at line 172 of file Logger.hpp.

◆ Logger() [2/3]

logit::Logger::Logger ( const Logger & )
privatedelete

◆ Logger() [3/3]

logit::Logger::Logger ( Logger && )
privatedelete

Member Function Documentation

◆ add_logger()

void logit::Logger::add_logger ( std::unique_ptr< ILogger > logger,
std::unique_ptr< ILogFormatter > formatter,
const bool & single_mode = false )
inline

Adds a logger and its corresponding formatter.

Parameters
loggerA unique pointer to the logger instance.
formatterA unique pointer to the formatter instance.
single_modeIf true, this logger will only be invoked by specific log macros (e.g., LOGIT_TRACE_TO) that explicitly target it using the logger's index. It will not process logs from general log macros (e.g., LOGIT_TRACE).

Definition at line 47 of file Logger.hpp.

◆ get_float_param()

double logit::Logger::get_float_param ( const int & logger_index,
const LoggerParam & param ) const
inline

Retrieves a floating-point parameter from the logger.

Parameters
logger_indexThe index of the logger.
paramThe logger parameter to retrieve.
Returns
A double representing the requested parameter, or 0.0 if the parameter is unsupported.

Definition at line 103 of file Logger.hpp.

◆ get_instance()

static Logger & logit::Logger::get_instance ( )
inlinestatic

Retrieves the singleton instance of Logger.

Returns
A reference to the single instance of the Logger class.

Definition at line 26 of file Logger.hpp.

◆ get_int_param()

int64_t logit::Logger::get_int_param ( const int & logger_index,
const LoggerParam & param ) const
inline

Retrieves an integer parameter from the logger.

Parameters
logger_indexThe index of the logger.
paramThe logger parameter to retrieve.
Returns
An integer representing the requested parameter, or 0 if the parameter is unsupported.

Definition at line 91 of file Logger.hpp.

◆ get_string_param()

std::string logit::Logger::get_string_param ( const int & logger_index,
const LoggerParam & param ) const
inline

Retrieves a string parameter from the logger.

Parameters
logger_indexThe index of the logger.
paramThe logger parameter to retrieve.
Returns
A string representing the requested parameter, or an empty string if the parameter is unsupported.

Definition at line 79 of file Logger.hpp.

◆ log()

void logit::Logger::log ( const LogRecord & record)
inline

Logs a LogRecord using all added loggers and formatters.

The log() method formats the log message using each logger's corresponding formatter and sends the formatted message to each logger.

Parameters
recordThe log record to be logged.

Definition at line 61 of file Logger.hpp.

◆ log_and_return() [1/3]

auto logit::Logger::log_and_return ( const LogRecord & record) -> std::tuple<>
inline

Logs a message without arguments and returns an empty tuple.

Parameters
recordThe log record.
Returns
An empty tuple.

Definition at line 136 of file Logger.hpp.

◆ log_and_return() [2/3]

template<typename T >
auto logit::Logger::log_and_return ( const LogRecord & record,
T && arg ) -> decltype(arg)
inline

Logs the message and returns a single argument.

Template Parameters
TType of the argument.
Parameters
recordThe log record.
argThe argument to be logged.
Returns
The logged argument.

Definition at line 128 of file Logger.hpp.

◆ log_and_return() [3/3]

template<typename... Ts>
auto logit::Logger::log_and_return ( const LogRecord & record,
Ts &&... args ) -> decltype(std::forward_as_tuple(std::forward<Ts>(args)...))
inline

Logs the message and returns a tuple of arguments.

Template Parameters
TsTypes of the arguments.
Parameters
recordThe log record.
argsThe arguments to be logged.
Returns
A tuple containing the arguments.

Definition at line 117 of file Logger.hpp.

◆ operator=() [1/2]

Logger & logit::Logger::operator= ( const Logger & )
privatedelete

◆ operator=() [2/2]

Logger & logit::Logger::operator= ( Logger && )
privatedelete

◆ print()

template<typename... Ts>
void logit::Logger::print ( const LogRecord & record,
Ts const &... args )
inlineprivate

Logs a record with the given arguments.

Template Parameters
TsTypes of the arguments.
Parameters
recordThe log record.
argsThe arguments to be logged.

Definition at line 159 of file Logger.hpp.

◆ wait()

void logit::Logger::wait ( )
inline

Waits for all asynchronous loggers to finish processing.

This method ensures that all log messages are fully processed before continuing. It calls the wait() function of each logger.

Definition at line 35 of file Logger.hpp.

Member Data Documentation

◆ m_loggers

std::vector<LoggerStrategy> logit::Logger::m_loggers
private

Container for logger-formatter pairs.

Definition at line 151 of file Logger.hpp.

◆ m_mutex

std::mutex logit::Logger::m_mutex
private

Mutex for thread safety during logging operations.

Definition at line 152 of file Logger.hpp.


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