Logger that writes each log message to a unique file with date-time and hash in the filename.
More...
#include <UniqueFileLogger.hpp>
|
| 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 | wait () override |
| Waits for all asynchronous tasks to complete.
|
|
virtual | ~ILogger ()=default |
|
|
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.
|
|
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.
◆ UniqueFileLogger() [1/3]
logit::UniqueFileLogger::UniqueFileLogger |
( |
| ) |
|
|
inline |
◆ UniqueFileLogger() [2/3]
logit::UniqueFileLogger::UniqueFileLogger |
( |
const Config & | config | ) |
|
|
inline |
Constructor with custom configuration.
- Parameters
-
config | The 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
-
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 60 of file UniqueFileLogger.hpp.
◆ ~UniqueFileLogger()
virtual logit::UniqueFileLogger::~UniqueFileLogger |
( |
| ) |
|
|
inlinevirtual |
◆ 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_ms | The 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_ms | The 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
-
length | The 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
-
param | The 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
-
param | The 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
-
param | The 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
-
filename | The 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 |
◆ 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
-
filename | The 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
-
record | The log record containing log information. |
message | The 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 |
◆ wait()
void logit::UniqueFileLogger::wait |
( |
| ) |
|
|
inlineoverridevirtual |
◆ 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
-
message | The log message to write. |
timestamp_ms | The 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.
◆ m_config
Config logit::UniqueFileLogger::m_config |
|
private |
◆ m_last_log_ts
std::atomic<int64_t> logit::UniqueFileLogger::m_last_log_ts = ATOMIC_VAR_INIT(0) |
|
private |
◆ m_mutex
std::mutex logit::UniqueFileLogger::m_mutex |
|
mutableprivate |
◆ m_pending_logs_cv
std::condition_variable logit::UniqueFileLogger::m_pending_logs_cv |
|
mutableprivate |
◆ m_thread_log_info
std::unordered_map<std::thread::id, ThreadLogInfo> logit::UniqueFileLogger::m_thread_log_info |
|
private |
◆ m_thread_log_info_mutex
std::mutex logit::UniqueFileLogger::m_thread_log_info_mutex |
|
mutableprivate |
The documentation for this class was generated from the following file: