![]() |
LogIt++
|
Logs messages to files with date-based rotation and automatic deletion of old logs. More...
#include <FileLogger.hpp>
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 ¶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. | |
![]() | |
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 (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 ×tamp_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. | |
std::atomic< int > | m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE)) |
Logs messages to files with date-based rotation and automatic deletion of old logs.
This logger writes logs to files organized by date. It supports asynchronous logging and manages old files based on a configurable retention period.
Key Features:
Definition at line 59 of file FileLogger.hpp.
|
inline |
Default constructor that uses default configuration.
Definition at line 71 of file FileLogger.hpp.
|
inline |
Constructor with custom configuration.
config | The configuration for the logger. |
Definition at line 77 of file FileLogger.hpp.
|
inline |
Constructor with directory and asynchronous flag.
directory | The directory where log files will be stored. |
async | Boolean flag for asynchronous logging. |
auto_delete_days | Number of days after which old log files are deleted. |
Definition at line 85 of file FileLogger.hpp.
|
inlinevirtual |
Destructor to stop logging and close file.
Definition at line 96 of file FileLogger.hpp.
|
inlineprivate |
Creates a file path for the log file based on the date timestamp.
date_ts | The timestamp representing the date for the log file. |
Definition at line 261 of file FileLogger.hpp.
|
inlineprivate |
Gets the current timestamp in milliseconds.
Definition at line 348 of file FileLogger.hpp.
|
inlineprivate |
Gets the current UTC date timestamp in seconds.
Definition at line 342 of file FileLogger.hpp.
|
inlineprivate |
Extracts the date timestamp from the log filename.
filename | The filename to extract the date from. |
Definition at line 335 of file FileLogger.hpp.
|
inlineprivate |
Gets the full path to the logging directory.
Definition at line 229 of file FileLogger.hpp.
|
inlineoverridevirtual |
Retrieves a floating-point parameter from the logger.
param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 160 of file FileLogger.hpp.
|
inlineoverridevirtual |
Retrieves an integer parameter from the logger.
param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 147 of file FileLogger.hpp.
|
inlineprivate |
Retrieves the last log file name.
Definition at line 361 of file FileLogger.hpp.
|
inlineprivate |
Retrieves the last log file path.
Definition at line 354 of file FileLogger.hpp.
|
inlineprivate |
Retrieves the timestamp of the last log.
Definition at line 368 of file FileLogger.hpp.
|
inlineoverridevirtual |
Gets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 176 of file FileLogger.hpp.
|
inlineoverridevirtual |
Retrieves a string parameter from the logger.
param | The parameter type to retrieve. |
Implements logit::ILogger.
Definition at line 132 of file FileLogger.hpp.
|
inlineprivate |
Retrieves the time since the last log.
Definition at line 374 of file FileLogger.hpp.
|
inlineprivate |
Initializes the logging directory.
Definition at line 223 of file FileLogger.hpp.
|
inlineprivate |
Checks if the filename matches the log file naming pattern.
filename | The filename to check. |
Definition at line 327 of file FileLogger.hpp.
|
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.
record | The log record containing log information. |
message | The formatted log message. |
Implements logit::ILogger.
Definition at line 107 of file FileLogger.hpp.
|
inlineprivate |
Opens a new log file based on the provided date timestamp.
date_ts | The timestamp representing the date for the log file. |
Definition at line 239 of file FileLogger.hpp.
|
inlineprivate |
Removes old log files based on the auto-delete days configuration.
Definition at line 281 of file FileLogger.hpp.
|
inlineoverridevirtual |
Sets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 171 of file FileLogger.hpp.
|
inlineprivate |
Starts the logging process by initializing the file and directory.
Definition at line 198 of file FileLogger.hpp.
|
inlineprivate |
Stops the logging process by closing the file and waiting for tasks.
Definition at line 214 of file FileLogger.hpp.
|
inlineoverridevirtual |
Waits for all asynchronous tasks to complete.
Implements logit::ILogger.
Definition at line 181 of file FileLogger.hpp.
|
inlineprivate |
Writes a log message to the file.
message | The log message to write. |
timestamp_ms | The timestamp of the log message in milliseconds. |
Definition at line 269 of file FileLogger.hpp.
|
private |
Configuration for the file logger.
Definition at line 188 of file FileLogger.hpp.
|
private |
Timestamp of the current log file's date.
Definition at line 193 of file FileLogger.hpp.
|
private |
Output file stream for logging.
Definition at line 189 of file FileLogger.hpp.
|
private |
Name of the currently open log file.
Definition at line 192 of file FileLogger.hpp.
|
private |
Path of the currently open log file.
Definition at line 191 of file FileLogger.hpp.
|
mutableprivate |
Mutex to protect file path operations.
Definition at line 190 of file FileLogger.hpp.
|
private |
Timestamp of the last log.
Definition at line 194 of file FileLogger.hpp.
|
private |
Definition at line 195 of file FileLogger.hpp.
|
mutableprivate |
Mutex to protect file operations.
Definition at line 187 of file FileLogger.hpp.