Skip to content

Commit

Permalink
Added SDL_OutputDebug(), SDL_OutputDebugV(), and SDL_OutputDebugString()
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 13, 2024
1 parent b766c82 commit 66647c9
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 13 deletions.
66 changes: 58 additions & 8 deletions include/SDL3/SDL_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fm
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -265,7 +265,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogTrace(int category, SDL_PRINTF_FORMAT_ST
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -288,7 +288,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -312,7 +312,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogDebug(int category, SDL_PRINTF_FORMAT_ST
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -336,7 +336,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STR
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -360,7 +360,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STR
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -384,7 +384,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogError(int category, SDL_PRINTF_FORMAT_ST
*
* \param category the category of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand All @@ -409,7 +409,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogCritical(int category, SDL_PRINTF_FORMAT
* \param category the category of the message.
* \param priority the priority of the message.
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the **fmt** string,
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
Expand Down Expand Up @@ -503,6 +503,56 @@ extern SDL_DECLSPEC void SDLCALL SDL_GetLogOutputFunction(SDL_LogOutputFunction
*/
extern SDL_DECLSPEC void SDLCALL SDL_SetLogOutputFunction(SDL_LogOutputFunction callback, void *userdata);

/**
* Output a line of text to the default debug output for the platform.
*
* The text should not include any newline characters, those will be automatically added as needed for the current platform.
*
* \param fmt a printf() style message format string.
* \param ... additional parameters matching % tokens in the `fmt` string,
* if any.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.6.
*
* \sa SDL_OutputDebugString
* \sa SDL_OutputDebugV
*/
extern SDL_DECLSPEC void SDLCALL SDL_OutputDebug(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);

/**
* Output a line of text to the default debug output for the platform.
*
* The text should not include any newline characters, those will be automatically added as needed for the current platform.
*
* \param fmt a printf() style message format string.
* \param ap a variable argument list.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.6.
*
* \sa SDL_OutputDebug
* \sa SDL_OutputDebugString
*/
extern SDL_DECLSPEC void SDLCALL SDL_OutputDebugV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(1);

/**
* Output a line of text to the default debug output for the platform.
*
* The text should not include any newline characters, those will be automatically added as needed for the current platform.
*
* \param message a line of text in UTF-8 format.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.6.
*
* \sa SDL_OutputDebug
* \sa SDL_OutputDebugV
*/
extern SDL_DECLSPEC void SDLCALL SDL_OutputDebugString(const char *message);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down
59 changes: 55 additions & 4 deletions src/SDL_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SDL_COMPILE_TIME_ASSERT(category_names, SDL_arraysize(SDL_category_names) == SDL

#ifdef SDL_PLATFORM_ANDROID
static int SDL_android_priority[] = {
ANDROID_LOG_UNKNOWN,
ANDROID_LOG_DEBUG,
ANDROID_LOG_VERBOSE,
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
Expand Down Expand Up @@ -642,8 +642,7 @@ enum {
static HANDLE stderrHandle = NULL;
#endif

static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
const char *message)
static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message)
{
#if defined(SDL_PLATFORM_WINDOWS)
// Way too many allocations here, urgh
Expand Down Expand Up @@ -735,7 +734,6 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
extern void SDL_NSLog(const char *prefix, const char *text);
{
SDL_NSLog(GetLogPriorityPrefix(priority), message);
return;
}
#elif defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_PS2)
{
Expand Down Expand Up @@ -795,3 +793,56 @@ void SDL_SetLogOutputFunction(SDL_LogOutputFunction callback, void *userdata)
}
SDL_UnlockMutex(SDL_log_function_lock);
}

void SDL_OutputDebug(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
SDL_OutputDebugV(fmt, ap);
va_end(ap);
}

void SDL_OutputDebugV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
{
char *message = NULL;
char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
size_t len_plus_term;
int len;
va_list aq;

// Render into stack buffer
va_copy(aq, ap);
len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
va_end(aq);

if (len < 0) {
return;
}

// If message truncated, allocate and re-render
if (len >= sizeof(stack_buf) && SDL_size_add_check_overflow(len, 1, &len_plus_term)) {
// Allocate exactly what we need, including the zero-terminator
message = (char *)SDL_malloc(len_plus_term);
if (!message) {
return;
}
va_copy(aq, ap);
len = SDL_vsnprintf(message, len_plus_term, fmt, aq);
va_end(aq);
} else {
message = stack_buf;
}

SDL_OutputDebugString(message);

// Free only if dynamically allocated
if (message != stack_buf) {
SDL_free(message);
}
}

void SDL_OutputDebugString(const char *message)
{
SDL_LogOutput(NULL, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INVALID, message);
}
18 changes: 17 additions & 1 deletion src/dynapi/SDL_dynapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ static void SDL_InitDynamicAPI(void);
if (str != buf) { \
jump_table.SDL_free(str); \
} \
return false; \
return false; \
} \
_static void SDLCALL SDL_OutputDebug##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
{ \
va_list ap; \
initcall; \
va_start(ap, fmt); \
jump_table.SDL_OutputDebugV(fmt, ap); \
va_end(ap); \
} \
_static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) \
{ \
Expand Down Expand Up @@ -238,6 +246,14 @@ static bool SDLCALL SDL_SetError_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char
va_end(ap);
return SDL_SetError_REAL("%s", buf);
}
static void SDLCALL SDL_OutputDebug_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;
SDL_Log_REAL("SDL3CALL SDL_OutputDebug");
va_start(ap, fmt);
SDL_OutputDebugV_REAL(fmt, ap);
va_end(ap);
}
static int SDLCALL SDL_sscanf_LOGSDLCALLS(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
{
int result;
Expand Down
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,9 @@ SDL3_0.0.0 {
SDL_DelayPrecise;
SDL_CalculateGPUTextureFormatSize;
SDL_SetErrorV;
SDL_OutputDebug;
SDL_OutputDebugV;
SDL_OutputDebugString;
# extra symbols go here (don't modify this line)
local: *;
};
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,6 @@
#define SDL_DelayPrecise SDL_DelayPrecise_REAL
#define SDL_CalculateGPUTextureFormatSize SDL_CalculateGPUTextureFormatSize_REAL
#define SDL_SetErrorV SDL_SetErrorV_REAL
#define SDL_OutputDebug SDL_OutputDebug_REAL
#define SDL_OutputDebugV SDL_OutputDebugV_REAL
#define SDL_OutputDebugString SDL_OutputDebugString_REAL
5 changes: 5 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,3 +1211,8 @@ SDL_DYNAPI_PROC(Uint32,SDL_StepBackUTF8,(const char *a, const char **b),(a,b),re
SDL_DYNAPI_PROC(void,SDL_DelayPrecise,(Uint64 a),(a),)
SDL_DYNAPI_PROC(Uint32,SDL_CalculateGPUTextureFormatSize,(SDL_GPUTextureFormat a, Uint32 b, Uint32 c, Uint32 d),(a,b,c,d),return)
SDL_DYNAPI_PROC(bool,SDL_SetErrorV,(SDL_PRINTF_FORMAT_STRING const char *a,va_list b),(a,b),return)
#ifndef SDL_DYNAPI_PROC_NO_VARARGS
SDL_DYNAPI_PROC(void,SDL_OutputDebug,(SDL_PRINTF_FORMAT_STRING const char *a,...),(a),)
#endif
SDL_DYNAPI_PROC(void,SDL_OutputDebugV,(SDL_PRINTF_FORMAT_STRING const char *a,va_list b),(a,b),)
SDL_DYNAPI_PROC(void,SDL_OutputDebugString,(const char *a),(a),)

0 comments on commit 66647c9

Please sign in to comment.