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 bool single_mode = false) {
51 std::lock_guard<std::mutex> lock(m_mutex);
52 m_loggers.push_back(
53 LoggerStrategy{std::move(logger),
54 std::move(formatter),
55 single_mode,
56 true // Loggers are enabled by default
57 });
58 }
59
63 void set_logger_enabled(int logger_index, bool enabled) {
64 std::lock_guard<std::mutex> lock(m_mutex);
65 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
66 m_loggers[logger_index].enabled = enabled;
67 }
68 }
69
73 bool is_logger_enabled(int logger_index) const {
74 std::lock_guard<std::mutex> lock(m_mutex);
75 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
76 return m_loggers[logger_index].enabled;
77 }
78 return false;
79 }
80
84 void set_logger_single_mode(int logger_index, bool single_mode) {
85 std::lock_guard<std::mutex> lock(m_mutex);
86 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
87 m_loggers[logger_index].single_mode = single_mode;
88 }
89 }
90
94 bool is_logger_single_mode(int logger_index) const {
95 std::lock_guard<std::mutex> lock(m_mutex);
96 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
97 return m_loggers[logger_index].single_mode;
98 }
99 return false;
100 }
101
108 void log(const LogRecord& record) {
109 std::lock_guard<std::mutex> lock(m_mutex);
110 // Log to the specific logger if the index is valid
111 if (record.logger_index >= 0 && record.logger_index < static_cast<int>(m_loggers.size())) {
112 const auto& strategy = m_loggers[record.logger_index];
113 strategy.logger->log(record, strategy.formatter->format(record));
114 return;
115 }
116 for (const auto& strategy : m_loggers) {
117 if (strategy.single_mode) continue;
118 strategy.logger->log(record, strategy.formatter->format(record));
119 }
120 }
121
126 std::string get_string_param(int logger_index, const LoggerParam& param) const {
127 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
128 const auto& strategy = m_loggers[logger_index];
129 return strategy.logger->get_string_param(param);
130 }
131 return std::string();
132 }
133
138 int64_t get_int_param(int logger_index, const LoggerParam& param) const {
139 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
140 const auto& strategy = m_loggers[logger_index];
141 return strategy.logger->get_int_param(param);
142 }
143 return 0;
144 }
145
150 double get_float_param(int logger_index, const LoggerParam& param) const {
151 if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
152 const auto& strategy = m_loggers[logger_index];
153 return strategy.logger->get_float_param(param);
154 }
155 return 0.0;
156 }
157
163 template <typename... Ts>
164 auto log_and_return(const LogRecord& record, Ts&&... args) -> decltype(std::forward_as_tuple(std::forward<Ts>(args)...)) {
165 this->print(record, args...);
166 return std::forward_as_tuple(std::forward<Ts>(args)...);
167 }
168
174 template <typename T>
175 auto log_and_return(const LogRecord& record, T&& arg) -> decltype(arg) {
176 this->print(record, arg);
177 return std::forward<decltype(arg)>(arg);
178 }
179
183 auto log_and_return(const LogRecord& record) -> std::tuple<> {
184 this->print(record);
185 return {};
186 }
187
188 private:
189
193 std::unique_ptr<ILogger> logger;
194 std::unique_ptr<ILogFormatter> formatter;
196 bool enabled;
197 };
198
199 std::vector<LoggerStrategy> m_loggers;
200 mutable std::mutex m_mutex;
201
206 template <typename... Ts>
207 void print(const LogRecord& record, Ts const&... args) {
208 if (sizeof...(Ts) == 0) {
209 log(record);
210 return;
211 }
212 LogRecord mutable_record = record;
213 auto var_names = split_arguments(mutable_record.arg_names);
214 mutable_record.args_array = args_to_array(var_names.begin(), args...);
215 log(mutable_record);
216 }
217
218 Logger() = default;
219
221 wait();
222 }
223
224 // Deleting copy and move constructors and assignment operators to enforce singleton.
225 Logger(const Logger&) = delete;
226 Logger& operator=(const Logger&) = delete;
227 Logger(Logger&&) = delete;
228 Logger& operator=(Logger&&) = delete;
229 };
230
231}; // namespace logit
232
233#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:183
void set_logger_enabled(int logger_index, bool enabled)
Enables or disables a logger by index.
Definition Logger.hpp:63
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:108
void set_logger_single_mode(int logger_index, bool single_mode)
Sets the single-mode flag for a logger.
Definition Logger.hpp:84
void add_logger(std::unique_ptr< ILogger > logger, std::unique_ptr< ILogFormatter > formatter, bool single_mode=false)
Adds a logger and its corresponding formatter.
Definition Logger.hpp:47
Logger & operator=(const Logger &)=delete
Logger(const Logger &)=delete
std::vector< LoggerStrategy > m_loggers
Container for logger-formatter pairs.
Definition Logger.hpp:199
auto log_and_return(const LogRecord &record, T &&arg) -> decltype(arg)
Logs the message and returns a single argument.
Definition Logger.hpp:175
std::mutex m_mutex
Mutex for thread safety during logging operations.
Definition Logger.hpp:200
bool is_logger_single_mode(int logger_index) const
Checks whether a logger is in single mode.
Definition Logger.hpp:94
double get_float_param(int logger_index, const LoggerParam &param) const
Retrieves a floating-point parameter from the logger.
Definition Logger.hpp:150
std::string get_string_param(int logger_index, const LoggerParam &param) const
Retrieves a string parameter from the logger.
Definition Logger.hpp:126
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:164
int64_t get_int_param(int logger_index, const LoggerParam &param) const
Retrieves an integer parameter from the logger.
Definition Logger.hpp:138
bool is_logger_enabled(int logger_index) const
Checks whether a logger is enabled.
Definition Logger.hpp:73
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:207
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:192
std::unique_ptr< ILogger > logger
The logger instance.
Definition Logger.hpp:193
bool single_mode
Flag indicating if the logger is in single mode.
Definition Logger.hpp:195
std::unique_ptr< ILogFormatter > formatter
The formatter instance.
Definition Logger.hpp:194
bool enabled
Flag indicating if the logger is enabled.
Definition Logger.hpp:196