From ad3b9cc8b032bfc86747da4e9c5cec6e7f1f5def Mon Sep 17 00:00:00 2001 From: Seppe Dekeyser Date: Tue, 21 Jul 2020 05:20:44 +0200 Subject: [PATCH] Implemented log message formatting --- logtools/src/logtools.h | 80 ++++++++++++++++++++++++++++++++++------- logtools/src/main.cpp | 4 +++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/logtools/src/logtools.h b/logtools/src/logtools.h index ab2049c..af6c648 100644 --- a/logtools/src/logtools.h +++ b/logtools/src/logtools.h @@ -49,13 +49,22 @@ #ifndef INCLUDE_LOGTOOLS_H #define INCLUDE_LOGTOOLS_H +// Platform definitions +#if defined(__linux__) +#define LOGTOOLS_LINUX +#elif defined(_WIN32) | defined(_WIN64) +#define LOGTOOLS_WINDOWS +#else +#error Unsupported platform! Logtools only works on Linux or Windows. +#endif + // Check if C++17 or greater, by checking if we have a C++17 specific feature. #if __cpp_inline_variables < 201606 #error Incorrect C++ version. Logtools requires C++ version 17 or greater. #endif ////////////////// Colors ////////////////// -#if defined(__linux__) +#if defined(LOGTOOLS_LINUX) // Linux Colors #define BLACK "\033[30m" @@ -69,7 +78,7 @@ #define RESET "\033[0m" -#elif defined(_WIN32) || defined(_WIN64) +#elif defined(LOGTOOLS_WINDOWS) // Windows Colors #define WIN32_LEAN_AND_MEAN @@ -91,6 +100,8 @@ //////////////// Main Logger //////////////// #include #include +#include +#include enum LogLevel { @@ -104,7 +115,7 @@ class Logger public: static void Init() { -#if defined(_WIN32) || defined(_WIN64) +#if defined(LOGTOOLS_WINDOWS) hConsole = GetStdHandle(STD_OUTPUT_HANDLE); #endif } @@ -142,29 +153,74 @@ class Logger LogColor("[ERROR] " + msg, RED); } + static void LogInfo(const char* fmt, ...) + { + va_list args; + va_start(args, fmt); + SetConsoleColor(WHITE); + std::cout << "[INFO] "; + std::vprintf(fmt, args); + std::cout << std::endl; + ResetConsoleColor(); + va_end(args); + } + + static void LogWarning(const char* fmt, ...) + { + va_list args; + va_start(args, fmt); + SetConsoleColor(YELLOW); + std::cout << "[WARNING] "; + std::vprintf(fmt, args); + std::cout << std::endl; + ResetConsoleColor(); + va_end(args); + } + + static void LogError(const char* fmt, ...) + { + va_list args; + va_start(args, fmt); + SetConsoleColor(RED); + std::cout << "[ERROR] "; + std::vprintf(fmt, args); + std::cout << std::endl; + ResetConsoleColor(); + va_end(args); + } + private: -#if defined(__linux__) +#if defined(LOGTOOLS_LINUX) // Linux specific - static void ResetConsoleColors() + static void SetConsoleColor(const std::string& color) + { + std::cout << color; + } + static void ResetConsoleColor() { std::cout << RESET; } static void LogColor(const std::string& msg, const std::string& color) { - std::cout << color << msg << std::endl; - ResetConsoleColors(); + SetConsoleColor(color); + std::cout << msg << std::endl; + ResetConsoleColor(); } -#elif defined(_WIN32) || defined(_WIN64) +#elif defined(LOGTOOLS_WINDOWS) // Windows specific - static void ResetConsoleColors() + static void SetConsoleColor(const WORD color) + { + SetConsoleTextAttribute(Instance().hConsole, color); + } + static void ResetConsoleColor() { SetConsoleTextAttribute(Instance().hConsole, RESET); } static void LogColor(const std::string& msg, const WORD color) { - SetConsoleTextAttribute(Instance().hConsole, color); + SetConsoleColor(color); std::cout << msg << std::endl; - ResetConsoleColors(); + ResetConsoleColor(); } #endif @@ -175,7 +231,7 @@ class Logger } private: -#if defined(_WIN32) || defined(_WIN64) +#if defined(LOGTOOLS_WINDOWS) inline static HANDLE hConsole; #endif }; diff --git a/logtools/src/main.cpp b/logtools/src/main.cpp index cfb5691..2ecddde 100644 --- a/logtools/src/main.cpp +++ b/logtools/src/main.cpp @@ -8,4 +8,8 @@ int main() Logger::LogWarning("This is a warning"); Logger::LogError("Failed to do something important!"); Logger::Log(LogLevel::Info, "Greetings!"); + + Logger::LogInfo("Hello %d", 1); + Logger::LogWarning("Hello %s", "abc"); + Logger::LogError("Hello %.2f", 1.2345f); }