Unfortunate regression to get Windows ColorCodes working again. Will clean up shortly. (Test On Linux Also)

This commit is contained in:
2024-06-28 14:45:04 -04:00
parent 99c1fe9f4c
commit 83df783e7c
4 changed files with 68 additions and 32 deletions

View File

@@ -90,9 +90,9 @@ namespace jlog
/// logging callback (Event) /// logging callback (Event)
/// @note This file is implemented differently per-platform to handle differences in console color handling. /// @note This file is implemented differently per-platform to handle differences in console color handling.
/// @see windows/jlog.cpp linux/jlog.cpp /// @see windows/jlog.cpp linux/jlog.cpp
void log(const std::vector<token> tokens, const std::string& filename); void log(const std::vector<token>& tokens, const std::string& filename);
void log(const std::vector<token> tokens); void log(const std::vector<token>& tokens);
void ltlog(const std::vector<token> tokens); // Just for debug purposes void ltlog(const std::vector<token>& tokens); // Just for debug purposes
#pragma region Generic Formatters #pragma region Generic Formatters

View File

@@ -1,6 +1,26 @@
#include <jlog/jlog.hpp> #include <jlog/jlog.hpp>
namespace jlog { namespace jlog {
void log(const std::vector<token>& 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) std::string consoleMsgFormatter(token t)
{ {
if (!t.delimiter.empty()) if (!t.delimiter.empty())

View File

@@ -55,44 +55,36 @@ namespace jlog
stream << message; stream << message;
} }
std::string toks2msg(std::vector<token> tokens, std::function<std::string(token)> formatter) //std::string toks2msg(std::vector<token> tokens, std::function<std::string(token)> formatter)
{ //{
std::string msg; // std::string msg;
for (const token& t: tokens) // for (const token& t: tokens)
{ // {
msg += formatter(t); // msg += formatter(t);
} // }
return msg; // return msg;
} //}
std::string toks2consoleMsg(std::vector<token> tokens) //std::string toks2consoleMsg(std::vector<token> tokens)
{ //{
return toks2msg(tokens, consoleMsgFormatter); // return toks2msg(tokens, consoleMsgFormatter);
} //}
std::string toks2logfileMsg(std::vector<token> tokens) //std::string toks2logfileMsg(std::vector<token> tokens)
{ //{
return toks2msg(tokens, logfileMsgFormatter); // return toks2msg(tokens, logfileMsgFormatter);
} //}
void log(const std::vector<token> tokens, const std::string& filename) void log(const std::vector<token>& tokens)
{
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<token> tokens)
{ {
log(tokens, default_logfile); log(tokens, default_logfile);
} }
// Mainly for debug purposes // Mainly for debug purposes
void ltlog(const std::vector<token> tokens) void ltlog(const std::vector<token>& tokens)
{ {
log(tokens); log(tokens);
log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n"); //log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n");
} }
std::vector<token> trace_format( std::vector<token> trace_format(

View File

@@ -7,6 +7,30 @@ inline void SetConsoleTextColor(WORD color) {
namespace jlog namespace jlog
{ {
void log(const std::vector<token>& 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. // No fucking clue if this will work properly. Don't have a windows machine to test on right now.
// - Maxine // - Maxine
// No for a number of reasons // No for a number of reasons
@@ -27,7 +51,7 @@ namespace jlog
} }
SetConsoleTextColor(t.colorCode.nt_code); SetConsoleTextColor(t.colorCode.nt_code);
return t.content; return t.content + " ";
SetConsoleTextColor(color_codes::FG_DEFAULT.nt_code); SetConsoleTextColor(color_codes::FG_DEFAULT.nt_code);