2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 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 */
25 #include "SDL_error.h"
26 #include "SDL_error_c.h"
28 #define SDL_ERRBUFIZE 1024
31 SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
33 /* Ignore call if invalid format pointer was passed */
36 SDL_error *error = SDL_GetErrBuf();
38 error->error = 1; /* mark error as valid */
41 SDL_vsnprintf(error->str, ERR_MAX_STRLEN, fmt, ap);
44 if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
45 /* If we are in debug mode, print out the error message */
46 SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", error->str);
53 /* Available for backwards compatibility */
57 const SDL_error *error = SDL_GetErrBuf();
58 return error->error ? error->str : "";
64 SDL_GetErrBuf()->error = 0;
67 /* Very common errors go here */
69 SDL_Error(SDL_errorcode code)
73 return SDL_SetError("Out of memory");
75 return SDL_SetError("Error reading from datastream");
77 return SDL_SetError("Error writing to datastream");
79 return SDL_SetError("Error seeking in datastream");
81 return SDL_SetError("That operation is not supported");
83 return SDL_SetError("Unknown SDL error");
89 main(int argc, char *argv[])
91 char buffer[BUFSIZ + 1];
93 SDL_SetError("Hi there!");
94 printf("Error 1: %s\n", SDL_GetError());
96 SDL_memset(buffer, '1', BUFSIZ);
98 SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
99 printf("Error 2: %s\n", SDL_GetError());
106 SDL_GetErrorMsg(char *errstr, int maxlen)
108 const SDL_error *error = SDL_GetErrBuf();
111 SDL_strlcpy(errstr, error->str, maxlen);
119 /* vi: set ts=4 sw=4 expandtab: */