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

Logger that outputs log messages to files with date-based rotation and old file deletion. More...

#include <FileLogger.hpp>

Inheritance diagram for logit::FileLogger:
logit::ILogger

Classes

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

Public Member Functions

 FileLogger ()
 Default constructor that uses default configuration.
 
 FileLogger (const Config &config)
 Constructor with custom configuration.
 
 FileLogger (const std::string &directory, const bool &async=true, const int &auto_delete_days=30)
 Constructor with directory and asynchronous flag.
 
virtual ~FileLogger ()
 Destructor to stop logging and close file.
 
void log (const LogRecord &record, const std::string &message) override
 Logs a message to a file 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.
 
- Public Member Functions inherited from logit::ILogger
virtual ~ILogger ()=default
 

Private Member Functions

void start_logging ()
 Starts the logging process by initializing the file and directory.
 
void stop_logging ()
 Stops the logging process by closing the file and waiting for tasks.
 
void initialize_directory ()
 Initializes the logging directory.
 
std::string get_directory_path () const
 Gets the full path to the logging directory.
 
void open_log_file (const int64_t &date_ts)
 Opens a new log file based on the provided date timestamp.
 
std::string create_file_path (const int64_t &date_ts) const
 Creates a file path for the log file based on the date timestamp.
 
void write_log (const std::string &message, const int64_t &timestamp_ms)
 Writes a log message to the file.
 
void remove_old_logs ()
 Removes old log files based on the auto-delete days configuration.
 
bool is_valid_log_filename (const std::string &filename) const
 Checks if the filename matches the log file naming pattern.
 
int64_t get_date_ts_from_filename (const std::string &filename) const
 Extracts the date timestamp from the log filename.
 
int64_t get_current_utc_date_ts () const
 Gets the current UTC date timestamp in seconds.
 
int64_t current_timestamp_ms () const
 Gets the current timestamp in milliseconds.
 
std::string get_last_log_file_path () const
 Retrieves the last log file path.
 
std::string get_last_log_file_name () const
 Retrieves the last log file name.
 
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 file operations.
 
Config m_config
 Configuration for the file logger.
 
std::ofstream m_file
 Output file stream for logging.
 
std::mutex m_file_path_mutex
 Mutex to protect file path operations.
 
std::string m_file_path
 Path of the currently open log file.
 
std::string m_file_name
 Name of the currently open log file.
 
int64_t m_current_date_ts = 0
 Timestamp of the current log file's date.
 
std::atomic< int64_t > m_last_log_ts = ATOMIC_VAR_INIT(0)
 Timestamp of the last log.
 

Detailed Description

Logger that outputs log messages to files with date-based rotation and old file deletion.

The FileLogger provides synchronous or asynchronous logging to files. It writes logs to files named by date in a configurable directory. When the UTC day changes, it creates a new log file. It also deletes log files older than a specified number of days.

Definition at line 27 of file FileLogger.hpp.

Constructor & Destructor Documentation

◆ FileLogger() [1/3]

logit::FileLogger::FileLogger ( )
inline

Default constructor that uses default configuration.

Definition at line 39 of file FileLogger.hpp.

◆ FileLogger() [2/3]

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

Constructor with custom configuration.

Parameters
configThe configuration for the logger.

Definition at line 45 of file FileLogger.hpp.

◆ FileLogger() [3/3]

logit::FileLogger::FileLogger ( const std::string & directory,
const bool & async = true,
const int & auto_delete_days = 30 )
inline

Constructor with directory and asynchronous flag.

Parameters
directoryThe directory where log files will be stored.
asyncBoolean flag for asynchronous logging.
auto_delete_daysNumber of days after which old log files are deleted.

Definition at line 53 of file FileLogger.hpp.

◆ ~FileLogger()

virtual logit::FileLogger::~FileLogger ( )
inlinevirtual

Destructor to stop logging and close file.

Definition at line 64 of file FileLogger.hpp.

Member Function Documentation

◆ create_file_path()

std::string logit::FileLogger::create_file_path ( const int64_t & date_ts) const
inlineprivate

Creates a file path for the log file based on the date timestamp.

Parameters
date_tsThe timestamp representing the date for the log file.
Returns
The path to the log file.

Definition at line 210 of file FileLogger.hpp.

◆ current_timestamp_ms()

int64_t logit::FileLogger::current_timestamp_ms ( ) const
inlineprivate

Gets the current timestamp in milliseconds.

Returns
The current timestamp in milliseconds.

Definition at line 288 of file FileLogger.hpp.

◆ get_current_utc_date_ts()

int64_t logit::FileLogger::get_current_utc_date_ts ( ) const
inlineprivate

Gets the current UTC date timestamp in seconds.

Returns
The current UTC date timestamp in seconds.

Definition at line 282 of file FileLogger.hpp.

◆ get_date_ts_from_filename()

int64_t logit::FileLogger::get_date_ts_from_filename ( const std::string & filename) const
inlineprivate

