Added SDL_SetLogPriorityPrefix()

SDL_Log() no longer prints a log prefix by default for SDL_LOG_PRIORITY_INFO and below. The log prefixes can be customized with SDL_SetLogPriorityPrefix().
This commit is contained in:
Sam Lantinga 2024-04-01 20:07:51 -07:00
parent 074dd8c35f
commit 6161c437c7
8 changed files with 69 additions and 17 deletions

View file

@ -66,7 +66,7 @@ static SDL_Mutex *log_function_mutex = NULL;
#endif
/* If this list changes, update the documentation for SDL_HINT_LOGGING */
static const char *SDL_priority_prefixes[] = {
static const char * const SDL_priority_names[] = {
NULL,
"VERBOSE",
"DEBUG",
@ -75,10 +75,22 @@ static const char *SDL_priority_prefixes[] = {
"ERROR",
"CRITICAL"
};
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);
/* If this list changes, update the documentation for SDL_HINT_LOGGING */
static const char *SDL_category_prefixes[] = {
static const char * const SDL_category_names[] = {
"APP",
"ERROR",
"ASSERT",
@ -89,7 +101,7 @@ static const char *SDL_category_prefixes[] = {
"INPUT",
"TEST"
};
SDL_COMPILE_TIME_ASSERT(category_prefixes, SDL_arraysize(SDL_category_prefixes) == SDL_LOG_CATEGORY_RESERVED1);
SDL_COMPILE_TIME_ASSERT(category_names, SDL_arraysize(SDL_category_names) == SDL_LOG_CATEGORY_RESERVED1);
#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
#pragma GCC diagnostic pop
@ -172,8 +184,8 @@ static SDL_bool SDL_ParseLogCategory(const char *string, size_t length, int *cat
return SDL_TRUE;
}
for (i = 0; i < SDL_arraysize(SDL_category_prefixes); ++i) {
if (SDL_strncasecmp(string, SDL_category_prefixes[i], length) == 0) {
for (i = 0; i < SDL_arraysize(SDL_category_names); ++i) {
if (SDL_strncasecmp(string, SDL_category_names[i], length) == 0) {
*category = i;
return SDL_TRUE;
}
@ -205,7 +217,7 @@ static SDL_bool SDL_ParseLogPriority(const char *string, size_t length, SDL_LogP
}
for (i = SDL_LOG_PRIORITY_VERBOSE; i < SDL_NUM_LOG_PRIORITIES; ++i) {
if (SDL_strncasecmp(string, SDL_priority_prefixes[i], length) == 0) {
if (SDL_strncasecmp(string, SDL_priority_names[i], length) == 0) {
*priority = (SDL_LogPriority)i;
return SDL_TRUE;
}
@ -303,6 +315,24 @@ void SDL_ResetLogPriorities(void)
SDL_forced_priority = SDL_FALSE;
}
int SDL_SetLogPriorityPrefix(SDL_LogPriority priority, const char *prefix)
{
if (priority < SDL_LOG_PRIORITY_VERBOSE || priority >= SDL_NUM_LOG_PRIORITIES) {
return SDL_InvalidParamError("priority");
}
if (!prefix) {
prefix = "";
} else {
prefix = SDL_GetPersistentString(prefix);
if (!prefix) {
return -1;
}
}
SDL_priority_prefixes[priority] = prefix;
return 0;
}
void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;
@ -379,7 +409,7 @@ void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_ST
static const char *GetCategoryPrefix(int category)
{
if (category < SDL_LOG_CATEGORY_RESERVED1) {
return SDL_category_prefixes[category];
return SDL_category_names[category];
}
if (category < SDL_LOG_CATEGORY_CUSTOM) {
return "RESERVED";
@ -518,9 +548,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]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
length = SDL_strlen(SDL_priority_prefixes[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_priority_prefixes[priority], message);
tstr = WIN_UTF8ToString(output);
/* Output to debugger */
@ -566,7 +596,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_priority_prefixes[priority], message);
(void)fclose(pFile);
}
}
@ -575,7 +605,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_priority_prefixes[priority], message);
(void)fclose(pFile);
}
}
@ -584,14 +614,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_priority_prefixes[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_priority_prefixes[priority], message);
#endif
}