Windows color codes.

This commit is contained in:
2024-06-16 18:49:38 -07:00
parent eb21b8a81f
commit 4b512df77a
6 changed files with 378 additions and 43 deletions

View File

@@ -16,8 +16,14 @@ include(cmake/CPM.cmake)
file(GLOB_RECURSE jlog_HEADERS "include/jlog/*.h" "include/jlog/*.hpp")
file(GLOB_RECURSE jlog_SRC "src/jlog/*.c" "src/jlog/*.cpp")
if(UNIX AND NOT APPLE)
file(GLOB_RECURSE jlog_SRC "src/jlog/linux/*.c" "src/jlog/linux/*.cpp")
endif()
if(WIN32)
file(GLOB_RECURSE jlog_SRC "src/jlog/windows/*.c" "src/jlog/windows/*.cpp")
endif()
include_directories("include")
CPMAddPackage(

View File

@@ -4,8 +4,37 @@
#include <cstdint>
#include <format>
#define ESC "\033["
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _WIN32
namespace jlog::ansi_escape_codes
{
const WORD FG_BLACK = 0;
const WORD FG_RED = FOREGROUND_RED;
const WORD FG_GREEN = FOREGROUND_GREEN;
const WORD FG_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
const WORD FG_BLUE = FOREGROUND_BLUE;
const WORD FG_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE;
const WORD FG_CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD FG_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD FG_DEFAULT = FG_WHITE;
const WORD FG_BRIGHT_BLACK = 0 | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_GREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_BLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
inline void SetConsoleTextColor(WORD color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
}
#else
namespace jlog::ansi_escape_codes
{
static const std::string CURSOR_HOME = "\033[H";
@@ -107,3 +136,4 @@ namespace jlog::ansi_escape_codes
return "";
}
}
#endif

View File

@@ -9,9 +9,11 @@
#include <format>
#include <string>
#include <iostream>
#include <chrono>
#include <Event.h>
#include <fstream>
#include <jlog/ansi_escape_codes.hpp>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __linux__
#define FUNCTION __PRETTY_FUNCTION__
@@ -21,7 +23,6 @@
#define FUNCTION __FUNCSIG__
#endif
namespace jlog
{
enum class severity
@@ -41,108 +42,96 @@ namespace jlog
std::string timestamp;
};
static Event<LogEntry> on_log = Event<LogEntry>();
inline std::vector<LogEntry> log_history;
inline std::vector<LogEntry> log_history;
struct token
{
std::string colorCode = "";
#ifdef _WIN32
WORD colorCode = ansi_escape_codes::FG_DEFAULT;
#else
std::string colorCode = ansi_escape_codes::RESET;
#endif
std::string content = "";
std::string delimiter = "[]";
};
std::string get_timestamp();
void log_to_console(const std::string& message);
void log_to_file(const std::string& message);
void log(std::vector<token> tokens);
void info (const std::string& message);
void sinfo (const std::string& message);
void usinfo (const std::string& message);
void info(const std::string& message);
void sinfo(const std::string& message);
void usinfo(const std::string& message);
void verbose(const std::string& message);
void sverbose(const std::string& message);
void usverbose(const std::string& message);
void debug (const std::string& message);
void sdebug (const std::string& message);
void usdebug (const std::string& message);
void debug(const std::string& message);
void sdebug(const std::string& message);
void usdebug(const std::string& message);
void warning(const std::string& message);
void swarning(const std::string& message);
void uswarning(const std::string& message);
void error(const std::string& message);
void serror(const std::string& message);
void userror(const std::string& message);
void error (const std::string& message);
void serror (const std::string& message);
void userror (const std::string& message);
void fatal (const std::string& message);
void sfatal (const std::string& message);
void usfatal (const std::string& message);
void fatal(const std::string& message);
void sfatal(const std::string& message);
void usfatal(const std::string& message);
void info_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void verbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void debug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void warning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void swarning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void uswarning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void error_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void serror_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void userror_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void fatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
}
#define INFO(i) jlog::info_spec(i, FUNCTION, __FILE__, __LINE__);
#define SINFO(i) jlog::sinfo_spec(i, FUNCTION, __FILE__, __LINE__);
#define USINFO(i) jlog::usinfo_spec(i, FUNCTION, __FILE__, __LINE__);
#define VERBOSE(i) jlog::verbose_spec(i, FUNCTION, __FILE__, __LINE__);
#define SVERBOSE(i) jlog::sverbose_spec(i, FUNCTION, __FILE__, __LINE__);
#define USVERBOSE(i) jlog::usverbose_spec(i, FUNCTION, __FILE__, __LINE__);
#define DEBUG(i) jlog::debug_spec(i, FUNCTION, __FILE__, __LINE__);
#define SDEBUG(i) jlog::sdebug_spec(i, FUNCTION, __FILE__, __LINE__);
#define USDEBUG(i) jlog::usdebug_spec(i, FUNCTION, __FILE__, __LINE__);
#define WARNING(i) jlog::warning_spec(i, FUNCTION, __FILE__, __LINE__);
#define SWARNING(i) jlog::swarning_spec(i, FUNCTION, __FILE__, __LINE__);
#define USWARNING(i) jlog::uswarning_spec(i, FUNCTION, __FILE__, __LINE__);
#define ERROR(i) jlog::error_spec(i, FUNCTION, __FILE__, __LINE__);
#define SERROR(i) jlog::serror_spec(i, FUNCTION, __FILE__, __LINE__);
#define USERROR(i) jlog::userror_spec(i, FUNCTION, __FILE__, __LINE__);
#define FATAL(i) jlog::fatal_spec(i, FUNCTION, __FILE__, __LINE__);
#define SFATAL(i) jlog::sfatal_spec(i, FUNCTION, __FILE__, __LINE__);
#define USFATAL(i) jlog::usfatal_spec(i, FUNCTION, __FILE__, __LINE__);

