added logger

triing to add logger and some queue features (main loop can send and retrieve items form queue based on schedule and communication with dekstop interface)
This commit is contained in:
jakani24
2023-12-23 20:15:44 +01:00
parent 45f8ad413f
commit a7e323618c
92 changed files with 966 additions and 16 deletions

View File

@@ -0,0 +1,59 @@
#ifndef LOG_CPP
#define LOG_CPP
#include "log.h"
std::string get_loglevel(LOGLEVEL level) {
switch (level) {
case LOGLEVEL::INFO:
return "INFO";
case LOGLEVEL::WARN:
return "WARNING";
case LOGLEVEL::ERR:
return "ERROR";
default:
return "UNKNOWN";
}
}
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... args) {
std::string prefix = get_loglevel(level);
std::time_t now = std::time(nullptr);
std::tm tm = *std::localtime(&now);
std::ostringstream logStream;
logStream << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << " " << prefix << message;
if constexpr (sizeof...(args) > 0) {
((logStream << ' ' << std::forward<Args>(args)), ...);
}
logStream << std::endl;
std::string logString = logStream.str();
// Open the file based on log level
std::ofstream logFile;
switch (level) {
case LOGLEVEL::INFO:
logFile.open(INFOFILE, std::ios_base::app);
break;
case LOGLEVEL::WARNING:
logFile.open(WARNFILE, std::ios_base::app);
break;
case LOGLEVEL::ERROR:
logFile.open(ERRORFILE, std::ios_base::app);
break;
}
// Write the log to the file
if (logFile.is_open()) {
logFile << logString.c_str();
logFile.close();
}
//write the log to the general file
logFile.open(LOGFILE, std::ios_base::app);
if (logFile.is_open()) {
logFile << logString.c_str();
logFile.close();
}
}
#endif