diff --git a/include/jlog/nt_color_codes.hpp b/include/jlog/nt_color_codes.hpp index 0a3c43d..fe97259 100644 --- a/include/jlog/nt_color_codes.hpp +++ b/include/jlog/nt_color_codes.hpp @@ -2,6 +2,7 @@ // NT color codes taken from windows.h / consoleapi2.h + #ifndef WORD #define WORD unsigned short #endif diff --git a/src/jlog/linux/jlog.cpp b/src/jlog/linux/jlog.cpp index de61efa..ec43474 100644 --- a/src/jlog/linux/jlog.cpp +++ b/src/jlog/linux/jlog.cpp @@ -1,7 +1,9 @@ -#include +//#include -namespace jlog { +//namespace jlog { + + /* void log(const std::vector& tokens, const std::string& filename) { for (const token& t : tokens) @@ -40,6 +42,7 @@ namespace jlog { return t.content + " "; } + */ /* void log(const std::vector tokens, const std::string& filename) @@ -50,4 +53,4 @@ namespace jlog { log_to_file(filename, "\n"); } */ -} \ No newline at end of file +//} \ No newline at end of file diff --git a/src/jlog/shared/jlog.cpp b/src/jlog/shared/jlog.cpp index 07758d1..4413e28 100644 --- a/src/jlog/shared/jlog.cpp +++ b/src/jlog/shared/jlog.cpp @@ -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 + 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 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); + } + + 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& 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) { @@ -84,7 +124,7 @@ namespace jlog 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( @@ -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}); - } } \ No newline at end of file diff --git a/src/jlog/windows/jlog.cpp b/src/jlog/windows/jlog.cpp index 5126f0a..6f9b331 100644 --- a/src/jlog/windows/jlog.cpp +++ b/src/jlog/windows/jlog.cpp @@ -1,15 +1,20 @@ -#define NOMINMAX -#include -#include -#include +//#define NOMINMAX +//#include +//#include +//#include +/* inline void SetConsoleTextColor(WORD color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); } +*/ -namespace jlog -{ +//namespace jlog +//{ + + + /* void log(const std::vector& 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"); } */ -} +//}