1#ifndef _LOGIT_FILE_LOGGER_HPP_INCLUDED
2#define _LOGIT_FILE_LOGGER_HPP_INCLUDED
58 const std::string& directory,
59 const bool& async =
true,
60 const int& auto_delete_days = 30) {
79 void log(
const LogRecord& record,
const std::string& message)
override {
82 std::lock_guard<std::mutex> lock(
m_mutex);
85 }
catch (
const std::exception& e) {
86 std::cerr <<
"Log error: " << e.what() << std::endl;
92 std::lock_guard<std::mutex> lock(
m_mutex);
95 }
catch (
const std::exception& e) {
96 std::cerr <<
"Log async log error: " << e.what() << std::endl;
113 return std::string();
164 std::lock_guard<std::mutex> lock(
m_mutex);
169 }
catch (
const std::exception& e) {
170 std::cerr <<
"Initialization error: " << e.what() << std::endl;
177 std::lock_guard<std::mutex> lock(
m_mutex);
207 throw std::runtime_error(
"Failed to open log file: " +
m_file_path);
215 std::string date_str = time_shield::to_iso8601_date(date_ts);
222 void write_log(
const std::string& message,
const int64_t& timestamp_ms) {
223 const int64_t message_date_ts = time_shield::start_of_day(time_shield::ms_to_sec(timestamp_ms));
229 m_file << message << std::endl;
236# if __cplusplus >= 201703L
238 if (!fs::exists(dir_path) ||
239 !fs::is_directory(dir_path)) {
243 for (
const auto& entry : fs::directory_iterator(dir_path)) {
244 if (!fs::is_regular_file(entry.status()))
continue;
245 std::string filename = entry.path().filename().string();
248 if (file_date_ts < threshold_ts) {
249 fs::remove(entry.path());
255 for (
const auto& file_path : file_list) {
257 std::string filename = file_path.substr(file_path.find_last_of(
"/\\") + 1);
260 if (file_date_ts < threshold_ts) {
261 remove(file_path.c_str());
272 static const std::regex pattern(R
"((\d{4}-\d{2}-\d{2})\.log)");
273 return std::regex_match(filename, pattern);
280 constexpr size_t EXTENSION_LENGTH =
sizeof(
".log") - 1;
281 return time_shield::ts(filename.substr(0, filename.size() - EXTENSION_LENGTH));
Defines the interface for loggers used in the logging system.
Provides various logging macros for different log levels and options.
Defines the TaskExecutor class, which manages task execution in a separate thread.
Logs messages to files with date-based rotation and automatic deletion of old logs.
std::mutex m_mutex
Mutex to protect file operations.
int64_t m_current_date_ts
Timestamp of the current log file's date.
void stop_logging()
Stops the logging process by closing the file and waiting for tasks.
void initialize_directory()
Initializes the logging directory.
std::string get_last_log_file_name() const
Retrieves the last log file name.
void remove_old_logs()
Removes old log files based on the auto-delete days configuration.
std::string create_file_path(const int64_t &date_ts) const
Creates a file path for the log file based on the date timestamp.
int64_t get_date_ts_from_filename(const std::string &filename) const
Extracts the date timestamp from the log filename.
FileLogger(const Config &config)
Constructor with custom configuration.
int64_t current_timestamp_ms() const
Gets the current timestamp in milliseconds.
Config m_config
Configuration for the file logger.
int64_t get_time_since_last_log() const
Retrieves the time since the last log.
std::string get_last_log_file_path() const
Retrieves the last log file path.
FileLogger(const std::string &directory, const bool &async=true, const int &auto_delete_days=30)
Constructor with directory and asynchronous flag.
std::mutex m_file_path_mutex
Mutex to protect file path operations.
int64_t get_int_param(const LoggerParam ¶m) const override
Retrieves an integer parameter from the logger.
std::ofstream m_file
Output file stream for logging.
std::atomic< int64_t > m_last_log_ts
Timestamp of the last log.
virtual ~FileLogger()
Destructor to stop logging and close file.
void start_logging()
Starts the logging process by initializing the file and directory.
bool is_valid_log_filename(const std::string &filename) const
Checks if the filename matches the log file naming pattern.
std::string get_string_param(const LoggerParam ¶m) const override
Retrieves a string parameter from the logger.
int64_t get_last_log_ts() const
Retrieves the timestamp of the last log.
std::string m_file_path
Path of the currently open log file.
void log(const LogRecord &record, const std::string &message) override
Logs a message to a file with thread safety.
FileLogger()
Default constructor that uses default configuration.
int64_t get_current_utc_date_ts() const
Gets the current UTC date timestamp in seconds.
void open_log_file(const int64_t &date_ts)
Opens a new log file based on the provided date timestamp.
std::string get_directory_path() const
Gets the full path to the logging directory.
void write_log(const std::string &message, const int64_t ×tamp_ms)
Writes a log message to the file.
void wait() override
Waits for all asynchronous tasks to complete.
std::string m_file_name
Name of the currently open log file.
double get_float_param(const LoggerParam ¶m) const override
Retrieves a floating-point parameter from the logger.
Interface for loggers that handle log message output.
void add_task(std::function< void()> task)
Adds a task to the queue in a thread-safe manner.
void wait()
Waits for all tasks in the queue to be processed.
static TaskExecutor & get_instance()
Get the singleton instance of the TaskExecutor.
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds. If LOGIT_CURRENT_TIMESTAMP_MS is not defined,...
The primary namespace for the LogIt++ library.
std::string get_exe_path()
Retrieves the directory of the executable file.
std::vector< std::string > get_list_files(const std::string &path)
Recursively retrieves a list of all files in a directory.
void create_directories(const std::string &path)
Creates directories recursively for the given path.
LoggerParam
Enumeration for different logger parameters that can be retrieved.
@ TimeSinceLastLog
The time elapsed since the last log in seconds.
@ LastLogTimestamp
The timestamp of the last log.
@ LastFileName
The name of the last file written to.
@ LastFilePath
The full path of the last file written to.
std::string get_file_name(const std::string &file_path)
Extracts the file name from a full file path.
Utility functions for path manipulation, including relative path computation.
Configuration for the file logger.
int auto_delete_days
Number of days after which old log files are deleted.
bool async
Flag indicating whether logging should be asynchronous.
std::string directory
Directory where log files are stored.
Stores log metadata and content.
const int64_t timestamp_ms
Timestamp in milliseconds.