2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "./SDL_internal.h"
23 /* Simple error handling in SDL */
26 #include "SDL_error.h"
27 #include "SDL_error_c.h"
30 /* Routine to get the thread-specific error variable */
31 #if SDL_THREADS_DISABLED
32 /* The default (non-thread-safe) global error variable */
33 static SDL_error SDL_global_error;
34 #define SDL_GetErrBuf() (&SDL_global_error)
36 extern SDL_error *SDL_GetErrBuf(void);
37 #endif /* SDL_THREADS_DISABLED */
39 #define SDL_ERRBUFIZE 1024
41 /* Private functions */
44 SDL_LookupString(const char *key)
46 /* FIXME: Add code to lookup key in language string hash-table */
50 /* Public functions */
53 SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
58 /* Ignore call if invalid format pointer was passed */
59 if (fmt == NULL) return -1;
61 /* Copy in the key, mark error as valid */
62 error = SDL_GetErrBuf();
64 SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
70 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
74 case 0: /* Malformed format string.. */
84 error->args[error->argc++].value_i = va_arg(ap, int);
87 error->args[error->argc++].value_f = va_arg(ap, double);
90 error->args[error->argc++].value_ptr = va_arg(ap, void *);
95 const char *str = va_arg(ap, const char *);
98 SDL_strlcpy((char *) error->args[i].buf, str,
106 if (error->argc >= ERR_MAX_ARGS) {
113 /* If we are in debug mode, print out an error message */
114 SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
119 /* This function has a bit more overhead than most error functions
120 so that it supports internationalization and thread-safe errors.
123 SDL_GetErrorMsg(char *errstr, int maxlen)
127 /* Clear the error string */
131 /* Get the thread-safe error, and print it out */
132 error = SDL_GetErrBuf();
139 fmt = SDL_LookupString(error->key);
141 while (*fmt && (maxlen > 0)) {
143 char tmp[32], *spot = tmp;
145 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
146 && spot < (tmp + SDL_arraysize(tmp) - 2)) {
164 SDL_snprintf(msg, maxlen, tmp,
165 error->args[argi++].value_i);
174 SDL_snprintf(msg, maxlen, tmp,
175 error->args[argi++].value_f);
184 SDL_snprintf(msg, maxlen, tmp,
185 error->args[argi++].value_ptr);
194 SDL_snprintf(msg, maxlen, tmp,
195 SDL_LookupString(error->args[argi++].
210 /* slide back if we've overshot the end of our buffer. */
212 msg -= (-maxlen) + 1;
215 *msg = 0; /* NULL terminate the string */
220 /* Available for backwards compatibility */
224 static char errmsg[SDL_ERRBUFIZE];
226 return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
234 error = SDL_GetErrBuf();
238 /* Very common errors go here */
240 SDL_Error(SDL_errorcode code)
244 return SDL_SetError("Out of memory");
246 return SDL_SetError("Error reading from datastream");
248 return SDL_SetError("Error writing to datastream");
250 return SDL_SetError("Error seeking in datastream");
251 case SDL_UNSUPPORTED:
252 return SDL_SetError("That operation is not supported");
254 return SDL_SetError("Unknown SDL error");
260 main(int argc, char *argv[])
262 char buffer[BUFSIZ + 1];
264 SDL_SetError("Hi there!");
265 printf("Error 1: %s\n", SDL_GetError());
267 SDL_memset(buffer, '1', BUFSIZ);
269 SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
270 printf("Error 2: %s\n", SDL_GetError());
275 /* vi: set ts=4 sw=4 expandtab: */