LogIt++
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _LOGIT_LOGGER_HPP_INCLUDED
3#define _LOGIT_LOGGER_HPP_INCLUDED
6
7#include "Logger/ILogger.hpp"
9#include <memory>
10#include <mutex>
11#include <sstream>
12
13namespace logit {
14
21 class Logger {
22 public:
23
26 static Logger& get_instance() {
27 static Logger instance;
28 return instance;
29 }
30
35 void wait() {
36 for (const auto& strategy : m_loggers) {
37 strategy.logger->wait();
38 }
39 }
40
48 std::unique_ptr<ILogger> logger,
49 std::unique_ptr<ILogFormatter> formatter,
50 const bool& single_mode = false) {
51 std::lock_guard<std::mutex> lock(m_mutex);
52 m_loggers.push_back(LoggerStrategy{std::move(logger), std::move(formatter), single_mode});
53 }
54
61 void log(const LogRecord& record) {
62 std::lock_guard<std::mutex> lock(m_mutex);
63 // Log to the specific logger if the index is valid
64 if (record.logger_index >= 0 && record.logger_index < static_cast<int>(m_loggers.size())) {
65 const auto& strategy = m_loggers[record.logger_index];
66 strategy.logger->log(record, strategy.formatter->format(record));
67 return;
68 }
69 for (const auto& strategy : m_loggers) {
70 if (strategy.single_mode) continue;
71 strategy.logger->log(record, strategy.formatter->format(record));
72 }
73 }
74
79 std::string get_string_param(const int& logger_index, const LoggerParam& param) const {
80 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
81 const auto& strategy = m_loggers[logger_index];
82 return strategy.logger->get_string_param(param);
83 }
84 return std::string();
85 }
86
91 int64_t get_int_param(const int& logger_index, const LoggerParam& param) const {
92 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
93 const auto& strategy = m_loggers[logger_index];
94 return strategy.logger->get_int_param(param);
95 }
96 return 0;
97 }
98
103 double get_float_param(const int& logger_index, const LoggerParam& param) const {
104 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
105 const auto& strategy = m_loggers[logger_index];
106 return strategy.logger->get_float_param(param);
107 }
108 return 0.0;
109 }
110
116 template <typename... Ts>
117 auto log_and_return(const LogRecord& record, Ts&&... args) -> decltype(std::forward_as_tuple(std::forward<Ts>(args)...)) {
118 this->print(record, args...);
119 return std::forward_as_tuple(std::forward<Ts>(args)...);
120 }
121
127 template <typename T>
128 auto log_and_return(const LogRecord& record, T&& arg) -> decltype(arg) {
129 this->print(record, arg);
130 return std::forward<decltype(arg)>(arg);
131 }
132
136 auto log_and_return(const LogRecord& record) -> std::tuple<> {
137 this->print(record);
138 return {};
139 }
140
141 private:
142
146 std::unique_ptr<ILogger> logger;
147 std::unique_ptr<ILogFormatter> formatter;
149 };
150
151 std::vector<LoggerStrategy> m_loggers;
152 std::mutex m_mutex;
153
158 template <typename... Ts>
159 void print(const LogRecord& record, Ts const&... args) {
160 if (sizeof...(Ts) == 0) {
161 log(record);
162 return;
163 }
164 LogRecord mutable_record = record;
165 auto var_names = split_arguments(mutable_record.arg_names);
166 mutable_record.args_array = args_to_array(var_names.begin(), args...);
167 log(mutable_record);
168 }
169
170 Logger() = default;
171
173 wait();
174 }
175
176 // Deleting copy and move constructors and assignment operators to enforce singleton.
177 Logger(const Logger&) = delete;
178 Logger& operator=(const Logger&) = delete;
179 Logger(Logger&&) = delete;
180 Logger& operator=(Logger&&) = delete;
181 };
182
183}; // namespace logit
184
185#endif // _LOGIT_LOGGER_HPP_INCLUDED
Defines the interface for log formatters used in the logging system.
Defines the interface for loggers used in the logging system.
Singleton class that manages multiple loggers and formatters.
Definition Logger.hpp:21
Logger()=default
auto log_and_return(const LogRecord &record) -> std::tuple<>
Logs a message without arguments and returns an empty tuple.
Definition Logger.hpp:136
void wait()
Waits for all asynchronous loggers to finish processing.
Definition Logger.hpp:35
void log(const LogRecord &record)
Logs a LogRecord using all added loggers and formatters.
Definition Logger.hpp:61
Logger & operator=(const Logger &)=delete
Logger(const Logger &)=delete
std::vector< LoggerStrategy > m_loggers
Container for logger-formatter pairs.
Definition Logger.hpp:151
auto log_and_return(const LogRecord &record, T &&arg) -> decltype(arg)
Logs the message and returns a single argument.
Definition Logger.hpp:128
std::mutex m_mutex
Mutex for thread safety during logging operations.
Definition Logger.hpp:152
double get_float_param(const int &logger_index, const LoggerParam &param) const
Retrieves a floating-point parameter from the logger.
Definition Logger.hpp:103
void add_logger(std::unique_ptr< ILogger > logger, std::unique_ptr< ILogFormatter > formatter, const bool &single_mode=false)
Adds a logger and its corresponding formatter.
Definition Logger.hpp:47
int64_t get_int_param(const int &logger_index, const LoggerParam &param) const
Retrieves an integer parameter from the logger.
Definition Logger.hpp:91
auto log_and_return(const LogRecord &record, Ts &&... args) -> decltype(std::forward_as_tuple(std::forward< Ts >(args)...))
Logs the message and returns a tuple of arguments.
Definition Logger.hpp:117
std::string get_string_param(const int &logger_index, const LoggerParam &param) const
Retrieves a string parameter from the logger.
Definition Logger.hpp:79
static Logger & get_instance()
Retrieves the singleton instance of Logger.
Definition Logger.hpp:26
Logger & operator=(Logger &&)=delete
Logger(Logger &&)=delete
void print(const LogRecord &record, Ts const &... args)
Logs a record with the given arguments.
Definition Logger.hpp:159
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.
Definition Enums.hpp:46
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.
Definition LogRecord.hpp:15
const int logger_index
Logger index (-1 to log to all).
Definition LogRecord.hpp:25
const std::string format
Format string for the message.
Definition LogRecord.hpp:21
std::vector< VariableValue > args_array
Argument values for the log.
Definition LogRecord.hpp:23
const std::string arg_names
Argument names for the log.
Definition LogRecord.hpp:22
Structure to hold a logger-formatter pair.
Definition Logger.hpp:145
std::unique_ptr< ILogger > logger
The logger instance.
Definition Logger.hpp:146
bool single_mode
Flag indicating if the logger is in single mode.
Definition Logger.hpp:148
std::unique_ptr< ILogFormatter > formatter
The formatter instance.
Definition Logger.hpp:147