View File

@@ -19,7 +19,7 @@ int main()
USINFO("This is EVEN less useful information.");
USDEBUG("Ultra compact debugging information.");
USVERBOSE("Isn't this an oxymoron?");
USWARNING("Captain Quark grade miscalculation!");
USWARNING("Captain Qwark grade miscalculation!");
USERROR("You're fucked!");
USFATAL("You're super fucked!!!");

View File

@@ -2,11 +2,9 @@
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
#include <string>
#include <fstream>
namespace jlog {
static std::string get_timestamp();
static void log_to_console(const std::string& message);
static void log_to_file(const std::string& message);
std::string get_timestamp()
{

312
src/jlog/windows/jlog.cpp Normal file
View File

@@ -0,0 +1,312 @@
#include <source_location>
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
#include <string>
#include <chrono>
#include <fstream>
#include <iostream>
#include <windows.h>
namespace jlog {
std::string get_timestamp() {
using namespace std::chrono;
auto const timestamp = current_zone()->to_local(system_clock::now());
auto dp = floor<days>(timestamp);
year_month_day ymd{floor<days>(timestamp)};
hh_mm_ss time{floor<milliseconds>(timestamp - dp)};
auto y = ymd.year();
auto m = ymd.month();
auto d = ymd.day();
auto h = time.hours();
auto M = time.minutes();
auto s = time.seconds();
auto ms = time.subseconds();
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
}
void log_to_console(const std::string& message) {
std::cout << message;
}
void log_to_file(const std::string& message) {
std::ofstream latest_log("latest.log", std::ios_base::app);
latest_log << message;
latest_log.close();
}
void log(std::vector<token> tokens) {
for (const token& t : tokens) {
if (!t.delimiter.empty()) {
ansi_escape_codes::SetConsoleTextColor(t.colorCode);
log_to_console(std::string(1, t.delimiter[0]) + t.content + std::string(1, t.delimiter[1]) + " ");
log_to_file(std::string(1, t.delimiter[0]) + t.content + std::string(1, t.delimiter[1]) + " ");
} else {
ansi_escape_codes::SetConsoleTextColor(t.colorCode);
log_to_console(t.content + " ");
log_to_file(t.content + " ");
}
}
ansi_escape_codes::SetConsoleTextColor(ansi_escape_codes::FG_DEFAULT);
log_to_console("\n");
log_to_file("\n");
}
void direct(const std::string& message) {
log({{.content = message, .delimiter = ""}});
}
void info(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sinfo(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usinfo(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void verbose(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sverbose(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usverbose(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void debug(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sdebug(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usdebug(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void warning(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void swarning(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void uswarning(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void error(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void serror(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void userror(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void fatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sfatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usfatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void info_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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 = ansi_escape_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 = ansi_escape_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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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 = ansi_escape_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 = ansi_escape_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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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 = ansi_escape_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 = ansi_escape_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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .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 = ansi_escape_codes::FG_YELLOW, .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 = ansi_escape_codes::FG_YELLOW, .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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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({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 = ansi_escape_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 = ansi_escape_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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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({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 = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_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 = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
}