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

@@ -29,8 +29,7 @@ int main() {
//check for tasks in com
//check for scheduled tasks
//execute tasks
//check_for_com_tasks(MAIN_COM,MAIN_COM_PATH);
log(LOGLEVEL::INFO,"test");
log(LOGLEVEL::WARN,"test","test2",222);
printf("check_from_com:%d\n",check_for_com_tasks(MAIN_COM, MAIN_COM_PATH));
printf("check_from_task:%d\n", check_for_sched_tasks(SCHED,SCHED_PATH));
//unlock_task("tsk1"); else it will only be executed once. but this function has to be called at the end of the task. else it will nvr be executed again. this would be bad :(

View File

@@ -14,7 +14,7 @@ std::string get_loglevel(LOGLEVEL level) {
return "UNKNOWN";
}
}
/*
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... args) {
std::string prefix = get_loglevel(level);
@@ -54,6 +54,6 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
logFile << logString.c_str();
logFile.close();
}
}
}*/
#endif

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

View File

@@ -1,4 +1,6 @@
#pragma once
#ifndef WELL_KNOWN_H
#define WELL_KNOWN_H
#define MAIN_COM "main_com.txt"
#define MAIN_COM_PATH "C:\\Program Files\\cyberhex\\com\\main_com.txt"
@@ -12,3 +14,4 @@
#define INFOFILE "C:\\Program Files\\cyberhex\\secure\\log\\info.txt"
#define WARNFILE "C:\\Program Files\\cyberhex\\secure\\log\\warn.txt"
#define ERRORFILE "C:\\Program Files\\cyberhex\\secure\\log\\error.txt"
#endif // !WELL_KNOWN_H

Binary file not shown.

View File

@@ -1,6 +1,3 @@
 Quellen werden auf Modulabhängigkeiten überprüft...
client_backend.cpp
log.cpp
Code wird generiert...
client_backend.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl log<>(enum LOGLEVEL,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??$log@$$V@@YAXW4LOGLEVEL@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)" in Funktion "main".
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
client_backend.vcxproj -> C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe

View File

@@ -1 +0,0 @@
<EFBFBD><EFBFBD>

View File

@@ -1 +0,0 @@
<EFBFBD><EFBFBD>

View File

@@ -1 +0,0 @@
<EFBFBD><EFBFBD>

View File

@@ -1 +0,0 @@
<EFBFBD><EFBFBD>