Added SDL_GetErrorMsg() to get the error message in a thread-safe way
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Mar 2020 21:28:09 -0700
changeset 136248fb0748c2cce
parent 13623 77d56667ebe0
child 13625 d195863f8226
Added SDL_GetErrorMsg() to get the error message in a thread-safe way
include/SDL_error.h
src/SDL_error.c
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
     1.1 --- a/include/SDL_error.h	Fri Mar 13 19:19:29 2020 -0700
     1.2 +++ b/include/SDL_error.h	Fri Mar 13 21:28:09 2020 -0700
     1.3 @@ -37,9 +37,45 @@
     1.4  #endif
     1.5  
     1.6  /* Public functions */
     1.7 -/* SDL_SetError() unconditionally returns -1. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 + *  \brief Set the error message for the current thread
    1.12 + *
    1.13 + *  \return -1, there is no error handling for this function
    1.14 + */
    1.15  extern DECLSPEC int SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
    1.16 +
    1.17 +/**
    1.18 + *  \brief Get the last error message that was set
    1.19 + *
    1.20 + * SDL API functions may set error messages and then succeed, so you should
    1.21 + * only use the error value if a function fails.
    1.22 + * 
    1.23 + * This returns a pointer to a static buffer for convenience and should not
    1.24 + * be called by multiple threads simultaneously.
    1.25 + *
    1.26 + *  \return a pointer to the last error message that was set
    1.27 + */
    1.28  extern DECLSPEC const char *SDLCALL SDL_GetError(void);
    1.29 +
    1.30 +/**
    1.31 + *  \brief Get the last error message that was set for the current thread
    1.32 + *
    1.33 + * SDL API functions may set error messages and then succeed, so you should
    1.34 + * only use the error value if a function fails.
    1.35 + * 
    1.36 + *  \param errstr A buffer to fill with the last error message that was set
    1.37 + *                for the current thread
    1.38 + *  \param maxlen The size of the buffer pointed to by the errstr parameter
    1.39 + *
    1.40 + *  \return errstr
    1.41 + */
    1.42 +extern DECLSPEC char * SDLCALL SDL_GetErrorMsg(char *errstr, int maxlen);
    1.43 +
    1.44 +/**
    1.45 + *  \brief Clear the error message for the current thread
    1.46 + */
    1.47  extern DECLSPEC void SDLCALL SDL_ClearError(void);
    1.48  
    1.49  /**
     2.1 --- a/src/SDL_error.c	Fri Mar 13 19:19:29 2020 -0700
     2.2 +++ b/src/SDL_error.c	Fri Mar 13 21:28:09 2020 -0700
     2.3 @@ -39,8 +39,6 @@
     2.4  
     2.5  /* Public functions */
     2.6  
     2.7 -static char *SDL_GetErrorMsg(char *errstr, int maxlen);
     2.8 -
     2.9  int
    2.10  SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
    2.11  {
    2.12 @@ -192,7 +190,7 @@
    2.13  /* This function has a bit more overhead than most error functions
    2.14     so that it supports internationalization and thread-safe errors.
    2.15  */
    2.16 -static char *
    2.17 +char *
    2.18  SDL_GetErrorMsg(char *errstr, int maxlen)
    2.19  {
    2.20      SDL_error *error;
     3.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Fri Mar 13 19:19:29 2020 -0700
     3.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Fri Mar 13 21:28:09 2020 -0700
     3.3 @@ -756,3 +756,4 @@
     3.4  #define SDL_JoystickSetVirtualBall SDL_JoystickSetVirtualBall_REAL
     3.5  #define SDL_JoystickSetVirtualButton SDL_JoystickSetVirtualButton_REAL
     3.6  #define SDL_JoystickSetVirtualHat SDL_JoystickSetVirtualHat_REAL
     3.7 +#define SDL_GetErrorMsg SDL_GetErrorMsg_REAL
     4.1 --- a/src/dynapi/SDL_dynapi_procs.h	Fri Mar 13 19:19:29 2020 -0700
     4.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Fri Mar 13 21:28:09 2020 -0700
     4.3 @@ -816,3 +816,4 @@
     4.4  SDL_DYNAPI_PROC(int,SDL_JoystickSetVirtualBall,(SDL_Joystick *a, int b, Sint16 c, Sint16 d),(a,b,c,d),return)
     4.5  SDL_DYNAPI_PROC(int,SDL_JoystickSetVirtualButton,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
     4.6  SDL_DYNAPI_PROC(int,SDL_JoystickSetVirtualHat,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
     4.7 +SDL_DYNAPI_PROC(char*,SDL_GetErrorMsg,(char *a, int b),(a,b),return)