2019-11-05 15:08:17 +01:00
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
/**
|
|
|
|
* @author Julian Hinxlage
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
|
|
|
|
2019-11-05 15:08:17 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//converts the level to a string of the level
|
2019-11-05 15:08:17 +01:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//converts the level to a ansi color code
|
2019-11-05 15:08:17 +01:00
|
|
|
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;
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//log the current time
|
|
|
|
void logTime(){
|
|
|
|
time_t rawtime;
|
|
|
|
time(&rawtime);
|
|
|
|
struct tm *timeinfo;
|
|
|
|
timeinfo = localtime(&rawtime);
|
|
|
|
|
|
|
|
if (timeinfo->tm_hour < 10) {
|
|
|
|
std::cout << "0";
|
|
|
|
}
|
|
|
|
std::cout << timeinfo->tm_hour;
|
|
|
|
std::cout << ":";
|
|
|
|
if (timeinfo->tm_min < 10) {
|
|
|
|
std::cout << "0";
|
|
|
|
}
|
|
|
|
std::cout << timeinfo->tm_min;
|
|
|
|
std::cout << ":";
|
|
|
|
if (timeinfo->tm_sec < 10) {
|
|
|
|
std::cout << "0";
|
|
|
|
}
|
|
|
|
std::cout << timeinfo->tm_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
//log the message
|
2019-11-05 15:08:17 +01:00
|
|
|
void log(const std::string &msg, LogLevel level) {
|
|
|
|
if(level >= logLevel) {
|
|
|
|
std::string levelName = getLevelName(level);
|
|
|
|
int maxLevelNameLength = 8;
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//time
|
2019-11-05 15:08:17 +01:00
|
|
|
std::cout << "[";
|
2019-11-05 15:19:51 +01:00
|
|
|
logTime();
|
|
|
|
std::cout << "] ";
|
|
|
|
|
2019-11-05 15:08:17 +01:00
|
|
|
//color and level name;lo
|
2019-11-05 15:19:51 +01:00
|
|
|
std::cout << "[";
|
2019-11-05 15:08:17 +01:00
|
|
|
std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m";
|
|
|
|
std::cout << "] ";
|
|
|
|
for (int i = levelName.size(); i < maxLevelNameLength; i++) {
|
|
|
|
std::cout << " ";
|
|
|
|
}
|
2019-11-05 15:19:51 +01:00
|
|
|
|
|
|
|
//message
|
|
|
|
for(char c : msg){
|
|
|
|
if(c == '\n'){
|
|
|
|
//intend newlines so that they align with the start of the message
|
|
|
|
std::cout << "\n";
|
|
|
|
for(int i = 0; i < 22;i++){
|
|
|
|
std::cout << " ";
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
std::cout << c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
2019-11-05 15:08:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLogLevel(LogLevel level) {
|
|
|
|
logLevel = level;
|
|
|
|
}
|
|
|
|
|