1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org> |
4 | |
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. |
8 | |
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: |
12 | |
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. |
20 | */ |
21 | #include "./SDL_internal.h" |
22 | |
23 | /* Simple error handling in SDL */ |
24 | |
25 | #include "SDL_error.h" |
26 | #include "SDL_error_c.h" |
27 | |
28 | #define SDL_ERRBUFIZE 1024 |
29 | |
30 | int |
31 | SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) |
32 | { |
33 | /* Ignore call if invalid format pointer was passed */ |
34 | if (fmt != NULL) { |
35 | va_list ap; |
36 | SDL_error *error = SDL_GetErrBuf(); |
37 | |
38 | error->error = 1; /* mark error as valid */ |
39 | |
40 | va_start(ap, fmt); |
41 | SDL_vsnprintf(error->str, ERR_MAX_STRLEN, fmt, ap); |
42 | va_end(ap); |
43 | |
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); |
47 | } |
48 | } |
49 | |
50 | return -1; |
51 | } |
52 | |
53 | /* Available for backwards compatibility */ |
54 | const char * |
55 | SDL_GetError(void) |
56 | { |
57 | const SDL_error *error = SDL_GetErrBuf(); |
58 | return error->error ? error->str : "" ; |
59 | } |
60 | |
61 | void |
62 | SDL_ClearError(void) |
63 | { |
64 | SDL_GetErrBuf()->error = 0; |
65 | } |
66 | |
67 | /* Very common errors go here */ |
68 | int |
69 | SDL_Error(SDL_errorcode code) |
70 | { |
71 | switch (code) { |
72 | case SDL_ENOMEM: |
73 | return SDL_SetError("Out of memory" ); |
74 | case SDL_EFREAD: |
75 | return SDL_SetError("Error reading from datastream" ); |
76 | case SDL_EFWRITE: |
77 | return SDL_SetError("Error writing to datastream" ); |
78 | case SDL_EFSEEK: |
79 | return SDL_SetError("Error seeking in datastream" ); |
80 | case SDL_UNSUPPORTED: |
81 | return SDL_SetError("That operation is not supported" ); |
82 | default: |
83 | return SDL_SetError("Unknown SDL error" ); |
84 | } |
85 | } |
86 | |
87 | #ifdef TEST_ERROR |
88 | int |
89 | main(int argc, char *argv[]) |
90 | { |
91 | char buffer[BUFSIZ + 1]; |
92 | |
93 | SDL_SetError("Hi there!" ); |
94 | printf("Error 1: %s\n" , SDL_GetError()); |
95 | SDL_ClearError(); |
96 | SDL_memset(buffer, '1', BUFSIZ); |
97 | buffer[BUFSIZ] = 0; |
98 | SDL_SetError("This is the error: %s (%f)" , buffer, 1.0); |
99 | printf("Error 2: %s\n" , SDL_GetError()); |
100 | exit(0); |
101 | } |
102 | #endif |
103 | |
104 | |
105 | char * |
106 | SDL_GetErrorMsg(char *errstr, int maxlen) |
107 | { |
108 | const SDL_error *error = SDL_GetErrBuf(); |
109 | |
110 | if (error->error) { |
111 | SDL_strlcpy(errstr, error->str, maxlen); |
112 | } else { |
113 | *errstr = '\0'; |
114 | } |
115 | |
116 | return errstr; |
117 | } |
118 | |
119 | /* vi: set ts=4 sw=4 expandtab: */ |
120 | |