![]() |
LogIt++
|
Writes each log message to a unique file with automatic cleanup. More...
#include <UniqueFileLogger.hpp>
Classes | |
struct | Config |
Configuration for the unique file logger. More... | |
struct | ThreadLogInfo |
Public Member Functions | |
UniqueFileLogger () | |
Default constructor that uses default configuration. | |
UniqueFileLogger (const Config &config) | |
Constructor with custom configuration. | |
UniqueFileLogger (const std::string &directory, bool async=true, int auto_delete_days=30, size_t hash_length=8) | |
Constructor with directory and asynchronous flag. | |
virtual | ~UniqueFileLogger () |
void | log (const LogRecord &record, const std::string &message) override |
Logs a message to a unique 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 directory and removing old logs. | |
void | stop_logging () |
Stops the logging process by waiting for tasks. | |
void | initialize_directory () |
Initializes the logging directory. | |
std::string | get_directory_path () const |
Gets the full path to the logging directory. | |
std::string | write_log (const std::string &message, const int64_t ×tamp_ms) |
Writes a log message to a unique file. | |
std::string | create_unique_file_path (const int64_t ×tamp_ms) const |
Creates a unique file path based on the timestamp and a hash. | |
std::string | format_timestamp (const int64_t ×tamp_ms) const |
Formats the timestamp into a string with date and time. | |
std::string | generate_fixed_length_hash (size_t length) const |
Generates a fixed-length hash string. | |
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_timestamp_from_filename (const std::string &filename) const |
Extracts the timestamp from the filename. | |
int64_t | current_timestamp_ms () const |
Gets the current timestamp in milliseconds. | |
std::string | get_last_log_file_name () const |
Retrieves the last log file name for the calling thread. | |
std::string | get_last_log_file_path () const |
Retrieves the last log file path for the calling thread. | |
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 elapsed since the last log. | |
Private Attributes | |
std::mutex | m_mutex |
Mutex to protect file operations. | |
Config | m_config |
Configuration for the unique file logger. | |
std::mutex | m_thread_log_info_mutex |
Mutex to protect access to thread log information. | |
std::condition_variable | m_pending_logs_cv |
Condition variable to wait for pending logs to finish. | |
std::unordered_map< std::thread::id, ThreadLogInfo > | m_thread_log_info |
Map to store log information per thread. | |
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)) |
Writes each log message to a unique file with automatic cleanup.
This logger generates a unique file for each log message. The filename includes a timestamp and a hash to ensure uniqueness. It also deletes old log files after a specified number of days.
Key Features:
Definition at line 66 of file UniqueFileLogger.hpp.
|
inline |
Default constructor that uses default configuration.
Definition at line 79 of file UniqueFileLogger.hpp.
|
inline |
Constructor with custom configuration.
config | The configuration for the logger. |
Definition at line 85 of file UniqueFileLogger.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. |
hash_length | Length of the hash used in filenames. |
Definition at line 94 of file UniqueFileLogger.hpp.
|
inlinevirtual |
Definition at line 106 of file UniqueFileLogger.hpp.
|
inlineprivate |
Creates a unique file path based on the timestamp and a hash.
timestamp_ms | The timestamp in milliseconds. |
Definition at line 336 of file UniqueFileLogger.hpp.
|
inlineprivate |
Gets the current timestamp in milliseconds.
Definition at line 434 of file UniqueFileLogger.hpp.
|
inlineprivate |
Formats the timestamp into a string with date and time.
timestamp_ms | The timestamp in milliseconds. |
Definition at line 345 of file UniqueFileLogger.hpp.
|
inlineprivate |
Generates a fixed-length hash string.
length | The desired length of the hash. |
Definition at line 355 of file UniqueFileLogger.hpp.
|
inlineprivate |
Gets the full path to the logging directory.
Definition at line 310 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Retrieves a floating-point parameter from the logger.
param | The logger parameter to retrieve. |
Implements logit::ILogger.
Definition at line 232 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Retrieves an integer parameter from the logger.
param | The logger parameter to retrieve. |
Implements logit::ILogger.
Definition at line 219 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the last log file name for the calling thread.
This method waits until all pending log messages for the calling thread have been processed. It then returns the name of the last file that the calling thread wrote a log message to.
Definition at line 444 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the last log file path for the calling thread.
This method waits until all pending log messages for the calling thread have been processed. It then returns the path of the last file that the calling thread wrote a log message to.
Definition at line 464 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the timestamp of the last log.
Definition at line 480 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Gets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 248 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Retrieves a string parameter from the logger.
param | The logger parameter to retrieve. |
Implements logit::ILogger.
Definition at line 204 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the time elapsed since the last log.
Definition at line 486 of file UniqueFileLogger.hpp.
|
inlineprivate |
Extracts the timestamp from the filename.
filename | The filename to extract the timestamp from. |
Definition at line 427 of file UniqueFileLogger.hpp.
|
inlineprivate |
Initializes the logging directory.
Definition at line 304 of file UniqueFileLogger.hpp.
|
inlineprivate |
Checks if the filename matches the log file naming pattern.
filename | The filename to check. |
Definition at line 419 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Logs a message to a unique 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 log message to write. |
Implements logit::ILogger.
Definition at line 117 of file UniqueFileLogger.hpp.
|
inlineprivate |
Removes old log files based on the auto-delete days configuration.
Definition at line 372 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Sets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 243 of file UniqueFileLogger.hpp.
|
inlineprivate |
Starts the logging process by initializing the directory and removing old logs.
Definition at line 288 of file UniqueFileLogger.hpp.
|
inlineprivate |
Stops the logging process by waiting for tasks.
Definition at line 299 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Waits for all asynchronous tasks to complete.
Implements logit::ILogger.
Definition at line 253 of file UniqueFileLogger.hpp.
|
inlineprivate |
Writes a log message to a unique file.
message | The log message to write. |
timestamp_ms | The timestamp of the log message in milliseconds. |
Definition at line 318 of file UniqueFileLogger.hpp.
|
private |
Configuration for the unique file logger.
Definition at line 260 of file UniqueFileLogger.hpp.
|
private |
Timestamp of the last log.
Definition at line 283 of file UniqueFileLogger.hpp.
|
private |
Definition at line 284 of file UniqueFileLogger.hpp.
|
mutableprivate |
Mutex to protect file operations.
Definition at line 259 of file UniqueFileLogger.hpp.
|
mutableprivate |
Condition variable to wait for pending logs to finish.
Definition at line 280 of file UniqueFileLogger.hpp.
|
private |
Map to store log information per thread.
Definition at line 281 of file UniqueFileLogger.hpp.
|
mutableprivate |
Mutex to protect access to thread log information.
Definition at line 279 of file UniqueFileLogger.hpp.