Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For every variadic function, add a va_list function to the public api #10907

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/SDL3/SDL_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,36 @@ extern "C" {
*
* \sa SDL_ClearError
* \sa SDL_GetError
* \sa SDL_SetErrorV
*/
extern SDL_DECLSPEC bool SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);

/**
* Set the SDL error message for the current thread.
*
* Calling this function will replace any previous error message that was set.
*
* This function always returns false, since SDL frequently uses false to
* signify a failing result, leading to this idiom:
*
* ```c
* if (error_code) {
* return SDL_SetError("This operation has failed: %d", error_code);
* }
* ```
*
* \param fmt a printf()-style message format string.
* \param ap a variable argument list..
* \returns false.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_ClearError
* \sa SDL_GetError
* \sa SDL_SetError
*/
extern SDL_DECLSPEC bool SDLCALL SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(1);

/**
* Set an error indicating that memory allocation failed.
*
Expand Down
208 changes: 199 additions & 9 deletions include/SDL3/SDL_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,32 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetLogPriorityPrefix(SDL_LogPriority priori
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogV
*/
extern SDL_DECLSPEC void SDLCALL SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);

/**
* Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO.
*
* \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.0.0.
*
* \sa SDL_Log
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(1);

/**
* Log a message with SDL_LOG_PRIORITY_TRACE.
*
Expand All @@ -254,12 +277,35 @@ extern SDL_DECLSPEC void SDLCALL SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fm
* \sa SDL_LogInfo
* \sa SDL_LogMessage
* \sa SDL_LogMessageV
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogTraceV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogTrace(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_TRACE.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogTrace
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogTraceV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_VERBOSE.
*
Expand All @@ -280,9 +326,32 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogTrace(int category, SDL_PRINTF_FORMAT_ST
* \sa SDL_LogMessage
* \sa SDL_LogMessageV
* \sa SDL_LogWarn
* \sa SDL_LogVerboseV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_VERBOSE.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogVerbose
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogVerboseV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_DEBUG.
*
Expand All @@ -304,9 +373,34 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogDebugV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogDebug(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_DEBUG.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogDebug
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogDebugV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_INFO.
*
Expand All @@ -328,9 +422,33 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogDebug(int category, SDL_PRINTF_FORMAT_ST
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogInfoV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_INFO.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogInfo
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogInfoV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_WARN.
*
Expand All @@ -352,9 +470,33 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STR
* \sa SDL_LogMessageV
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_WARN.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogWarn
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogWarnV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_ERROR.
*
Expand All @@ -376,9 +518,33 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STR
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogErrorV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogError(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_ERROR.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogError
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogErrorV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with SDL_LOG_PRIORITY_CRITICAL.
*
Expand All @@ -400,9 +566,33 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogError(int category, SDL_PRINTF_FORMAT_ST
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogCriticalV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogCritical(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);

/**
* Log a message with SDL_LOG_PRIORITY_CRITICAL.
*
* \param category the category of the message.
* \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.0.0.
*
* \sa SDL_LogCritical
* \sa SDL_LogV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogMessageV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogCriticalV(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);

/**
* Log a message with the specified category and priority.
*
Expand Down Expand Up @@ -442,15 +632,15 @@ extern SDL_DECLSPEC void SDLCALL SDL_LogMessage(int category,
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_Log
* \sa SDL_LogCritical
* \sa SDL_LogDebug
* \sa SDL_LogError
* \sa SDL_LogInfo
* \sa SDL_LogMessage
* \sa SDL_LogTrace
* \sa SDL_LogVerbose
* \sa SDL_LogWarn
* \sa SDL_LogV
* \sa SDL_LogCriticalV
* \sa SDL_LogDebugV
* \sa SDL_LogErrorV
* \sa SDL_LogInfoV
* \sa SDL_LogTraceV
* \sa SDL_LogVerboseV
* \sa SDL_LogWarnV
*/
extern SDL_DECLSPEC void SDLCALL SDL_LogMessageV(int category,
SDL_LogPriority priority,
Expand Down
23 changes: 16 additions & 7 deletions src/SDL_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,37 @@
#include "SDL_error_c.h"

bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;
bool result;

va_start(ap, fmt);
result = SDL_SetErrorV(fmt, ap);
va_end(ap);
return result;
}

bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
{
// Ignore call if invalid format pointer was passed
if (fmt) {
va_list ap;
int result;
SDL_error *error = SDL_GetErrBuf(true);
va_list ap2;

error->error = SDL_ErrorCodeGeneric;

va_start(ap, fmt);
result = SDL_vsnprintf(error->str, error->len, fmt, ap);
va_end(ap);
va_copy(ap2, ap);
result = SDL_vsnprintf(error->str, error->len, fmt, ap2);

if (result >= 0 && (size_t)result >= error->len && error->realloc_func) {
size_t len = (size_t)result + 1;
char *str = (char *)error->realloc_func(error->str, len);
if (str) {
error->str = str;
error->len = len;
va_start(ap, fmt);
(void)SDL_vsnprintf(error->str, error->len, fmt, ap);
va_end(ap);
va_copy(ap2, ap);
(void)SDL_vsnprintf(error->str, error->len, fmt, ap2);
}
}

Expand Down
Loading
Loading