LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
logit::UniqueFileLogger Class Reference

Writes each log message to a unique file with automatic cleanup. More...

#include <UniqueFileLogger.hpp>

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

Classes

struct  Config
 Configuration for the unique file logger. More...
 
struct  ThreadLogInfo
 
struct  UniqueFileSortKey
 

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.
 
 UniqueFileLogger (const std::string &directory, bool async, int auto_delete_days, size_t hash_length, bool use_dedicated_executor, std::size_t queue_capacity, QueuePolicy queue_policy)
 Constructor with directory, async flag, and dedicated executor options.
 
virtual ~UniqueFileLogger ()
 
void set_config (const Config &config)
 Updates executor-related configuration at runtime.
 
bool set_queue_config (std::size_t queue_capacity, QueuePolicy queue_policy)
 Updates queue_capacity and queue_policy at runtime without draining tasks.
 
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 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.
 
LogClearResult clear_logs (const LogClearOptions &options=LogClearOptions()) override
 Clears managed unique log files.
 
void shutdown () override
 Stops logger-owned asynchronous resources after draining pending writes.
 
std::vector< LogFileInfo > list_log_files () const override
 Lists persisted log files owned by this backend.
 
LogFileReadResult read_log_file (const std::string &path) const override
 Reads one persisted log file owned by this backend.
 
std::vector< LogFileReadResult > read_log_files (const std::vector< std::string > &paths) const override
 Reads several persisted log files owned by this backend.
 
- Public Member Functions inherited from logit::ILogger
virtual ~ILogger ()=default
 
virtual std::vector< std::string > get_buffered_strings () const
 Returns buffered formatted messages in chronological order.
 
virtual std::vector< BufferedLogEntry > get_buffered_entries () const
 Returns buffered structured entries in chronological order.
 

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.
 
bool try_make_log_file_info (const std::string &file_path, LogFileInfo &info) const
 
LogFileReadResult read_log_file_from_info (const LogFileInfo &info) const
 
bool read_plain_file (const std::string &file_path, std::string &out) const
 
bool remove_file_path (const std::string &file_path) const
 
bool is_direct_child_path (const std::string &file_path, const std::string &directory_path) const
 
bool same_path (const std::string &lhs, const std::string &rhs) const
 
std::string normalize_path_separators (const std::string &path) const
 
bool is_compressed_log_filename (const std::string &filename) const
 
bool ends_with (const std::string &value, const std::string &suffix) const
 
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 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.
 

Static Private Member Functions

static Config make_config (const std::string &directory, bool async, int auto_delete_days, size_t hash_length, bool use_dedicated_executor, std::size_t queue_capacity, QueuePolicy queue_policy)
 
static UniqueFileSortKey make_file_sort_key (const LogFileInfo &info)
 
static bool ends_with_impl (const std::string &value, const std::string &suffix)
 
static std::string strip_compression_suffix_impl (const std::string &filename)
 

Private Attributes

std::mutex m_mutex
 Mutex to protect file operations.
 
std::mutex m_lifecycle_mutex
 Serializes direct log() calls with shutdown().
 
Config m_config
 Configuration for the unique file logger.
 
std::unique_ptr< detail::SingleThreadExecutor > m_executor
 Dedicated executor (null = use global).
 
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< int64_t > m_last_log_mono_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))
 
std::atomic< bool > m_shutdown = ATOMIC_VAR_INIT(false)
 

Detailed Description

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:

  • Unique file generation for each log message.
  • Automatic deletion of old files.
  • Synchronous or asynchronous operation.

Definition at line 79 of file UniqueFileLogger.hpp.

Constructor & Destructor Documentation

◆ UniqueFileLogger() [1/4]

logit::UniqueFileLogger::UniqueFileLogger ( )
inline

Default constructor that uses default configuration.

Definition at line 95 of file UniqueFileLogger.hpp.

◆ UniqueFileLogger() [2/4]

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

Constructor with custom configuration.

Parameters
configThe configuration for the logger.

Definition at line 101 of file UniqueFileLogger.hpp.

◆ UniqueFileLogger() [3/4]

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 115 of file UniqueFileLogger.hpp.

◆ UniqueFileLogger() [4/4]

logit::UniqueFileLogger::UniqueFileLogger ( const std::string & directory,
bool async,
int auto_delete_days,
size_t hash_length,
bool use_dedicated_executor,
std::size_t queue_capacity,
QueuePolicy queue_policy )
inline

