Skip to content

Commit

Permalink
Reset log prefixes on SDL_Quit()
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Aug 6, 2024
1 parent 15bfef5 commit fe5dcf7
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions src/SDL_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[] = {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -588,15 +611,15 @@ 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)
{
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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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
}

Expand Down

0 comments on commit fe5dcf7

Please sign in to comment.