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