Extracts the date timestamp from the log filename.

Parameters
filenameThe filename to extract the date from.
Returns
The date timestamp.

Definition at line 275 of file FileLogger.hpp.

◆ get_directory_path()

std::string logit::FileLogger::get_directory_path ( ) const
inlineprivate

Gets the full path to the logging directory.

Returns
The path to the logging directory.

Definition at line 186 of file FileLogger.hpp.

◆ get_float_param()

double logit::FileLogger::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 128 of file FileLogger.hpp.

◆ get_int_param()

int64_t logit::FileLogger::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 115 of file FileLogger.hpp.

◆ get_last_log_file_name()

std::string logit::FileLogger::get_last_log_file_name ( ) const
inlineprivate

Retrieves the last log file name.

Returns
The last log file name.

Definition at line 301 of file FileLogger.hpp.

◆ get_last_log_file_path()

std::string logit::FileLogger::get_last_log_file_path ( ) const
inlineprivate

Retrieves the last log file path.

Returns
The last log file path.

Definition at line 294 of file FileLogger.hpp.

◆ get_last_log_ts()

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

Retrieves the timestamp of the last log.

Returns
The last log timestamp.

Definition at line 308 of file FileLogger.hpp.

◆ get_string_param()

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

Retrieves a string parameter from the logger.

Parameters
paramThe parameter type to retrieve.
Returns
A string representing the requested parameter.

Implements logit::ILogger.

Definition at line 100 of file FileLogger.hpp.

◆ get_time_since_last_log()

int64_t logit::FileLogger::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 314 of file FileLogger.hpp.

◆ initialize_directory()

void logit::FileLogger::initialize_directory ( )
inlineprivate

Initializes the logging directory.

Definition at line 180 of file FileLogger.hpp.

◆ is_valid_log_filename()

bool logit::FileLogger::is_valid_log_filename ( const std::string & filename) const
inlineprivate

Checks if the filename matches the log file naming pattern.

Parameters
filenameThe filename to check.
Returns
True if the filename matches the pattern, false otherwise.

Definition at line 267 of file FileLogger.hpp.

◆ log()

void logit::FileLogger::log ( const LogRecord & record,
const std::string & message )
inlineoverridevirtual

Logs a message to a file 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 75 of file FileLogger.hpp.

◆ open_log_file()

void logit::FileLogger::open_log_file ( const int64_t & date_ts)
inlineprivate

Opens a new log file based on the provided date timestamp.

Parameters
date_tsThe timestamp representing the date for the log file.

Definition at line 192 of file FileLogger.hpp.

◆ remove_old_logs()

void logit::FileLogger::remove_old_logs ( )
inlineprivate

Removes old log files based on the auto-delete days configuration.

Definition at line 230 of file FileLogger.hpp.

◆ start_logging()

void logit::FileLogger::start_logging ( )
inlineprivate

Starts the logging process by initializing the file and directory.

Definition at line 155 of file FileLogger.hpp.

◆ stop_logging()

void logit::FileLogger::stop_logging ( )
inlineprivate

Stops the logging process by closing the file and waiting for tasks.

Definition at line 171 of file FileLogger.hpp.

◆ wait()

void logit::FileLogger::wait ( )
inlineoverridevirtual

Waits for all asynchronous tasks to complete.

Implements logit::ILogger.

Definition at line 139 of file FileLogger.hpp.

◆ write_log()

void logit::FileLogger::write_log ( const std::string & message,
const int64_t & timestamp_ms )
inlineprivate

Writes a log message to the file.

Parameters
messageThe log message to write.
timestamp_msThe timestamp of the log message in milliseconds.

Definition at line 218 of file FileLogger.hpp.

Member Data Documentation

◆ m_config

Config logit::FileLogger::m_config
private

Configuration for the file logger.

Definition at line 146 of file FileLogger.hpp.

◆ m_current_date_ts

int64_t logit::FileLogger::m_current_date_ts = 0
private

Timestamp of the current log file's date.

Definition at line 151 of file FileLogger.hpp.

◆ m_file

std::ofstream logit::FileLogger::m_file
private

Output file stream for logging.

Definition at line 147 of file FileLogger.hpp.

◆ m_file_name

std::string logit::FileLogger::m_file_name
private

Name of the currently open log file.

Definition at line 150 of file FileLogger.hpp.

◆ m_file_path

std::string logit::FileLogger::m_file_path
private

Path of the currently open log file.

Definition at line 149 of file FileLogger.hpp.

◆ m_file_path_mutex

std::mutex logit::FileLogger::m_file_path_mutex
mutableprivate

Mutex to protect file path operations.

Definition at line 148 of file FileLogger.hpp.

◆ m_last_log_ts

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

Timestamp of the last log.

Definition at line 152 of file FileLogger.hpp.

◆ m_mutex

std::mutex logit::FileLogger::m_mutex
mutableprivate

Mutex to protect file operations.

Definition at line 145 of file FileLogger.hpp.


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