+ 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;
return ptr;
}
//using a local buffer for shared memory testing
return getLocalMemory();
}

View File

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