2#ifndef _LOGIT_LOGGER_HPP_INCLUDED
3#define _LOGIT_LOGGER_HPP_INCLUDED
39 std::unique_ptr<ILogger> logger,
40 std::unique_ptr<ILogFormatter> formatter,
41 bool single_mode =
false) {
43 std::lock_guard<std::mutex> lock(
m_mutex);
57 std::lock_guard<std::mutex> lock(
m_mutex);
58 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
59 m_loggers[logger_index].enabled = enabled;
67 std::lock_guard<std::mutex> lock(
m_mutex);
68 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
79 std::lock_guard<std::mutex> lock(
m_mutex);
80 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
81 m_loggers[logger_index].single_mode = single_mode;
90 std::lock_guard<std::mutex> lock(
m_mutex);
91 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
92 m_loggers[logger_index].formatter->set_timestamp_offset(offset_ms);
101 std::lock_guard<std::mutex> lock(
m_mutex);
102 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
103 return m_loggers[logger_index].single_mode;
115 std::lock_guard<std::mutex> lock(
m_mutex);
119 if (!strategy.enabled)
return;
120 strategy.logger->log(record, strategy.formatter->
format(record));
124 if (strategy.single_mode)
continue;
125 if (!strategy.enabled)
continue;
126 strategy.logger->log(record, strategy.formatter->
format(record));
136 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
137 const auto& strategy =
m_loggers[logger_index];
138 return strategy.logger->get_string_param(param);
140 return std::string();
149 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
150 const auto& strategy =
m_loggers[logger_index];
151 return strategy.logger->get_int_param(param);
162 if (logger_index >= 0 && logger_index <
static_cast<int>(
m_loggers.size())) {
163 const auto& strategy =
m_loggers[logger_index];
164 return strategy.logger->get_float_param(param);
174 template <
typename... Ts>
176 this->
print(record, args...);
177 return std::forward_as_tuple(std::forward<Ts>(args)...);
187 template <
typename T>
189 this->
print(record, args);
190 return std::forward<decltype(args)>(args);
206 strategy.logger->wait();
240 template <
typename... Ts>
242 if (
sizeof...(Ts) == 0) {
Defines the interface for loggers used in the logging system.
auto log_and_return(const LogRecord &record) -> std::tuple<>
Logs message without arguments and returns empty tuple.
auto log_and_return(const LogRecord &record, T &&args) -> decltype(args)
Logs message and returns argument.
void set_logger_enabled(int logger_index, bool enabled)
Enables or disables a logger by index.
void wait()
Waits for all asynchronous loggers to finish processing.
void log(const LogRecord &record)
Logs a LogRecord using added loggers and formatters.
void set_logger_single_mode(int logger_index, bool single_mode)
Sets single-mode flag for a logger.
void set_timestamp_offset(int logger_index, int64_t offset_ms)
Sets timestamp offset for a specific logger.
void add_logger(std::unique_ptr< ILogger > logger, std::unique_ptr< ILogFormatter > formatter, bool single_mode=false)
Adds a logger and its corresponding formatter.
Logger & operator=(const Logger &)=delete
Logger(const Logger &)=delete
void shutdown()
Shuts down logger system.
std::vector< LoggerStrategy > m_loggers
Container for logger-formatter pairs.
std::mutex m_mutex
Mutex for thread safety during logging operations.
bool is_logger_single_mode(int logger_index) const
Checks whether a logger is in single mode.
std::atomic< bool > m_shutdown
Flag indicating if shutdown was requested.
double get_float_param(int logger_index, const LoggerParam ¶m) const
Retrieves a floating-point parameter from a logger.
std::string get_string_param(int logger_index, const LoggerParam ¶m) const
Retrieves a string parameter from a logger.
auto log_and_return(const LogRecord &record, Ts &&... args) -> decltype(std::forward_as_tuple(std::forward< Ts >(args)...))
Logs message and returns tuple of arguments.
int64_t get_int_param(int logger_index, const LoggerParam ¶m) const
Retrieves an integer parameter from a logger.
static void on_exit_handler()
Atexit shutdown handler for Logger and TaskExecutor.
bool is_logger_enabled(int logger_index) const
Checks if a logger is enabled.
static Logger & get_instance()
Retrieves singleton instance of Logger.
Logger & operator=(Logger &&)=delete
void print(const LogRecord &record, Ts const &... args)
Logs a record with given arguments.
void shutdown()
Shuts down the TaskExecutor by stopping the worker thread.
static TaskExecutor & get_instance()
Get the singleton instance of the TaskExecutor.
The primary namespace for the LogIt++ library.
std::vector< std::string > split_arguments(const std::string &all_names)
Splits a string of argument names into individual names, ignoring nested templates,...
LoggerParam
Enumeration for different logger parameters that can be retrieved.
std::vector< VariableValue > args_to_array(std::vector< std::string >::const_iterator name_iter)
Base case of recursion for argument conversion — when there are no more arguments.
Stores log metadata and content.
const int logger_index
Logger index (-1 to log to all).
const std::string format
Format string for the message.
std::vector< VariableValue > args_array
Argument values for the log.
const std::string arg_names
Argument names for the log.
Structure to hold a logger-formatter pair.
std::unique_ptr< ILogger > logger
The logger instance.
bool single_mode
Flag indicating if the logger is in single mode.
std::unique_ptr< ILogFormatter > formatter
The formatter instance.
bool enabled
Flag indicating if the logger is enabled.