+ logging to files

This commit is contained in:
Julian Hinxlage 2019-12-11 13:08:29 +01:00
parent 5e97100181
commit cd7dc30cde
3 changed files with 79 additions and 26 deletions

View File

@ -87,6 +87,7 @@ namespace vkvm {
impl.sharedMemory = ptr; impl.sharedMemory = ptr;
return ptr; return ptr;
} }
//using a local buffer for shared memory testing
return getLocalMemory(); return getLocalMemory();
} }

View File

@ -5,9 +5,15 @@
*/ */
#include "log.hpp" #include "log.hpp"
#include <fstream>
#include <iostream> #include <iostream>
namespace vkvm { namespace vkvm {
LogLevel logLevel = LogLevel::INFO;
LogLevel fileLogLevel = LogLevel::INFO;
std::ofstream logToFileStream;
//converts the level to a string of the level //converts the level to a string of the level
auto getLevelName(LogLevel level) -> std::string { auto getLevelName(LogLevel level) -> std::string {
switch(level){ switch(level){
@ -44,66 +50,97 @@ namespace vkvm {
} }
} }
LogLevel logLevel = LogLevel::INFO;
//log the current time //log the current time
auto logTime() -> void { auto logTime() -> std::string {
time_t rawtime; time_t rawtime;
time(&rawtime); time(&rawtime);
struct tm *timeinfo; struct tm *timeinfo;
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
constexpr int decimalBase = 10; constexpr int decimalBase = 10;
std::stringstream stream;
if (timeinfo->tm_hour < decimalBase) { if (timeinfo->tm_hour < decimalBase) {
std::cout << "0"; stream << "0";
} }
std::cout << timeinfo->tm_hour; stream << timeinfo->tm_hour;
std::cout << ":"; stream << ":";
if (timeinfo->tm_min < decimalBase) { if (timeinfo->tm_min < decimalBase) {
std::cout << "0"; stream << "0";
} }
std::cout << timeinfo->tm_min; stream << timeinfo->tm_min;
std::cout << ":"; stream << ":";
if (timeinfo->tm_sec < decimalBase) { if (timeinfo->tm_sec < decimalBase) {
std::cout << "0"; stream << "0";
} }
std::cout << timeinfo->tm_sec; stream << timeinfo->tm_sec;
return stream.str();
} }
//log the message //log the message
auto log(LogLevel level, const std::string &msg) -> void { auto log(LogLevel level, const std::string &msg) -> void {
if(level >= logLevel) { if(level >= logLevel || level >= fileLogLevel) {
std::string levelName = getLevelName(level); std::string levelName = getLevelName(level);
const int maxLevelNameLength = 8; const int maxLevelNameLength = 8;
//time std::stringstream stream;
std::cout << "["; std::stringstream noColorStream;
logTime();
std::cout << "] ";
//color and level name;lo //time
std::cout << "["; std::string time = logTime();
std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; stream << "[";
std::cout << "] "; 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++) { for (int i = levelName.size(); i < maxLevelNameLength; i++) {
std::cout << " "; stream << " ";
} }
//no color and level name
noColorStream << "[";
noColorStream << levelName;
noColorStream << "] ";
for (int i = levelName.size(); i < maxLevelNameLength; i++) {
noColorStream << " ";
}
//message //message
for(char c : msg){ for(char c : msg){
if(c == '\n'){ if(c == '\n'){
//intend newlines so that they align with the start of the message //intend newlines so that they align with the start of the message
std::cout << "\n"; stream << "\n";
noColorStream << "\n";
const int paddingSize = 22; const int paddingSize = 22;
for(int i = 0; i < paddingSize;i++){ for(int i = 0; i < paddingSize;i++){
std::cout << " "; stream << " ";
noColorStream << " ";
} }
}else{ }else{
std::cout << c; 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();
logToFileStream.flush();
} }
} }
std::cout << "\n";
} }
} }
@ -111,4 +148,17 @@ namespace vkvm {
logLevel = level; logLevel = level;
} }
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);
}
}
} }

View File

@ -47,6 +47,8 @@ namespace vkvm {
* @since 0.1.0 * @since 0.1.0
*/ */
auto setLogLevel(LogLevel level) -> void; auto setLogLevel(LogLevel level) -> void;
auto setLogFileLevel(LogLevel level) -> void;
auto setLogToFile(const std::string &file) -> void;
} }
#endif #endif