mcolortest merge #12

Merged
maxine merged 2 commits from mcolortest into main 2024-07-18 10:05:45 -04:00
5 changed files with 44 additions and 188 deletions

View File

@@ -26,7 +26,10 @@ CPMAddPackage(
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
)
include_directories(${Event_SOURCE_DIR}/include)
CPMAddPackage(
NAME mcolor
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-1.zip
)
if (UNIX)
add_library(jlog SHARED ${jlog_SRC})
@@ -37,6 +40,8 @@ if (WIN32)
target_compile_options(jlog PRIVATE /wd4005)
endif()
target_include_directories(jlog PUBLIC ${Event_SOURCE_DIR}/include ${mcolor_SOURCE_DIR}/include)
set_target_properties(jlog PROPERTIES LINKER_LANGUAGE CXX)
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
@@ -44,7 +49,7 @@ install(FILES ${jlog_HEADERS} DESTINATION include/${PROJECT_NAME})
#add_subdirectory(tests)
target_link_libraries(jlog PRIVATE Event)
target_link_libraries(jlog PRIVATE Event mcolor)
add_executable(LoggerDemo main.cpp)
target_link_libraries(LoggerDemo PUBLIC jlog)

View File

@@ -1,108 +0,0 @@
#pragma once
#include <string>
#include <cstdint>
#include <format>
namespace jlog::ansi_escape_codes
{
inline static const std::string CURSOR_HOME = "\033[H";
inline static const std::string ERASE_SCREEN_AFTER_CURSOR = "\033[0J";
inline static const std::string ERASE_SCREEN_BEFORE_CURSOR = "\033[1J";
inline static const std::string ERASE_SCREEN_ALL = "\033[2J";
inline static const std::string ERASE_LINE_AFTER_CURSOR = "\033[0K";
inline static const std::string ERASE_LINE_BEFORE_CURSOR = "\033[1K";
inline static const std::string ERASE_LINE_ALL = "\033[2K";
inline static const std::string RESET = "\033[0m";
inline static const std::string BOLD = "\033[1m";
inline static const std::string DIM = "\033[2m";
/// These are some examples of private modes, which are not defined by the spec, but are implemented in most terminals.
/// @see xterm control sequences for a more in-depth list.
/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
/// @note While these modes may be supported by most terminals, some may not work in multiplexers like tmux.
inline static const std::string CURSOR_INVISIBLE = "\033[?25l";
inline static const std::string CURSOR_VISIBLE = "\033[?25h";
inline static const std::string RESTORE_SCREEN = "\033[?47l";
inline static const std::string SAVE_SCREEN = "\033[?47h";
inline static const std::string FG_BLACK = "\033[30m";
inline static const std::string FG_RED = "\033[31m";
inline static const std::string FG_GREEN = "\033[32m";
inline static const std::string FG_YELLOW = "\033[33m";
inline static const std::string FG_BLUE = "\033[34m";
inline static const std::string FG_MAGENTA = "\033[35m";
inline static const std::string FG_CYAN = "\033[36m";
inline static const std::string FG_WHITE = "\033[37m";
inline static const std::string FG_DEFAULT = "\033[39m";
inline static const std::string FG_BRIGHT_BLACK = "\033[90m";
inline static const std::string FG_BRIGHT_RED = "\033[91m";
inline static const std::string FG_BRIGHT_GREEN = "\033[92m";
inline static const std::string FG_BRIGHT_YELLOW = "\033[93m";
inline static const std::string FG_BRIGHT_BLUE = "\033[94m";
inline static const std::string FG_BRIGHT_MAGENTA = "\033[95m";
inline static const std::string FG_BRIGHT_CYAN = "\033[96m";
inline static const std::string FG_BRIGHT_WHITE = "\033[97m";
inline static const std::string BG_BLACK = "\033[40m";
inline static const std::string BG_RED = "\033[41m";
inline static const std::string BG_GREEN = "\033[42m";
inline static const std::string BG_YELLOW = "\033[43m";
inline static const std::string BG_BLUE = "\033[44m";
inline static const std::string BG_MAGENTA = "\033[45m";
inline static const std::string BG_CYAN = "\033[46m";
inline static const std::string BG_WHITE = "\033[47m";
inline static const std::string BG_DEFAULT = "\033[49m";
inline static const std::string BG_BRIGHT_BLACK = "\033[100m";
inline static const std::string BG_BRIGHT_RED = "\033[101m";
inline static const std::string BG_BRIGHT_GREEN = "\033[102m";
inline static const std::string BG_BRIGHT_YELLOW = "\033[103m";
inline static const std::string BG_BRIGHT_BLUE = "\033[104m";
inline static const std::string BG_BRIGHT_MAGENTA = "\033[105m";
inline static const std::string BG_BRIGHT_CYAN = "\033[106m";
inline static const std::string BG_BRIGHT_WHITE = "\033[107m";
inline std::string true_color(uint8_t r, uint8_t g, uint8_t b)
{
return std::format("\033[38;2;{};{};{}m", r, g, b);
}
inline std::string cursor_to(int line, int col)
{
return "";
}
inline std::string cursor_up(int lines)
{
return "";
}
inline std::string cursor_down(int lines)
{
return "";
}
inline std::string cursor_left(int cols)
{
return "";
}
inline std::string cursor_right(int cols)
{
return "";
}
inline std::string cursor_to_col(int col)
{
return "";
}
}

