Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1#ifndef LOGGER_H
2#define LOGGER_H
3
4#include <string>
5#include <iostream>
6#include <fstream>
7#include <complex>
8#include <vector>
9#include <sstream>
10#include <ctime>
11#include <filesystem>
12#include <queue>
13#include <thread>
14#include <mutex>
15#include <condition_variable>
16#include <atomic>
17#include <unordered_map>
18#include "LoggingException.h"
19
39#define LOG_INFO(...) Logger::getInstance()->log(Logger::LogLevel::INFO, __FILE__, __LINE__, __func__, "", __VA_ARGS__)
40#define LOG_WARN(...) Logger::getInstance()->log(Logger::LogLevel::WARN, __FILE__, __LINE__, __func__, "",__VA_ARGS__)
41#define LOG_ERROR(type, ...) do { \
42 Logger::getInstance()->log(Logger::LogLevel::ERROR, __FILE__, __LINE__, __func__, type, __VA_ARGS__); \
43 std::terminate(); \
44} while(0)
45#define LOG_DEBUG(...) Logger::getInstance()->log(Logger::LogLevel::DEBUG, __FILE__, __LINE__, __func__,"", __VA_ARGS__)
46#define LOG_TRACE(...) Logger::getInstance()->log(Logger::LogLevel::TRACE, __FILE__, __LINE__, __func__,"", __VA_ARGS__)
47#define LOG_VERBOSE(...) Logger::getInstance()->log(Logger::LogLevel::VERBOSE, __FILE__, __LINE__, __func__,"", __VA_ARGS__)
48
53class Logger {
54public:
59 enum class LogLevel {
60 TRACE,
61 VERBOSE,
62 DEBUG,
63 INFO,
64 WARN,
65 ERROR
66 };
67
72 static Logger* getInstance();
73
78 void setLevel(LogLevel level);
79
85 void setLogDirectory(const std::string& directory, std::size_t maxSize = 10048576);
86
91 void setEnabled(bool enabled);
92
98 std::string getLogFilenameForThread(std::thread::id threadId);
99
106 std::string getLogFilenameWithSuffix(const std::string& baseName, int suffix);
107
108 std::thread::id threadId;
120 template<typename... Args>
121 void log(LogLevel messageLevel, const char* file, int line, const char* func, const char* errorType, const Args&... args) noexcept(false);
122
126 ~Logger();
127
128private:
129 static Logger* instance;
134 Logger();
135
136 LogLevel level = LogLevel::INFO;
137 std::string logDirectory{"logs"};
138 std::size_t maxFileSize{10048576};
139 std::unordered_map<std::thread::id, std::ofstream> logFiles;
140 std::mutex logFileMutex;
141 bool enabled{true};
147 void rotateLogFile(std::thread::id threadId);
148
154 std::string toString(LogLevel level);
155
160 std::string currentDateTime();
161
168 std::ofstream& getLogFile(std::thread::id threadId);
169
170
171
172 template<typename T>
173 void logMessage(std::ostream& os, const T& value);
174 template<typename T, typename... Args>
175 void logMessage(std::ostream& os, const T& value, const Args&... args);
176
177 template<typename... Args>
178 void logToFile(std::ostream& os, LogLevel messageLevel, const char* file, int line, const char* func, const char* errorType, const Args&... args);
179
180 template<typename... Args>
181 void logToTerminal(std::ostream& os, LogLevel messageLevel, const char* errorType, const Args&... args);
182
183 std::queue<std::string> logQueue;
184 std::thread loggingThread;
185 std::mutex queueMutex;
186 std::condition_variable conditionVar;
187 std::atomic<bool> exitFlag;
192 void processQueue();
193
194};
195
196#include "Logger.tpp"
197
198#endif // LOGGER_H
199
A singleton logging utility supporting multiple logging levels and concurrent logging to file and ter...
Definition Logger.h:53
std::thread::id threadId
Definition Logger.h:108
void setEnabled(bool enabled)
Enables or disables the Logger.
Definition Logger.cpp:50
std::string getLogFilenameWithSuffix(const std::string &baseName, int suffix)
Retrieves a log filename with a suffix.
Definition Logger.cpp:74
void setLevel(LogLevel level)
Sets the logging level.
Definition Logger.cpp:38
LogLevel
Logging levels supported by the Logger.
Definition Logger.h:59
void setLogDirectory(const std::string &directory, std::size_t maxSize=10048576)
Sets the directory for storing log files.
Definition Logger.cpp:42
~Logger()
Destructor for Logger, ensures graceful shutdown of logging.
Definition Logger.cpp:25
void log(LogLevel messageLevel, const char *file, int line, const char *func, const char *errorType, const Args &... args) noexcept(false)
Logs a message with the specified level and details.
std::string getLogFilenameForThread(std::thread::id threadId)
Retrieves the log filename for the specified thread.
Definition Logger.cpp:80
static Logger * getInstance()
Retrieves the singleton instance of the Logger.
Definition Logger.cpp:5
double T(double x)
Wilson coefficient T(x).