+ logging

This commit is contained in:
Julian Hinxlage 2019-11-05 15:08:17 +01:00
parent 3d8bf74fbf
commit 53afcffba8
2 changed files with 79 additions and 0 deletions

60
src/log.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "log.h"
#include <iostream>
const char *getLevelName(LogLevel level){
switch(level){
case LogLevel::DEBUG:
return "DEBUG";
case LogLevel::INFO:
return "INFO";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::ERROR:
return "ERROR";
case LogLevel::CRITICAL:
return "CRITICAL";
default:
return "NON";
}
}
const char *getLevelColor(LogLevel level){
switch(level){
case LogLevel::DEBUG:
return "0;37";
case LogLevel::INFO:
return "0";
case LogLevel::WARNING:
return "1;33";
case LogLevel::ERROR:
return "1;31";
case LogLevel::CRITICAL:
return "1;35";
default:
return "0";
}
}
LogLevel logLevel = LogLevel::INFO;
void log(const std::string &msg, LogLevel level) {
if(level >= logLevel) {
std::string levelName = getLevelName(level);
int maxLevelNameLength = 8;
std::cout << "[";
//color and level name;lo
std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m";
std::cout << "] ";
for (int i = levelName.size(); i < maxLevelNameLength; i++) {
std::cout << " ";
}
std::cout << msg << "\n";
}
}
void setLogLevel(LogLevel level) {
logLevel = level;
}

19
src/log.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef LIBRARY_LOG_H
#define LIBRARY_LOG_H
#include <string>
enum LogLevel{
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
CRITICAL = 5,
OFF = 6
};
void log(const std::string &msg, LogLevel level = LogLevel::INFO);
void setLogLevel(LogLevel level);
#endif //LIBRARY_LOG_H