View File

@@ -1,51 +0,0 @@
#pragma once
#include <jlog/ansi_escape_codes.hpp>
// Platform independent color code mapping
struct colorcode
{
std::string ansi_code;
};
namespace jlog::color_codes
{
static const colorcode FG_BLACK {ansi_escape_codes::FG_BLACK};
static const colorcode FG_RED {ansi_escape_codes::FG_RED};
static const colorcode FG_GREEN {ansi_escape_codes::FG_GREEN };
static const colorcode FG_YELLOW {ansi_escape_codes::FG_YELLOW};
static const colorcode FG_BLUE {ansi_escape_codes::FG_BLUE};
static const colorcode FG_MAGENTA {ansi_escape_codes::FG_MAGENTA};
static const colorcode FG_CYAN {ansi_escape_codes::FG_CYAN};
static const colorcode FG_WHITE {ansi_escape_codes::FG_WHITE};
static const colorcode FG_DEFAULT {ansi_escape_codes::FG_DEFAULT};
static const colorcode FG_BRIGHT_BLACK {ansi_escape_codes::FG_BRIGHT_BLACK};
static const colorcode FG_BRIGHT_RED {ansi_escape_codes::FG_BRIGHT_RED};
static const colorcode FG_BRIGHT_GREEN {ansi_escape_codes::FG_BRIGHT_GREEN};
static const colorcode FG_BRIGHT_YELLOW {ansi_escape_codes::FG_BRIGHT_YELLOW};
static const colorcode FG_BRIGHT_BLUE {ansi_escape_codes::FG_BRIGHT_MAGENTA};
static const colorcode FG_BRIGHT_MAGENTA {ansi_escape_codes::FG_BRIGHT_MAGENTA};
static const colorcode FG_BRIGHT_CYAN {ansi_escape_codes::FG_BRIGHT_CYAN};
static const colorcode FG_BRIGHT_WHITE {ansi_escape_codes::FG_BRIGHT_WHITE};
static const colorcode BG_BLACK {ansi_escape_codes::BG_BLACK};
static const colorcode BG_RED {ansi_escape_codes::BG_RED};
static const colorcode BG_GREEN {ansi_escape_codes::BG_GREEN};
static const colorcode BG_YELLOW {ansi_escape_codes::BG_YELLOW};
static const colorcode BG_BLUE {ansi_escape_codes::BG_BLUE};
static const colorcode BG_MAGENTA {ansi_escape_codes::BG_MAGENTA};
static const colorcode BG_CYAN {ansi_escape_codes::BG_CYAN};
static const colorcode BG_WHITE {ansi_escape_codes::BG_WHITE};
static const colorcode BG_DEFAULT {ansi_escape_codes::BG_DEFAULT};
static const colorcode BG_BRIGHT_BLACK {ansi_escape_codes::BG_BRIGHT_BLACK};
static const colorcode BG_BRIGHT_RED {ansi_escape_codes::BG_BRIGHT_RED};
static const colorcode BG_BRIGHT_GREEN {ansi_escape_codes::BG_BRIGHT_GREEN};
static const colorcode BG_BRIGHT_YELLOW {ansi_escape_codes::BG_BRIGHT_YELLOW};
static const colorcode BG_BRIGHT_BLUE {ansi_escape_codes::BG_BRIGHT_BLUE};
static const colorcode BG_BRIGHT_MAGENTA {ansi_escape_codes::BG_BRIGHT_MAGENTA};
static const colorcode BG_BRIGHT_CYAN {ansi_escape_codes::BG_BRIGHT_CYAN};
static const colorcode BG_BRIGHT_WHITE {ansi_escape_codes::BG_BRIGHT_WHITE};
}

