SDL: Reset log prefixes on SDL_Quit()

From fe5dcf79be0c00d7220ea0a8fdecf1306d2c90d9 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 6 Aug 2024 13:01:04 -0700
Subject: [PATCH] Reset log prefixes on SDL_Quit()

---
 src/SDL_log.c | 59 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/src/SDL_log.c b/src/SDL_log.c
index 267ade6e6b6a6..2b2680f5fdef7 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -50,9 +50,12 @@ typedef struct SDL_LogLevel
     struct SDL_LogLevel *next;
 } SDL_LogLevel;
 
+
 /* The default log output function */
 static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message);
 
+static void SDL_ResetLogPrefixes(void);
+
 static SDL_LogLevel *SDL_loglevels;
 static SDL_bool SDL_forced_priority = SDL_FALSE;
 static SDL_LogPriority SDL_forced_priority_level;
@@ -77,17 +80,7 @@ static const char * const SDL_priority_names[] = {
 };
 SDL_COMPILE_TIME_ASSERT(priority_names, SDL_arraysize(SDL_priority_names) == SDL_NUM_LOG_PRIORITIES);
 
-/* If this list changes, update the documentation for SDL_HINT_LOGGING */
-static const char *SDL_priority_prefixes[] = {
-    NULL,
-    "",
-    "",
-    "",
-    "WARNING: ",
-    "ERROR: ",
-    "CRITICAL: "
-};
-SDL_COMPILE_TIME_ASSERT(priority_prefixes, SDL_arraysize(SDL_priority_prefixes) == SDL_NUM_LOG_PRIORITIES);
+static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES];
 
 /* If this list changes, update the documentation for SDL_HINT_LOGGING */
 static const char * const SDL_category_names[] = {
@@ -130,6 +123,7 @@ void SDL_InitLog(void)
 void SDL_QuitLog(void)
 {
     SDL_ResetLogPriorities();
+    SDL_ResetLogPrefixes();
 
     if (log_function_mutex) {
         SDL_DestroyMutex(log_function_mutex);
@@ -315,6 +309,35 @@ void SDL_ResetLogPriorities(void)
     SDL_forced_priority = SDL_FALSE;
 }
 
+static void SDL_ResetLogPrefixes(void)
+{
+    for (int i = 0; i < SDL_arraysize(SDL_priority_prefixes); ++i) {
+        SDL_priority_prefixes[i] = NULL;
+    }
+}
+
+static const char *SDL_GetLogPriorityPrefix(SDL_LogPriority priority)
+{
+    if (priority < SDL_LOG_PRIORITY_VERBOSE || priority >= SDL_NUM_LOG_PRIORITIES) {
+        return "";
+    }
+
+    if (SDL_priority_prefixes[priority]) {
+        return SDL_priority_prefixes[priority];
+    }
+
+    switch (priority) {
+    case SDL_LOG_PRIORITY_WARN:
+        return "WARNING: ";
+    case SDL_LOG_PRIORITY_ERROR:
+        return "ERROR: ";
+    case SDL_LOG_PRIORITY_CRITICAL:
+        return "ERROR: ";
+    default:
+        return "";
+    }
+}
+
 int SDL_SetLogPriorityPrefix(SDL_LogPriority priority, const char *prefix)
 {
     if (priority < SDL_LOG_PRIORITY_VERBOSE || priority >= SDL_NUM_LOG_PRIORITIES) {
@@ -548,9 +571,9 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
         }
 #endif /* !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK) */
 
-        length = SDL_strlen(SDL_priority_prefixes[priority]) + SDL_strlen(message) + 1 + 1 + 1;
+        length = SDL_strlen(SDL_GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
         output = SDL_small_alloc(char, length, &isstack);
-        (void)SDL_snprintf(output, length, "%s%s\r\n", SDL_priority_prefixes[priority], message);
+        (void)SDL_snprintf(output, length, "%s%s\r\n", SDL_GetLogPriorityPrefix(priority), message);
         tstr = WIN_UTF8ToString(output);
 
         /* Output to debugger */
@@ -588,7 +611,7 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
      */
     extern void SDL_NSLog(const char *prefix, const char *text);
     {
-        SDL_NSLog(SDL_priority_prefixes[priority], message);
+        SDL_NSLog(SDL_GetLogPriorityPrefix(priority), message);
         return;
     }
 #elif defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_PS2)
@@ -596,7 +619,7 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
         FILE *pFile;
         pFile = fopen("SDL_Log.txt", "a");
         if (pFile) {
-            (void)fprintf(pFile, "%s%s\n", SDL_priority_prefixes[priority], message);
+            (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
             (void)fclose(pFile);
         }
     }
@@ -605,7 +628,7 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
         FILE *pFile;
         pFile = fopen("ux0:/data/SDL_Log.txt", "a");
         if (pFile) {
-            (void)fprintf(pFile, "%s%s\n", SDL_priority_prefixes[priority], message);
+            (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
             (void)fclose(pFile);
         }
     }
@@ -614,14 +637,14 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
         FILE *pFile;
         pFile = fopen("sdmc:/3ds/SDL_Log.txt", "a");
         if (pFile) {
-            (void)fprintf(pFile, "%s%s\n", SDL_priority_prefixes[priority], message);
+            (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
             (void)fclose(pFile);
         }
     }
 #endif
 #if defined(HAVE_STDIO_H) && \
     !(defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT)))
-    (void)fprintf(stderr, "%s%s\n", SDL_priority_prefixes[priority], message);
+    (void)fprintf(stderr, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
 #endif
 }