diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index da6dc26..6674f98 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -87,6 +87,7 @@ namespace vkvm { impl.sharedMemory = ptr; return ptr; } + //using a local buffer for shared memory testing return getLocalMemory(); } diff --git a/src/log.cpp b/src/log.cpp index c015f84..f957dac 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -5,9 +5,15 @@ */ #include "log.hpp" +#include #include 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); + } + } + } diff --git a/src/log.hpp b/src/log.hpp index 8d80990..1b40ad0 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -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