Forcing abstraction to work with windows and beating windows into submission to work like a good boy

This commit is contained in:
2024-07-01 14:05:12 -04:00
parent f2651b58df
commit 47caaba587
4 changed files with 80 additions and 185 deletions

View File

@@ -2,6 +2,7 @@
// NT color codes taken from windows.h / consoleapi2.h
#ifndef WORD
#define WORD unsigned short
#endif

View File

@@ -1,7 +1,9 @@
#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)
@@ -40,6 +42,7 @@ namespace jlog {
return t.content + " ";
}
*/
/*
void log(const std::vector<token> tokens, const std::string& filename)
@@ -50,4 +53,4 @@ namespace jlog {
log_to_file(filename, "\n");
}
*/
}
//}

View File

@@ -33,6 +33,18 @@ namespace jlog
void log_to_console(const std::string& message)
{
// Beat windows into submission and make it use ANSI color codes
// This also looks fugly, but it works
#ifdef __WIN32
#include <windows.h>
HANDLE handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
GetConsoleMode( handleOut , &consoleMode);
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
consoleMode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode( handleOut , consoleMode );
#endif
std::cout << message;
}
@@ -55,25 +67,53 @@ namespace jlog
stream << message;
}
//std::string toks2msg(std::vector<token> tokens, std::function<std::string(token)> formatter)
//{
// std::string msg;
// for (const token& t: tokens)
// {
// msg += formatter(t);
// }
// return msg;
//}
std::string toks2msg(std::vector<token> tokens, std::function<std::string(token)> formatter)
{
std::string msg;
for (const token& t: tokens)
{
msg += formatter(t);
}
return msg;
}
//std::string toks2consoleMsg(std::vector<token> tokens)
//{
// return toks2msg(tokens, consoleMsgFormatter);
//}
std::string toks2consoleMsg(std::vector<token> tokens)
{
return toks2msg(tokens, consoleMsgFormatter);
}
//std::string toks2logfileMsg(std::vector<token> tokens)
//{
// return toks2msg(tokens, logfileMsgFormatter);
//}
std::string toks2logfileMsg(std::vector<token> tokens)
{
return toks2msg(tokens, logfileMsgFormatter);
}
std::string consoleMsgFormatter(token t)
{
if (!t.delimiter.empty())
{
return std::format("{}{}{}{}{} ", t.colorCode.ansi_code, t.delimiter[0], t.content, t.delimiter[1], ansi_escape_codes::RESET);
}
return std::format("{}{}{} ", t.colorCode.ansi_code, t.content, ansi_escape_codes::RESET);
}
std::string logfileMsgFormatter(token t)
{
if (!t.delimiter.empty())
{
return std::format("{}{}{} ", t.delimiter[0], t.content, t.delimiter[1]);
}
return t.content + " ";
}
void log(const std::vector<token>& 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<token>& tokens)
{
@@ -84,7 +124,7 @@ namespace jlog
void ltlog(const std::vector<token>& tokens)
{
log(tokens);
//log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n");
log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n");
}
std::vector<token> trace_format(
@@ -208,161 +248,4 @@ namespace jlog
tokens.insert(tokens.end(), lf_tokens.begin(), lf_tokens.end());
return tokens;
}
void info_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void verbose_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void debug_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void warning_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void swarning_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void uswarning_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void error_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_GREEN, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void serror_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void userror_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void fatal_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto timestamp = token{.content = jlog::get_timestamp()};
auto severity = token{.colorCode = color_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line)
{
auto severity = token{.colorCode = color_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
}

View File

@@ -1,15 +1,20 @@
#define NOMINMAX
#include <Windows.h>
#include <jlog/jlog.hpp>
#include <string>
//#define NOMINMAX
//#include <Windows.h>
//#include <jlog/jlog.hpp>
//#include <string>
/*
inline void SetConsoleTextColor(WORD color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
*/
namespace jlog
{
//namespace jlog
//{
/*
void log(const std::vector<token>& tokens, const std::string& filename)
{
for (const token& t : tokens)
@@ -31,6 +36,7 @@ namespace jlog
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.
@@ -41,6 +47,7 @@ namespace jlog
// 3. Additionally, we can't set the console color back to defaults after returning a value.
// This is a well-intentioned abstraction, but without a full grasp of the implications, and therefore won't work without considerable redesign.
/*
std::string consoleMsgFormatter(token t)
{
std::string ft;
@@ -68,6 +75,7 @@ namespace jlog
return t.content + " ";
}
*/
// Commented out for now so we can bring it back later if need be
/*
@@ -88,4 +96,4 @@ namespace jlog
log_to_file("\n");
}
*/
}
//}