15#include <condition_variable>
17#include <unordered_map>
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__); \
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__)
85 void setLogDirectory(
const std::string& directory, std::size_t maxSize = 10048576);
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);
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;
147 void rotateLogFile(std::thread::id
threadId);
154 std::string toString(
LogLevel level);
160 std::string currentDateTime();
168 std::ofstream& getLogFile(std::thread::id
threadId);
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);
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);
180 template<
typename... Args>
181 void logToTerminal(std::ostream& os,
LogLevel messageLevel,
const char* errorType,
const Args&... args);
183 std::queue<std::string> logQueue;
184 std::thread loggingThread;
185 std::mutex queueMutex;
186 std::condition_variable conditionVar;
187 std::atomic<bool> exitFlag;
A singleton logging utility supporting multiple logging levels and concurrent logging to file and ter...
void setEnabled(bool enabled)
Enables or disables the Logger.
std::string getLogFilenameWithSuffix(const std::string &baseName, int suffix)
Retrieves a log filename with a suffix.
void setLevel(LogLevel level)
Sets the logging level.
LogLevel
Logging levels supported by the Logger.
void setLogDirectory(const std::string &directory, std::size_t maxSize=10048576)
Sets the directory for storing log files.
~Logger()
Destructor for Logger, ensures graceful shutdown of logging.
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.
static Logger * getInstance()
Retrieves the singleton instance of the Logger.
double T(double x)
Wilson coefficient T(x).