![]() |
LogIt++
Header-only, macro-first C++ logging library
|
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 |
| 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 ¶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. | |
| 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 ×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. | |
| 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) |
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 79 of file UniqueFileLogger.hpp.
|
inline |
Default constructor that uses default configuration.
Definition at line 95 of file UniqueFileLogger.hpp.
|
inline |
Constructor with custom configuration.
| config | The configuration for the logger. |
Definition at line 101 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 115 of file UniqueFileLogger.hpp.
|
inline |
Constructor with directory, async flag, and dedicated executor options.
Definition at line 128 of file UniqueFileLogger.hpp.
|
inlinevirtual |
Definition at line 145 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Clears managed unique log files.
Implements logit::ILogger.
Definition at line 387 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 542 of file UniqueFileLogger.hpp.
|
inlineprivate |
Gets the current timestamp in milliseconds.
Definition at line 768 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 686 of file UniqueFileLogger.hpp.
|
inlinestaticprivate |
Definition at line 690 of file UniqueFileLogger.hpp.
|
inlineprivate |
Formats the timestamp into a string with date and time.
| timestamp_ms | The timestamp in milliseconds. |
Definition at line 551 of file UniqueFileLogger.hpp.
|
inlineprivate |
Generates a fixed-length hash string.
| length | The desired length of the hash. |
Definition at line 561 of file UniqueFileLogger.hpp.
|
inlineprivate |
Extracts the date timestamp from the filename.
| filename | The filename to extract the date from. |
Definition at line 762 of file UniqueFileLogger.hpp.
|
inlineprivate |
Gets the full path to the logging directory.
Definition at line 516 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 353 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Retrieves an integer parameter from the logger.
| param | The logger parameter to retrieve. |
Implements logit::ILogger.
Definition at line 340 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 778 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 798 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the timestamp of the last log.
Definition at line 814 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Gets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 371 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Retrieves a string parameter from the logger.
| param | The logger parameter to retrieve. |
Implements logit::ILogger.
Definition at line 325 of file UniqueFileLogger.hpp.
|
inlineprivate |
Retrieves the time elapsed since the last log.
Definition at line 820 of file UniqueFileLogger.hpp.
|
inlineprivate |
Initializes the logging directory.
Definition at line 510 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 682 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 632 of file UniqueFileLogger.hpp.
|
inlineprivate |
Checks if the filename matches the log file naming pattern.
| filename | The filename to check. |
Definition at line 753 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Lists persisted log files owned by this backend.
Implements logit::ILogger.
Definition at line 827 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 230 of file UniqueFileLogger.hpp.
|
inlinestaticprivate |
Definition at line 474 of file UniqueFileLogger.hpp.
|
inlinestaticprivate |
Definition at line 674 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 661 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Reads one persisted log file owned by this backend.
| path | Full path returned by list_log_files(). |
Implements logit::ILogger.
Definition at line 884 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 595 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Reads several persisted log files owned by this backend.
| paths | Full paths returned by list_log_files(). |
Implements logit::ILogger.
Definition at line 902 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 607 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 624 of file UniqueFileLogger.hpp.
|
inlineprivate |
Removes old log files based on the auto-delete days configuration.
Definition at line 706 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 649 of file UniqueFileLogger.hpp.
|
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.
|
inlineoverridevirtual |
Sets the minimal log level for this logger.
Implements logit::ILogger.
Definition at line 366 of file UniqueFileLogger.hpp.
|
inline |
Updates queue_capacity and queue_policy at runtime without draining tasks.
| queue_capacity | New maximum queue size (0 = unlimited). |
| queue_policy | Overflow policy for the executor queue. |
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.
|
inlineoverridevirtual |
Stops logger-owned asynchronous resources after draining pending writes.
Implements logit::ILogger.
Definition at line 430 of file UniqueFileLogger.hpp.
|
inlineprivate |
Starts the logging process by initializing the directory and removing old logs.
Definition at line 494 of file UniqueFileLogger.hpp.
|
inlineprivate |
Stops the logging process by waiting for tasks.
Definition at line 505 of file UniqueFileLogger.hpp.
|
inlinestaticprivate |
Definition at line 695 of file UniqueFileLogger.hpp.
|
inlineprivate |
Definition at line 577 of file UniqueFileLogger.hpp.
|
inlineoverridevirtual |
Waits for all asynchronous tasks to complete.
Implements logit::ILogger.
Definition at line 376 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 524 of file UniqueFileLogger.hpp.
|
private |
Configuration for the unique file logger.
Definition at line 445 of file UniqueFileLogger.hpp.
|
private |
Dedicated executor (null = use global).
Definition at line 446 of file UniqueFileLogger.hpp.
|
private |
Timestamp of the last log.
Definition at line 470 of file UniqueFileLogger.hpp.
|
private |
Timestamp of the last log.
Definition at line 469 of file UniqueFileLogger.hpp.
|
private |
Serializes direct log() calls with shutdown().
Definition at line 444 of file UniqueFileLogger.hpp.
|
private |
Definition at line 471 of file UniqueFileLogger.hpp.
|
mutableprivate |
Mutex to protect file operations.
Definition at line 443 of file UniqueFileLogger.hpp.
|
mutableprivate |
Condition variable to wait for pending logs to finish.
Definition at line 466 of file UniqueFileLogger.hpp.
|
private |
Definition at line 472 of file UniqueFileLogger.hpp.
|
private |
Map to store log information per thread.
Definition at line 467 of file UniqueFileLogger.hpp.
|
mutableprivate |
Mutex to protect access to thread log information.
Definition at line 465 of file UniqueFileLogger.hpp.