Improved logger

Improved logger
This commit is contained in:
jojos38 2020-01-28 15:47:55 +01:00
parent b3a26c9897
commit 35eebe95ad
3 changed files with 38 additions and 15 deletions

View File

@ -2,29 +2,30 @@
// Created by jojos38 on 28.01.2020. // Created by jojos38 on 28.01.2020.
// //
#include "logger.h" #include "logger.h"
using namespace std; using namespace std;
int level = 0; int loggerlevel;
void setLoggerLevel(char level_string[]) { void setLoggerLevel(char level_string[]) {
if (!strcmp(level_string, "ALL")) if (!strcmp(level_string, "ALL"))
level = 0; loggerlevel = 0;
if (!strcmp(level_string, "DEBUG")) if (!strcmp(level_string, "DEBUG"))
level = 1; loggerlevel = 1;
if (!strcmp(level_string, "INFO")) if (!strcmp(level_string, "INFO"))
level = 2; loggerlevel = 2;
if (!strcmp(level_string, "WARN")) if (!strcmp(level_string, "WARN"))
level = 3; loggerlevel = 3;
if (!strcmp(level_string, "ERROR")) if (!strcmp(level_string, "ERROR"))
level = 4; loggerlevel = 4;
if (!strcmp(level_string, "OFF")) if (!strcmp(level_string, "OFF"))
level = 5; loggerlevel = 5;
} }
stringstream getDate() { stringstream getDate() {
@ -71,6 +72,7 @@ stringstream getDate() {
return date; return date;
} }
/*
void info(char obj[]) { void info(char obj[]) {
if (level <= 2) if (level <= 2)
cout << getDate().str() << "\u001b[36m" << "[INFO]" << "\u001b[0m" << " " << obj << endl; cout << getDate().str() << "\u001b[36m" << "[INFO]" << "\u001b[0m" << " " << obj << endl;
@ -89,4 +91,4 @@ void warn(char obj[]) {
void debug(char obj[]) { void debug(char obj[]) {
if (level <= 1) if (level <= 1)
cout << getDate().str() << "\u001b[35m" << "[DBUG]" << "\u001b[0m" << " " << obj << endl; cout << getDate().str() << "\u001b[35m" << "[DBUG]" << "\u001b[0m" << " " << obj << endl;
} }*/

View File

@ -7,13 +7,35 @@
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include<sstream> #include <sstream>
#include <string.h> #include <string.h>
using namespace std;
void setLoggerLevel(char level[]); extern int loggerlevel;
void info(char obj[]); stringstream getDate();
void error(char obj[]); void setLoggerLevel(char level_string[]);
void warn(char obj[]);
void debug(char obj[]); template<typename T>
void info(const T& toPrint) {
if (loggerlevel <= 2)
cout << getDate().str() << "\u001b[36m" << "[INFO]" << "\u001b[0m" << " " << toPrint << endl;
}
template<typename T>
void error(const T& toPrint) {
if (loggerlevel <= 4)
cout << getDate().str() << "\x1B[31m" << "[ERRO]" << "\u001b[0m" << " " << toPrint << endl;
}
template<typename T>
void warn(const T& toPrint) {
if (loggerlevel <= 3)
cout << getDate().str() << "\u001b[33m" << "[WARN]" << "\u001b[0m" << " " << toPrint << endl;
}
template<typename T>
void debug(const T& toPrint) {
if (loggerlevel <= 1)
cout << getDate().str() << "\u001b[35m" << "[DBUG]" << "\u001b[0m" << " " << toPrint << endl;
}
#endif // LOGGER_H #endif // LOGGER_H

View File

@ -9,6 +9,5 @@
int main() { int main() {
// ALL > DEBUG > INFO > WARN > ERROR > OFF // ALL > DEBUG > INFO > WARN > ERROR > OFF
setLoggerLevel("ALL"); setLoggerLevel("ALL");
startRUDP("localhost", 30814); startRUDP("localhost", 30814);
} }