Constructor with directory, async flag, and dedicated executor options.

Definition at line 128 of file UniqueFileLogger.hpp.

◆ ~UniqueFileLogger()

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

Definition at line 145 of file UniqueFileLogger.hpp.

Member Function Documentation

◆ clear_logs()

LogClearResult logit::UniqueFileLogger::clear_logs ( const LogClearOptions & options = LogClearOptions())
inlineoverridevirtual

Clears managed unique log files.

Implements logit::ILogger.

Definition at line 387 of file UniqueFileLogger.hpp.

◆ 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 542 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 768 of file UniqueFileLogger.hpp.

◆ ends_with()

bool logit::UniqueFileLogger::ends_with ( const std::string & value,
const std::string & suffix ) const
inlineprivate

Definition at line 686 of file UniqueFileLogger.hpp.

◆ ends_with_impl()

static bool logit::UniqueFileLogger::ends_with_impl ( const std::string & value,
const std::string & suffix )
inlinestaticprivate

Definition at line 690 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 551 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 561 of file UniqueFileLogger.hpp.

◆ get_date_ts_from_filename()

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

Extracts the date timestamp from the filename.

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

Definition at line 762 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 516 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 353 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 340 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 778 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 798 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 814 of file UniqueFileLogger.hpp.

◆ get_log_level()

LogLevel logit::UniqueFileLogger::get_log_level ( ) const
inlineoverridevirtual

Gets the minimal log level for this logger.

Implements logit::ILogger.

Definition at line 371 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 325 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 820 of file UniqueFileLogger.hpp.

◆ initialize_directory()

void logit::UniqueFileLogger::initialize_directory ( )
inlineprivate

Initializes the logging directory.

Definition at line 510 of file UniqueFileLogger.hpp.

◆ is_compressed_log_filename()

bool logit::UniqueFileLogger::is_compressed_log_filename ( const std::string & filename) const
inlineprivate

Definition at line 682 of file UniqueFileLogger.hpp.

◆ is_direct_child_path()

bool logit::UniqueFileLogger::is_direct_child_path ( const std::string & file_path,
const std::string & directory_path ) const
inlineprivate

Definition at line 632 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 753 of file UniqueFileLogger.hpp.

◆ list_log_files()

std::vector< LogFileInfo > logit::UniqueFileLogger::list_log_files ( ) const
inlineoverridevirtual

Lists persisted log files owned by this backend.

Returns
Persisted unique-log files sorted newest-first.

Implements logit::ILogger.

Definition at line 827 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 230 of file UniqueFileLogger.hpp.

◆ make_config()

static Config logit::UniqueFileLogger::make_config ( const std::string & directory,
bool async,
int auto_delete_days,
size_t hash_length,
bool use_dedicated_executor,
std::size_t queue_capacity,
QueuePolicy queue_policy )
inlinestaticprivate

Definition at line 474 of file UniqueFileLogger.hpp.

◆ make_file_sort_key()

static UniqueFileSortKey logit::UniqueFileLogger::make_file_sort_key ( const LogFileInfo & info)
inlinestaticprivate

Definition at line 674 of file UniqueFileLogger.hpp.

◆ normalize_path_separators()

std::string logit::UniqueFileLogger::normalize_path_separators ( const std::string & path) const
inlineprivate

Definition at line 661 of file UniqueFileLogger.hpp.

◆ read_log_file()

LogFileReadResult logit::UniqueFileLogger::read_log_file ( const std::string & path) const
inlineoverridevirtual

Reads one persisted log file owned by this backend.

Parameters
pathFull path returned by list_log_files().
Returns
Read result. Compressed files are listed but unreadable in v1.

Implements logit::ILogger.

Definition at line 884 of file UniqueFileLogger.hpp.

◆ read_log_file_from_info()

LogFileReadResult logit::UniqueFileLogger::read_log_file_from_info ( const LogFileInfo & info) const
inlineprivate

Definition at line 595 of file UniqueFileLogger.hpp.

◆ read_log_files()

std::vector< LogFileReadResult > logit::UniqueFileLogger::read_log_files ( const std::vector< std::string > & paths) const
inlineoverridevirtual

Reads several persisted log files owned by this backend.

Parameters
pathsFull paths returned by list_log_files().
Returns
Per-file results in the same order as the request.

Implements logit::ILogger.

Definition at line 902 of file UniqueFileLogger.hpp.

◆ read_plain_file()