View File

@@ -9,7 +9,7 @@
#include <string>
#include <Event.h>
#include <jlog/color_codes.hpp>
#include <mcolor.h>
#ifdef __linux__
#define FUNCTION __PRETTY_FUNCTION__
@@ -51,7 +51,7 @@ namespace jlog
/// These are strung together to build full logger messages in a flexible manner.
struct token
{
colorcode colorCode = color_codes::FG_DEFAULT;
mcolor::ansiColors::Colors colorCode = mcolor::ansiColors::Colors::FG_DEFAULT;
std::string content = "";
std::string delimiter = "[]";
};
@@ -108,11 +108,11 @@ namespace jlog
/// Returns a formatted sequence of tokens given a severity and message.
/// @param severity_name The severity tag to prefix to the message. Could theoretically also be a context.
/// @param message The actual message to include
/// @param severity_cc The colorcode to assign to the severity. See color_codes.hpp
/// @param severity_cc The colorcode to assign to the severity.
std::vector<token> log_format(
const std::string& severity_name,
const std::string& message,
const colorcode& severity_cc = color_codes::FG_WHITE);
const mcolor::ansiColors::Colors& severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
/// Returns a more detailed formatted sequence of tokens.
/// @param severity_name The severity tag to prefix to the message. Could theoretically also be a context.
@@ -120,14 +120,14 @@ namespace jlog
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
/// @param severity_cc The colorcode to assign to the severity. See color_codes.hpp
/// @param severity_cc The colorcode to assign to the severity.
std::vector<token> log_detailed_format(
const std::string& severity_name,
const std::string& message,
const std::string& func,
const std::string& file,
int line,
const colorcode& severity_cc = color_codes::FG_WHITE);
const mcolor::ansiColors::Colors& severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
/// Returns a token sequence pre-formatted for the INFO log level.
/// @param message The message to send out.

View File

