From cd3ca0b17ae48543d6c951f912f96794993d308f Mon Sep 17 00:00:00 2001 From: Robert DeForrest Reynolds Date: Tue, 14 Oct 2025 06:22:04 -0700 Subject: [PATCH] Added colored logging using ANSI coloring In src/raylib.h: Added ANSI colors In src/utils.c: strcpy(buffer, "LOG_TYPE: ") -> printf(ANSI_CODE "LOGTYPE: ") In examples/core_custom_logging.c: Change the example to demonstrate using ANSI colors for custom logger --- examples/core/core_custom_logging.c | 17 ++++++++++------- src/raylib.h | 11 +++++++++++ src/utils.c | 14 ++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/examples/core/core_custom_logging.c b/examples/core/core_custom_logging.c index 5ea2a4762..4c579e754 100644 --- a/examples/core/core_custom_logging.c +++ b/examples/core/core_custom_logging.c @@ -15,7 +15,7 @@ * ********************************************************************************************/ -#include "raylib.h" +#include "../src/raylib.h" #include // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen() #include // Required for: time_t, tm, time(), localtime(), strftime() @@ -23,22 +23,25 @@ // Custom logging function void CustomLog(int msgType, const char *text, va_list args) { + char timeStr[64] = { 0 }; time_t now = time(NULL); struct tm *tm_info = localtime(&now); strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tm_info); - printf("[%s] ", timeStr); switch (msgType) { - case LOG_INFO: printf("[INFO] : "); break; - case LOG_ERROR: printf("[ERROR]: "); break; - case LOG_WARNING: printf("[WARN] : "); break; - case LOG_DEBUG: printf("[DEBUG]: "); break; + case LOG_INFO: printf(ANSI_GREEN "[%s] [INFO] : ", timeStr); break; + case LOG_ERROR: printf(ANSI_YELLOW "[%s] [ERROR]: ", timeStr); break; + case LOG_WARNING: printf(ANSI_RED "[%s] [WARN] : ", timeStr); break; + case LOG_DEBUG: printf(ANSI_BLUE "[%s] [DEBUG]: ", timeStr); break; + case LOG_FATAL: printf(ANSI_BOLD ANSI_RED "[%s] [FATAL] : ", timeStr); break; default: break; } + printf("%s", ANSI_RESET); + vprintf(text, args); printf("\n"); } @@ -54,7 +57,7 @@ int main(void) const int screenHeight = 450; // Set custom logger - SetTraceLogCallback(CustomLog); + // SetTraceLogCallback(CustomLog); InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging"); diff --git a/src/raylib.h b/src/raylib.h index c57efc2e1..912b13d7f 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -570,6 +570,17 @@ typedef enum { LOG_NONE // Disable logging } TraceLogLevel; +// ANSI Colors +#define ANSI_RESET "\x1b[0m" +#define ANSI_RED "\x1b[31m" +#define ANSI_GREEN "\x1b[32m" +#define ANSI_YELLOW "\x1b[33m" +#define ANSI_BLUE "\x1b[34m" +#define ANSI_MAGENTA "\x1b[35m" +#define ANSI_CYAN "\x1b[36m" +#define ANSI_WHITE "\x1b[37m" +#define ANSI_BOLD "\x1b[1m" + // Keyboard keys (US keyboard layout) // NOTE: Use GetKeyPressed() to allow redefining // required keys for alternative layouts diff --git a/src/utils.c b/src/utils.c index 5ccbc4fa9..e87c03d43 100644 --- a/src/utils.c +++ b/src/utils.c @@ -133,15 +133,17 @@ void TraceLog(int logType, const char *text, ...) switch (logType) { - case LOG_TRACE: strcpy(buffer, "TRACE: "); break; - case LOG_DEBUG: strcpy(buffer, "DEBUG: "); break; - case LOG_INFO: strcpy(buffer, "INFO: "); break; - case LOG_WARNING: strcpy(buffer, "WARNING: "); break; - case LOG_ERROR: strcpy(buffer, "ERROR: "); break; - case LOG_FATAL: strcpy(buffer, "FATAL: "); break; + case LOG_TRACE: printf(ANSI_BLUE "TRACE: "); break; + case LOG_DEBUG: printf(ANSI_CYAN "DEBUG: "); break; + case LOG_INFO: printf(ANSI_GREEN "INFO: "); break; + case LOG_WARNING: printf(ANSI_YELLOW "WARNING: "); break; + case LOG_ERROR: printf(ANSI_RED "ERROR: "); break; + case LOG_FATAL: printf(ANSI_BOLD ANSI_RED "FATAL: "); break; default: break; } + printf("%s", ANSI_RESET); + unsigned int textSize = (unsigned int)strlen(text); memcpy(buffer + strlen(buffer), text, (textSize < (MAX_TRACELOG_MSG_LENGTH - 12))? textSize : (MAX_TRACELOG_MSG_LENGTH - 12)); strcat(buffer, "\n");