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

@@ -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__);