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-13 13:37:49 +01:00
|
|
|
#include "log.hpp"
|
2019-12-11 13:08:29 +01:00
|
|
|
#include <fstream>
|
2019-11-05 15:08:17 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
namespace vkvm {
|
2019-12-11 13:08:29 +01:00
|
|
|
|
|
|
|
LogLevel logLevel = LogLevel::INFO;
|
|
|
|
LogLevel fileLogLevel = LogLevel::INFO;
|
2019-12-19 11:45:13 +01:00
|
|
|
std::ofstream logToFileStream;//NOLINT
|
2019-12-11 13:08:29 +01:00
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
//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";
|
|
|
|
}
|
2019-11-05 15:08:17 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//converts the level to a ansi color code
|
2019-11-13 13:37:49 +01:00
|
|
|
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";
|
|
|
|
}
|
2019-11-05 15:08:17 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:19:51 +01:00
|
|
|
//log the current time
|
2019-12-11 13:08:29 +01:00
|
|
|
auto logTime() -> std::string {
|
2019-11-13 13:37:49 +01:00
|
|
|
time_t rawtime;
|
|
|
|
time(&rawtime);
|
|
|
|
struct tm *timeinfo;
|
|
|
|
timeinfo = localtime(&rawtime);
|
|
|
|
constexpr int decimalBase = 10;
|
2019-12-11 13:08:29 +01:00
|
|
|
std::stringstream stream;
|
2019-11-12 15:03:17 +01:00
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
if (timeinfo->tm_hour < decimalBase) {
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << "0";
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << timeinfo->tm_hour;
|
|
|
|
stream << ":";
|
2019-11-13 13:37:49 +01:00
|
|
|
if (timeinfo->tm_min < decimalBase) {
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << "0";
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << timeinfo->tm_min;
|
|
|
|
stream << ":";
|
2019-11-13 13:37:49 +01:00
|
|
|
if (timeinfo->tm_sec < decimalBase) {
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << "0";
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << timeinfo->tm_sec;
|
|
|
|
return stream.str();
|
2019-11-05 15:19:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//log the message
|
2019-11-13 13:37:49 +01:00
|
|
|
auto log(LogLevel level, const std::string &msg) -> void {
|
2019-12-11 13:08:29 +01:00
|
|
|
if(level >= logLevel || level >= fileLogLevel) {
|
2019-11-13 13:37:49 +01:00
|
|
|
std::string levelName = getLevelName(level);
|
|
|
|
const int maxLevelNameLength = 8;
|
2019-11-05 15:08:17 +01:00
|
|
|
|
2019-12-11 13:08:29 +01:00
|
|
|
std::stringstream stream;
|
|
|
|
std::stringstream noColorStream;
|
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
//time
|
2019-12-11 13:08:29 +01:00
|
|
|
std::string time = logTime();
|
|
|
|
stream << "[";
|
|
|
|
stream << time;
|
|
|
|
stream << "] ";
|
|
|
|
|
|
|
|
noColorStream << "[";
|
|
|
|
noColorStream << time;
|
|
|
|
noColorStream << "] ";
|
|
|
|
|
|
|
|
//color and level name
|
|
|
|
stream << "[";
|
|
|
|
stream << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m";
|
|
|
|
stream << "] ";
|
|
|
|
for (int i = levelName.size(); i < maxLevelNameLength; i++) {
|
|
|
|
stream << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
//no color and level name
|
|
|
|
noColorStream << "[";
|
|
|
|
noColorStream << levelName;
|
|
|
|
noColorStream << "] ";
|
2019-11-13 13:37:49 +01:00
|
|
|
for (int i = levelName.size(); i < maxLevelNameLength; i++) {
|
2019-12-11 13:08:29 +01:00
|
|
|
noColorStream << " ";
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|
2019-11-05 15:19:51 +01:00
|
|
|
|
2019-12-11 13:08:29 +01:00
|
|
|
|
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
//message
|
|
|
|
for(char c : msg){
|
|
|
|
if(c == '\n'){
|
|
|
|
//intend newlines so that they align with the start of the message
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << "\n";
|
|
|
|
noColorStream << "\n";
|
2019-11-13 13:37:49 +01:00
|
|
|
const int paddingSize = 22;
|
|
|
|
for(int i = 0; i < paddingSize;i++){
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << " ";
|
|
|
|
noColorStream << " ";
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|
|
|
|
}else{
|
2019-12-11 13:08:29 +01:00
|
|
|
stream << c;
|
|
|
|
noColorStream << c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream << "\n";
|
|
|
|
noColorStream << "\n";
|
|
|
|
|
|
|
|
if(level >= logLevel){
|
|
|
|
std::cout << stream.str();
|
|
|
|
}
|
|
|
|
if(logToFileStream.is_open()){
|
|
|
|
if(level >= fileLogLevel){
|
|
|
|
logToFileStream << noColorStream.str();
|
2019-12-19 12:14:26 +01:00
|
|
|
//TODO(julian): fixme
|
2019-12-11 13:08:29 +01:00
|
|
|
logToFileStream.flush();
|
2019-11-05 15:19:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 15:08:17 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
auto setLogLevel(LogLevel level) -> void {
|
|
|
|
logLevel = level;
|
|
|
|
}
|
2019-11-05 15:08:17 +01:00
|
|
|
|
2019-12-11 13:08:29 +01:00
|
|
|
auto setLogFileLevel(LogLevel level) -> void {
|
|
|
|
fileLogLevel = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto setLogToFile(const std::string &file) -> void {
|
|
|
|
if(logToFileStream.is_open()){
|
|
|
|
logToFileStream.close();
|
|
|
|
}
|
|
|
|
if(!file.empty()){
|
|
|
|
logToFileStream.open(file, std::ofstream::app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:37:49 +01:00
|
|
|
}
|