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

Logger that writes each log message to a unique file with date-time and hash in the filename. More...

#include <UniqueFileLogger.hpp>

Inheritance diagram for logit::UniqueFileLogger:
logit::ILogger

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 &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 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 &timestamp_ms)
 Writes a log message to a unique file.
 
std::string create_unique_file_path (const int64_t &timestamp_ms) const
 Creates a unique file path based on the timestamp and a hash.
 
std::string format_timestamp (const int64_t &timestamp_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, ThreadLogInfom_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.
 

Detailed Description

Logger that writes each log message to a unique file with date-time and hash in the filename.

The UniqueFileLogger writes each log message to a separate file. The filename consists of the date, time, and a fixed-length hash to ensure uniqueness. It also deletes log files older than a specified number of days.

Definition at line 32 of file UniqueFileLogger.hpp.

Constructor & Destructor Documentation

◆ UniqueFileLogger() [1/3]

logit::UniqueFileLogger::UniqueFileLogger ( )
inline

Default constructor that uses default configuration.

Definition at line 45 of file UniqueFileLogger.hpp.

◆ UniqueFileLogger() [2/3]

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

Constructor with custom configuration.

Parameters
configThe configuration for the logger.

Definition at line 51 of file UniqueFileLogger.hpp.

◆ UniqueFileLogger() [3/3]

logit::UniqueFileLogger::UniqueFileLogger ( const std::string & directory,
bool async = true,
int auto_delete_days = 30,
size_t hash_length = 8 )
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.
hash_lengthLength of the hash used in filenames.

Definition at line 60 of file UniqueFileLogger.hpp.

◆ ~UniqueFileLogger()

virtual logit::UniqueFileLogger::~UniqueFileLogger ( )
inlinevirtual

Definition at line 72 of file UniqueFileLogger.hpp.

Member Function Documentation

◆ create_unique_file_path()

std::string logit::UniqueFileLogger::create_unique_file_path ( const int64_t & timestamp_ms) const
inlineprivate

Creates a unique file path based on the timestamp and a hash.

Parameters
timestamp_msThe timestamp in milliseconds.
Returns
The unique file path.

Definition at line 258 of file UniqueFileLogger.hpp.

◆ current_timestamp_ms()

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

Gets the current timestamp in milliseconds.

Returns
The current timestamp in milliseconds.

Definition at line 343 of file UniqueFileLogger.hpp.

◆ format_timestamp()

std::string logit::UniqueFileLogger::format_timestamp ( const int64_t & timestamp_ms) const
inlineprivate

Formats the timestamp into a string with date and time.

Parameters
timestamp_msThe timestamp in milliseconds.
Returns
The formatted timestamp string.

Definition at line 267 of file UniqueFileLogger.hpp.

◆ generate_fixed_length_hash()

std::string logit::UniqueFileLogger::generate_fixed_length_hash ( size_t length) const
inlineprivate

Generates a fixed-length hash string.

Parameters
lengthThe desired length of the hash.
Returns
The hash string.

Definition at line 277 of file UniqueFileLogger.hpp.

◆ get_directory_path()

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

Gets the full path to the logging directory.

Returns
The path to the logging directory.

Definition at line 236 of file UniqueFileLogger.hpp.

◆ get_float_param()

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

Retrieves a floating-point parameter from the logger.

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

Implements logit::ILogger.

Definition at line 169 of file UniqueFileLogger.hpp.

◆ get_int_param()

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

Retrieves an integer parameter from the logger.

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

Implements logit::ILogger.

Definition at line 156 of file UniqueFileLogger.hpp.

◆ get_last_log_file_name()

std::string logit::UniqueFileLogger::get_last_log_file_name ( ) const
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.

Returns
The last log file name for the calling thread, or an empty string if none exists.

Definition at line 353 of file UniqueFileLogger.hpp.

◆ get_last_log_file_path()

std::string logit::UniqueFileLogger::get_last_log_file_path ( ) const
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.

Returns
The last log file path for the calling thread, or an empty string if none exists.

Definition at line 373 of file UniqueFileLogger.hpp.

◆ get_last_log_ts()

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

Retrieves the timestamp of the last log.

Returns
The timestamp of the last log in milliseconds.

Definition at line 389 of file UniqueFileLogger.hpp.

◆ get_string_param()

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

Retrieves a string parameter from the logger.

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

Implements logit::ILogger.

Definition at line 141 of file UniqueFileLogger.hpp.

◆ get_time_since_last_log()

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

Retrieves the time elapsed since the last log.

Returns
The time in milliseconds since the last log was written.

Definition at line 395 of file UniqueFileLogger.hpp.

◆ get_timestamp_from_filename()

int64_t logit::UniqueFileLogger::get_timestamp_from_filename ( const std::string & filename) const
inlineprivate

Extracts the timestamp from the filename.

Parameters
filenameThe filename to extract the timestamp from.
Returns
The timestamp in milliseconds.

Definition at line 336 of file UniqueFileLogger.hpp.

◆ initialize_directory()

void logit::UniqueFileLogger::initialize_directory ( )
inlineprivate

Initializes the logging directory.

Definition at line 230 of file UniqueFileLogger.hpp.

◆ is_valid_log_filename()

bool logit::UniqueFileLogger::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 328 of file UniqueFileLogger.hpp.

◆ log()

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

Parameters
recordThe log record containing log information.
messageThe log message to write.

Implements logit::ILogger.

Definition at line 83 of file UniqueFileLogger.hpp.

◆ remove_old_logs()

void logit::UniqueFileLogger::remove_old_logs ( )
inlineprivate

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

Definition at line 294 of file UniqueFileLogger.hpp.

◆ start_logging()

void logit::UniqueFileLogger::start_logging ( )
inlineprivate

Starts the logging process by initializing the directory and removing old logs.

Definition at line 214 of file UniqueFileLogger.hpp.

◆ stop_logging()

void logit::UniqueFileLogger::stop_logging ( )
inlineprivate

Stops the logging process by waiting for tasks.

Definition at line 225 of file UniqueFileLogger.hpp.

◆ wait()

void logit::UniqueFileLogger::wait ( )
inlineoverridevirtual

Waits for all asynchronous tasks to complete.

Implements logit::ILogger.

Definition at line 180 of file UniqueFileLogger.hpp.

◆ write_log()

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

Writes a log message to a unique file.

Parameters
messageThe log message to write.
timestamp_msThe timestamp of the log message in milliseconds.
Returns
The name of the file the message was written to.

Definition at line 244 of file UniqueFileLogger.hpp.

Member Data Documentation

◆ m_config

Config logit::UniqueFileLogger::m_config
private

Configuration for the unique file logger.

Definition at line 187 of file UniqueFileLogger.hpp.

◆ m_last_log_ts

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

Timestamp of the last log.

Definition at line 210 of file UniqueFileLogger.hpp.

◆ m_mutex

std::mutex logit::UniqueFileLogger::m_mutex
mutableprivate

Mutex to protect file operations.

Definition at line 186 of file UniqueFileLogger.hpp.

◆ m_pending_logs_cv

std::condition_variable logit::UniqueFileLogger::m_pending_logs_cv
mutableprivate

Condition variable to wait for pending logs to finish.

Definition at line 207 of file UniqueFileLogger.hpp.

◆ m_thread_log_info

std::unordered_map<std::thread::id, ThreadLogInfo> logit::UniqueFileLogger::m_thread_log_info
private

Map to store log information per thread.

Definition at line 208 of file UniqueFileLogger.hpp.

◆ m_thread_log_info_mutex

std::mutex logit::UniqueFileLogger::m_thread_log_info_mutex
mutableprivate

Mutex to protect access to thread log information.

Definition at line 206 of file UniqueFileLogger.hpp.


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