From 492725ed637d7ac613d09c875a7133cccc9844d9 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Mon, 2 May 2022 20:43:52 -0400 Subject: [PATCH] Colored Logs --- libreborn/CMakeLists.txt | 2 +- libreborn/include/libreborn/log.h | 20 +++++++++++++++++--- libreborn/src/util/log.c | 16 ++++++++++++++++ media-layer/core/src/audio/engine.c | 4 ++-- media-layer/proxy/src/common/common.h | 2 +- mods/src/misc/logging.cpp | 2 +- 6 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 libreborn/src/util/log.c diff --git a/libreborn/CMakeLists.txt b/libreborn/CMakeLists.txt index 3739236b..63debc81 100644 --- a/libreborn/CMakeLists.txt +++ b/libreborn/CMakeLists.txt @@ -1,6 +1,6 @@ project(libreborn) -add_library(reborn-util STATIC src/util/elf.c src/util/exec.c src/util/string.c src/util/util.c) +add_library(reborn-util STATIC src/util/elf.c src/util/exec.c src/util/string.c src/util/util.c src/util/log.c) target_include_directories(reborn-util PUBLIC include) if(BUILD_ARM_COMPONENTS) diff --git a/libreborn/include/libreborn/log.h b/libreborn/include/libreborn/log.h index b850f3e2..a57dc5b1 100644 --- a/libreborn/include/libreborn/log.h +++ b/libreborn/include/libreborn/log.h @@ -3,9 +3,23 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + +// Colors +char *color_reset(); +char *color_yellow(); +char *color_faint(); +char *color_red(); + // Logging #define INFO(format, ...) { fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__); } -#define WARN(format, ...) { fprintf(stderr, "[WARN]: " format "\n", ##__VA_ARGS__); } -#define DEBUG(format, ...) { const char *debug = getenv("MCPI_DEBUG"); if (debug != NULL && strlen(debug) > 0) { fprintf(stderr, "[DEBUG]: " format "\n", ##__VA_ARGS__); } } -#define ERR(format, ...) { fprintf(stderr, "[ERR]: (%s:%i): " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } +#define WARN(format, ...) { fprintf(stderr, "%s[WARN]: " format "%s\n", color_yellow(), ##__VA_ARGS__, color_reset()); } +#define DEBUG(format, ...) { const char *debug = getenv("MCPI_DEBUG"); if (debug != NULL && strlen(debug) > 0) { fprintf(stderr, "%s[DEBUG]: " format "%s\n", color_faint(), ##__VA_ARGS__, color_reset()); } } +#define ERR(format, ...) { fprintf(stderr, "%s[ERR]: (%s:%i): " format "%s\n", color_red(), __FILE__, __LINE__, ##__VA_ARGS__, color_reset()); exit(EXIT_FAILURE); } #define IMPOSSIBLE() ERR("This Should Never Be Called") + +#ifdef __cplusplus +} +#endif diff --git a/libreborn/src/util/log.c b/libreborn/src/util/log.c new file mode 100644 index 00000000..b6a2dbcc --- /dev/null +++ b/libreborn/src/util/log.c @@ -0,0 +1,16 @@ +#include +#include + +#define COLOR(name, value) \ + char *color_##name() { \ + static char *out = NULL; \ + if (out == NULL) { \ + out = isatty(fileno(stderr)) ? "\x1b[" value "m" : ""; \ + } \ + return out; \ + } + +COLOR(reset, "0") +COLOR(yellow, "93") +COLOR(faint, "2") +COLOR(red, "91") diff --git a/media-layer/core/src/audio/engine.c b/media-layer/core/src/audio/engine.c index 17392d08..bc3a9307 100644 --- a/media-layer/core/src/audio/engine.c +++ b/media-layer/core/src/audio/engine.c @@ -48,7 +48,7 @@ void _media_audio_init() { } // Log - INFO("Loaded Audio Engine"); + DEBUG("Loaded Audio Engine"); is_loaded = 1; } @@ -80,6 +80,6 @@ void _media_audio_cleanup() { } // Log - INFO("Unloaded Audio Engine"); + DEBUG("Unloaded Audio Engine"); } } diff --git a/media-layer/proxy/src/common/common.h b/media-layer/proxy/src/common/common.h index 8299af45..23fa6729 100644 --- a/media-layer/proxy/src/common/common.h +++ b/media-layer/proxy/src/common/common.h @@ -22,7 +22,7 @@ extern "C" { #define CONNECTED_MSG "Connected" -#define PROXY_INFO(format, ...) INFO(PROXY_LOG_TAG format, ##__VA_ARGS__); +#define PROXY_INFO(format, ...) DEBUG(PROXY_LOG_TAG format, ##__VA_ARGS__); #define PROXY_ERR(format, ...) { close_connection(); ERR(PROXY_LOG_TAG format, ##__VA_ARGS__); } // Safely Send/Receive Data From The Connection diff --git a/mods/src/misc/logging.cpp b/mods/src/misc/logging.cpp index 4fa77aff..bf430419 100644 --- a/mods/src/misc/logging.cpp +++ b/mods/src/misc/logging.cpp @@ -89,7 +89,7 @@ static void Minecraft_update_injection(unsigned char *minecraft) { // Log When Game Is Saved void Level_saveLevelData_injection(unsigned char *level) { // Print Log Message - INFO("Saving Game"); + DEBUG("Saving Game"); // Call Original Method (*Level_saveLevelData)(level);