@@ -40,6 +40,7 @@ namespace jlog
// Beat windows into submission and make it use ANSI color codes
// This also looks fugly, but it works
/*
#ifdef WIN32
HANDLE handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
@@ -49,6 +50,10 @@ namespace jlog
SetConsoleMode( handleOut , consoleMode );
SetConsoleOutputCP(CP_UTF8);
#endif
*/
#ifdef WIN32
mcolor::windowsSaneify();
#endif
std::cout << message;
}
@@ -96,10 +101,10 @@ namespace jlog
{
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("{}{}{}{}{} ", mcolor::toEscapeCode(t.colorCode), t.delimiter[0], t.content, t.delimiter[1], mcolor::toEscapeCode(mcolor::ansiColors::Colors::RESET));
}
return std::format("{}{}{} ", t.colorCode.ansi_code, t.content, ansi_escape_codes::RESET);
return std::format("{}{}{} ", mcolor::toEscapeCode(t.colorCode), t.content, mcolor::toEscapeCode(mcolor::ansiColors::Colors::RESET));
}
std::string logfileMsgFormatter(token t)
@@ -128,8 +133,13 @@ namespace jlog
// Mainly for debug purposes
void ltlog(const std::vector<token>& tokens)
{
log(tokens);
log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n");
std::vector<token> wtokens;
//auto head = token{.colorCode = ansi_escape_codes::true_color(color_codes::BG_RED.rgb.r, color_codes::BG_RED.rgb.g, color_codes::BG_RED.rgb.b), .content = "GAS"};
//wtokens.push_back(head);
wtokens.insert(wtokens.end(), tokens.begin(), tokens.end());
log(wtokens);
log_to_file("logtest.log", toks2logfileMsg(wtokens) + "\n");
}
std::vector<token> trace_format(
@@ -149,22 +159,27 @@ namespace jlog
std::vector<token> warning_format(const std::string& message)
{
return log_format("INFO", message, color_codes::FG_YELLOW);
return log_format("INFO", message, mcolor::ansiColors::Colors::FG_YELLOW);
}
std::vector<token> error_format(const std::string& message)
{
return log_format("ERROR", message, color_codes::FG_RED);
return log_format("ERROR", message, mcolor::ansiColors::Colors::FG_RED);
}
std::vector<token> fatal_format(const std::string& message)
{
return log_format("FATAL", message, mcolor::ansiColors::Colors::FG_BRIGHT_RED);
}
std::vector<token> verbose_format(const std::string& message)
{
return log_format("VERBOSE", message, color_codes::FG_CYAN);
return log_format("VERBOSE", message, mcolor::ansiColors::Colors::FG_CYAN);
}
std::vector<token> debug_format(const std::string& message)
{
return log_format("DEBUG", message, color_codes::FG_GREEN);
return log_format("DEBUG", message, mcolor::ansiColors::Colors::FG_GREEN);
}
std::vector<token> debug_detailed_format(
@@ -173,7 +188,7 @@ namespace jlog
const std::string& file,
int line)
{
return log_detailed_format("DEBUG", message, func, file, line, color_codes::FG_GREEN);
return log_detailed_format("DEBUG", message, func, file, line, mcolor::ansiColors::Colors::FG_GREEN);
}
std::vector<token> info_detailed_format(
@@ -191,7 +206,7 @@ namespace jlog
const std::string& file,
int line)
{
return log_detailed_format("WARNING", message, func, file, line, color_codes::FG_YELLOW);
return log_detailed_format("WARNING", message, func, file, line, mcolor::ansiColors::Colors::FG_YELLOW);
}
std::vector<token> error_detailed_format(
@@ -200,12 +215,7 @@ namespace jlog
const std::string& file,
int line)
{
return log_detailed_format("ERROR", message, func, file, line, color_codes::FG_RED);
}
std::vector<token> fatal_format(const std::string& message)
{
return log_format("FATAL", message, color_codes::FG_BRIGHT_RED);
return log_detailed_format("ERROR", message, func, file, line, mcolor::ansiColors::Colors::FG_RED);
}
std::vector<token> verbose_detailed_format(
@@ -214,7 +224,7 @@ namespace jlog
const std::string& file,
int line)
{
return log_detailed_format("VERBOSE", message, func, file, line, color_codes::FG_CYAN);
return log_detailed_format("VERBOSE", message, func, file, line, mcolor::ansiColors::Colors::FG_CYAN);
}
std::vector<token> fatal_detailed_format(
@@ -223,13 +233,13 @@ namespace jlog
const std::string& file,
int line)
{
return log_detailed_format("FATAL", message, func, file, line, color_codes::FG_BRIGHT_RED);
return log_detailed_format("FATAL", message, func, file, line, mcolor::ansiColors::Colors::FG_BRIGHT_RED);
}
std::vector<token> log_format(
const std::string& severity_name,
const std::string& message,
const colorcode& severity_cc)
const mcolor::ansiColors::Colors& severity_cc)
{
auto severity = token{.colorCode = severity_cc, .content = severity_name};
auto content = token{.content = message, .delimiter = ""};
@@ -242,7 +252,7 @@ namespace jlog
const std::string& func,
const std::string& file,
int line,
const colorcode& severity_cc)
const mcolor::ansiColors::Colors& severity_cc)
{
std::vector<token> tokens;
auto timestamp = token{.content = jlog::get_timestamp()};
@@ -253,4 +263,4 @@ namespace jlog
tokens.insert(tokens.end(), lf_tokens.begin(), lf_tokens.end());
return tokens;
}
}
}