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
This commit is contained in:
Robert DeForrest Reynolds 2025-10-14 06:22:04 -07:00
parent 2d7b66dd37
commit cd3ca0b17a
3 changed files with 29 additions and 13 deletions

View File

@ -15,7 +15,7 @@
*
********************************************************************************************/
#include "raylib.h"
#include "../src/raylib.h"
#include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
#include <time.h> // 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");

View File

@ -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

View File

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