SDL  2.0
SDL_error.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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
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 
45  /* If we are in debug mode, print out the error message */
47  }
48  }
49 
50  return -1;
51 }
52 
53 /* Available for backwards compatibility */
54 const char *
56 {
57  const SDL_error *error = SDL_GetErrBuf();
58  return error->error ? error->str : "";
59 }
60 
61 void
63 {
64  SDL_GetErrBuf()->error = 0;
65 }
66 
67 /* Very common errors go here */
68 int
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());
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: */
#define SDL_memset
#define SDL_LogGetPriority
#define SDL_strlcpy
#define SDL_vsnprintf
#define SDL_LogDebug
const char * SDL_GetError(void)
Get the last error message that was set.
Definition: SDL_error.c:55
char * SDL_GetErrorMsg(char *errstr, int maxlen)
Get the last error message that was set for the current thread.
Definition: SDL_error.c:106
int SDL_Error(SDL_errorcode code)
Definition: SDL_error.c:69
int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Set the error message for the current thread.
Definition: SDL_error.c:31
void SDL_ClearError(void)
Clear the error message for the current thread.
Definition: SDL_error.c:62
SDL_errorcode
Definition: SDL_error.h:92
@ SDL_EFSEEK
Definition: SDL_error.h:96
@ SDL_EFWRITE
Definition: SDL_error.h:95
@ SDL_UNSUPPORTED
Definition: SDL_error.h:97
@ SDL_EFREAD
Definition: SDL_error.h:94
@ SDL_ENOMEM
Definition: SDL_error.h:93
#define ERR_MAX_STRLEN
Definition: SDL_error_c.h:30
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:205
@ SDL_LOG_PRIORITY_DEBUG
Definition: SDL_log.h:105
@ SDL_LOG_CATEGORY_ERROR
Definition: SDL_log.h:67
#define main
Definition: SDL_main.h:109
GLuint buffer
#define SDL_PRINTF_FORMAT_STRING
Definition: SDL_stdinc.h:306
#define NULL
Definition: begin_code.h:163
char str[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:35