From 97b975b036790657de83dd3521ff5501d3755bee Mon Sep 17 00:00:00 2001 From: Seppe Dekeyser Date: Mon, 14 Dec 2020 22:15:27 +0100 Subject: [PATCH] Implemented logging to file --- logtools/src/logtools.h | 88 ++++++++++++++++++++++++++++++----------- logtools/src/main.cpp | 3 +- premake5.lua | 2 +- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/logtools/src/logtools.h b/logtools/src/logtools.h index ea52d14..3ccf7ab 100644 --- a/logtools/src/logtools.h +++ b/logtools/src/logtools.h @@ -99,10 +99,14 @@ //////////////// Main Logger //////////////// #include +#include #include +#include +#include #include #include #include +#include enum class LogLevel { @@ -125,19 +129,31 @@ struct LogBinding struct LoggerSettings { - bool showDate; - bool showTime; + bool showDate = false; + bool showTime = false; }; class Logger { public: - static void Init() + static void Init(bool logToFile = false, const std::string& filePath = "", bool append = false) { #if defined(LOGTOOLS_WINDOWS) hConsole = GetStdHandle(STD_OUTPUT_HANDLE); #endif m_Settings = { false, false }; + + if (logToFile && !filePath.empty()) + { + if (append) + { + m_LogFile.open(filePath, std::fstream::out | std::fstream::ate | std::fstream::app); + } + else + { + m_LogFile.open(filePath, std::fstream::out | std::fstream::ate); + } + } } static void SetLevel(LogLevel level) @@ -149,7 +165,7 @@ class Logger { m_Settings = settings; } - + static void LogTrace(const std::string& msg) { @@ -224,9 +240,19 @@ class Logger return; SetConsoleColor(m_Bindings[static_cast(level)].color); - PrintTime(); - std::cout << "[" << m_Bindings[static_cast(level)].identifier << "] "; - std::cout << msg << std::endl; + + std::stringstream ss; + ss << PrintTime(); + ss << "[" << m_Bindings[static_cast(level)].identifier << "] "; + ss << msg << std::endl; + std::cout << ss.str(); + + if (m_LogFile && m_LogFile.is_open()) + { + m_LogFile << ss.str(); + m_LogFile.flush(); + } + ResetConsoleColor(); } @@ -236,17 +262,30 @@ class Logger return; SetConsoleColor(m_Bindings[static_cast(level)].color); - PrintTime(); - std::cout << "[" << m_Bindings[static_cast(level)].identifier << "] "; - std::vprintf(fmt, args); - std::cout << std::endl; + + std::stringstream ss; + ss << PrintTime(); + ss << "[" << m_Bindings[static_cast(level)].identifier << "] "; + + std::vector buf(1 + std::vsnprintf(nullptr, 0, fmt, args)); + std::vsnprintf(buf.data(), buf.size(), fmt, args); + ss << buf.data() << std::endl; + + std::cout << ss.str(); + + if (m_LogFile && m_LogFile.is_open()) + { + m_LogFile << ss.str(); + m_LogFile.flush(); + } + ResetConsoleColor(); } - static void PrintTime() + static std::string PrintTime() { if (!m_Settings.showDate && !m_Settings.showTime) - return; + return ""; time_t t = time(nullptr); std::tm now{}; @@ -257,30 +296,30 @@ class Logger localtime_r(&t, &now); #endif - std::printf("["); + std::stringstream ss; + ss << "["; if (m_Settings.showDate) { - std::printf("%.4d-%.2d-%.2d", - now.tm_year + 1900, - now.tm_mon + 1, - now.tm_hour); + ss << std::setw(4) << now.tm_year + 1900 << "-"; + ss << std::setw(2) << std::setfill('0') << now.tm_mon + 1 << "-"; + ss << std::setw(2) << std::setfill('0') << now.tm_hour; } if (m_Settings.showDate && m_Settings.showTime) { - std::printf(" "); + ss << " "; } if (m_Settings.showTime) { - std::printf("%.2d:%.2d:%.2d", - now.tm_hour, - now.tm_min, - now.tm_sec); + ss << std::setw(2) << std::setfill('0') << now.tm_hour << ":"; + ss << std::setw(2) << std::setfill('0') << now.tm_min << ":"; + ss << std::setw(2) << std::setfill('0') << now.tm_sec; } - std::printf("] "); + ss << "] "; + return ss.str(); } private: @@ -340,6 +379,7 @@ class Logger inline static LogLevel m_LogLevel = LogLevel::Trace; inline static LoggerSettings m_Settings = { false, false }; + inline static std::fstream m_LogFile; }; #endif //INCLUDE_LOGTOOLS_H diff --git a/logtools/src/main.cpp b/logtools/src/main.cpp index dd358ac..d2f91cc 100644 --- a/logtools/src/main.cpp +++ b/logtools/src/main.cpp @@ -1,10 +1,9 @@ #include "logtools.h" #include -#include int main() { - Logger::Init(); // Very important! + Logger::Init(true, "output.log", false); // Very important! Logger::Configure({ true, true }); diff --git a/premake5.lua b/premake5.lua index 7fdcc6b..290b877 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1,5 +1,5 @@ workspace "logtools" - architecture "x64" + architecture "x86_64" configurations {