From 83df783e7c1a6bb5130350c7e42031cb1c5ba98a Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 28 Jun 2024 14:45:04 -0400 Subject: [PATCH] Unfortunate regression to get Windows ColorCodes working again. Will clean up shortly. (Test On Linux Also) --- include/jlog/jlog.hpp | 6 ++--- src/jlog/linux/jlog.cpp | 20 ++++++++++++++++ src/jlog/shared/jlog.cpp | 48 ++++++++++++++++----------------------- src/jlog/windows/jlog.cpp | 26 ++++++++++++++++++++- 4 files changed, 68 insertions(+), 32 deletions(-) diff --git a/include/jlog/jlog.hpp b/include/jlog/jlog.hpp index 8222b3c..557fd41 100644 --- a/include/jlog/jlog.hpp +++ b/include/jlog/jlog.hpp @@ -90,9 +90,9 @@ namespace jlog /// logging callback (Event) /// @note This file is implemented differently per-platform to handle differences in console color handling. /// @see windows/jlog.cpp linux/jlog.cpp - void log(const std::vector tokens, const std::string& filename); - void log(const std::vector tokens); - void ltlog(const std::vector tokens); // Just for debug purposes + void log(const std::vector& tokens, const std::string& filename); + void log(const std::vector& tokens); + void ltlog(const std::vector& tokens); // Just for debug purposes #pragma region Generic Formatters diff --git a/src/jlog/linux/jlog.cpp b/src/jlog/linux/jlog.cpp index 789889a..de61efa 100644 --- a/src/jlog/linux/jlog.cpp +++ b/src/jlog/linux/jlog.cpp @@ -1,6 +1,26 @@ #include namespace jlog { + + void log(const std::vector& tokens, const std::string& filename) + { + for (const token& t : tokens) + { + + if (!t.delimiter.empty()) { + auto formatted_str = std::format("{}{}{}{}{} ", t.colorCode.ansi_code, t.delimiter[0], t.content, t.delimiter[1], ansi_escape_codes::FG_DEFAULT); + log_to_console(formatted_str); + log_to_file(filename, formatted_str); + } else { + auto formatted_str = std::format("{}{}{} ", t.colorCode.ansi_code, t.content, ansi_escape_codes::FG_DEFAULT); + log_to_console(t.content); + log_to_file(filename, t.content); + } + } + log_to_file(filename, "\n"); + log_to_console("\n"); + } + std::string consoleMsgFormatter(token t) { if (!t.delimiter.empty()) diff --git a/src/jlog/shared/jlog.cpp b/src/jlog/shared/jlog.cpp index 5546360..07758d1 100644 --- a/src/jlog/shared/jlog.cpp +++ b/src/jlog/shared/jlog.cpp @@ -55,44 +55,36 @@ namespace jlog stream << message; } - std::string toks2msg(std::vector tokens, std::function formatter) - { - std::string msg; - for (const token& t: tokens) - { - msg += formatter(t); - } - return msg; - } + //std::string toks2msg(std::vector tokens, std::function formatter) + //{ + // std::string msg; + // for (const token& t: tokens) + // { + // msg += formatter(t); + // } + // return msg; + //} - std::string toks2consoleMsg(std::vector tokens) - { - return toks2msg(tokens, consoleMsgFormatter); - } + //std::string toks2consoleMsg(std::vector tokens) + //{ + // return toks2msg(tokens, consoleMsgFormatter); + //} - std::string toks2logfileMsg(std::vector tokens) - { - return toks2msg(tokens, logfileMsgFormatter); - } + //std::string toks2logfileMsg(std::vector tokens) + //{ + // return toks2msg(tokens, logfileMsgFormatter); + //} - void log(const std::vector tokens, const std::string& filename) - { - log_to_console(toks2consoleMsg(tokens)); - log_to_console("\n"); - log_to_file(filename, toks2logfileMsg(tokens)); - log_to_file(filename, "\n"); - } - - void log(const std::vector tokens) + void log(const std::vector& tokens) { log(tokens, default_logfile); } // Mainly for debug purposes - void ltlog(const std::vector tokens) + void ltlog(const std::vector& tokens) { log(tokens); - log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n"); + //log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n"); } std::vector trace_format( diff --git a/src/jlog/windows/jlog.cpp b/src/jlog/windows/jlog.cpp index c58bdea..8e2c857 100644 --- a/src/jlog/windows/jlog.cpp +++ b/src/jlog/windows/jlog.cpp @@ -7,6 +7,30 @@ inline void SetConsoleTextColor(WORD color) { namespace jlog { + + void log(const std::vector& tokens, const std::string& filename) + { + for (const token& t : tokens) + { + + std::string formatted_str; + if (!t.delimiter.empty()) { + formatted_str = std::format("{}{}{} ", t.delimiter[0], t.content, t.delimiter[1]); + } else { + formatted_str = t.content + " "; + } + + SetConsoleTextColor(t.colorCode.nt_code); + log_to_console(formatted_str); + SetConsoleTextColor(nt_color_codes::FG_DEFAULT); + + log_to_file(filename, formatted_str); + } + log_to_file(filename, "\n"); + log_to_console("\n"); + } + + // No fucking clue if this will work properly. Don't have a windows machine to test on right now. // - Maxine // No for a number of reasons @@ -27,7 +51,7 @@ namespace jlog } SetConsoleTextColor(t.colorCode.nt_code); - return t.content; + return t.content + " "; SetConsoleTextColor(color_codes::FG_DEFAULT.nt_code);