fixed logger

fixed the logge rbecause it did not really work
This commit is contained in:
jakani24
2023-12-23 20:29:06 +01:00
parent a7e323618c
commit 89aab04cce
32 changed files with 45 additions and 22 deletions

View File

@@ -15,7 +15,44 @@ enum class LOGLEVEL {
std::string get_loglevel(LOGLEVEL level);
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... 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::WARN:
logFile.open(WARNFILE, std::ios_base::app);
break;
case LOGLEVEL::ERR:
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 // LOGGER_H