bool logit::UniqueFileLogger::read_plain_file ( const std::string & file_path,
std::string & out ) const
inlineprivate

Definition at line 607 of file UniqueFileLogger.hpp.

◆ remove_file_path()

bool logit::UniqueFileLogger::remove_file_path ( const std::string & file_path) const
inlineprivate

Definition at line 624 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 706 of file UniqueFileLogger.hpp.

◆ same_path()

bool logit::UniqueFileLogger::same_path ( const std::string & lhs,
const std::string & rhs ) const
inlineprivate

Definition at line 649 of file UniqueFileLogger.hpp.

◆ set_config()

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

Updates executor-related configuration at runtime.

This call is blocking. If asynchronous logging is currently enabled, it waits until already accepted tasks are drained before changing async mode or executor ownership. Only async, use_dedicated_executor, queue_capacity, and queue_policy are applied. File/rotation/compression fields are ignored. For hot queue tuning without switching executor mode, use set_queue_config().

Definition at line 156 of file UniqueFileLogger.hpp.

◆ set_log_level()

void logit::UniqueFileLogger::set_log_level ( LogLevel level)
inlineoverridevirtual

Sets the minimal log level for this logger.

Implements logit::ILogger.

Definition at line 366 of file UniqueFileLogger.hpp.

◆ set_queue_config()

bool logit::UniqueFileLogger::set_queue_config ( std::size_t queue_capacity,
QueuePolicy queue_policy )
inline

Updates queue_capacity and queue_policy at runtime without draining tasks.

Parameters
queue_capacityNew maximum queue size (0 = unlimited).
queue_policyOverflow policy for the executor queue.
Returns
true if a dedicated executor was present and updated; false if the logger is shut down or uses the global executor.

Unlike set_config(), this method never blocks on task drain and never changes async mode or executor ownership. If no dedicated executor exists, only the stored config fields are updated for future reference.

Definition at line 201 of file UniqueFileLogger.hpp.

◆ shutdown()

void logit::UniqueFileLogger::shutdown ( )
inlineoverridevirtual

Stops logger-owned asynchronous resources after draining pending writes.

Implements logit::ILogger.

Definition at line 430 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 494 of file UniqueFileLogger.hpp.

◆ stop_logging()

void logit::UniqueFileLogger::stop_logging ( )
inlineprivate

Stops the logging process by waiting for tasks.

Definition at line 505 of file UniqueFileLogger.hpp.

◆ strip_compression_suffix_impl()

static std::string logit::UniqueFileLogger::strip_compression_suffix_impl ( const std::string & filename)
inlinestaticprivate

Definition at line 695 of file UniqueFileLogger.hpp.

◆ try_make_log_file_info()

bool logit::UniqueFileLogger::try_make_log_file_info ( const std::string & file_path,
LogFileInfo & info ) const
inlineprivate

Definition at line 577 of file UniqueFileLogger.hpp.

◆ wait()

void logit::UniqueFileLogger::wait ( )
inlineoverridevirtual

Waits for all asynchronous tasks to complete.

Implements logit::ILogger.

Definition at line 376 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 524 of file UniqueFileLogger.hpp.

Member Data Documentation

◆ m_config

Config logit::UniqueFileLogger::m_config
private

Configuration for the unique file logger.

Definition at line 445 of file UniqueFileLogger.hpp.

◆ m_executor

std::unique_ptr<detail::SingleThreadExecutor> logit::UniqueFileLogger::m_executor
private

Dedicated executor (null = use global).

Definition at line 446 of file UniqueFileLogger.hpp.

◆ m_last_log_mono_ts

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

Timestamp of the last log.

Definition at line 470 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 469 of file UniqueFileLogger.hpp.

◆ m_lifecycle_mutex

std::mutex logit::UniqueFileLogger::m_lifecycle_mutex
private

Serializes direct log() calls with shutdown().

Definition at line 444 of file UniqueFileLogger.hpp.

◆ m_log_level

std::atomic<int> logit::UniqueFileLogger::m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE))
private

Definition at line 471 of file UniqueFileLogger.hpp.

◆ m_mutex

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

Mutex to protect file operations.

Definition at line 443 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 466 of file UniqueFileLogger.hpp.

◆ m_shutdown

std::atomic<bool> logit::UniqueFileLogger::m_shutdown = ATOMIC_VAR_INIT(false)
private

Definition at line 472 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 467 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 465 of file UniqueFileLogger.hpp.


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