diff --git a/logtools/src/logtools.h b/logtools/src/logtools.h index 128b3d2..a4936d2 100644 --- a/logtools/src/logtools.h +++ b/logtools/src/logtools.h @@ -1,11 +1,11 @@ /* - * LogTools // v1.1 // small and simple cross-platform logging toolset. + * LogTools // v1.2 // small and simple cross-platform logging toolset. * ************ https://github.com/SeppahBaws/logtools ************ * * -----------------------------[ LICENSE ]----------------------------- MIT License - Copyright (c) 2020 Seppe Dekeyser + Copyright (c) 2020-2021 Seppe Dekeyser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -99,10 +99,14 @@ //////////////// Main Logger //////////////// #include +#include #include +#include +#include #include #include #include +#include enum class LogLevel { @@ -125,19 +129,36 @@ 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 Shutdown() + { + m_LogFile.close(); } static void SetLevel(LogLevel level) @@ -149,7 +170,7 @@ class Logger { m_Settings = settings; } - + static void LogTrace(const std::string& msg) { @@ -224,9 +245,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 +267,33 @@ 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 << "] "; + + va_list args2; + va_copy(args2, args); + std::vector buf(1 + std::vsnprintf(nullptr, 0, fmt, args)); + std::vsnprintf(buf.data(), buf.size(), fmt, args2); + va_end(args2); + 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 +304,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_mday; } 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 +387,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..392a128 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 }); @@ -46,4 +45,6 @@ int main() Logger::Configure({ false, false }); Logger::LogTrace("Hello World!"); + + Logger::Shutdown(); // Very important! } 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 {