SDL_error.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "./SDL_internal.h"
  19. /* Simple error handling in SDL */
  20. #include "SDL_log.h"
  21. #include "SDL_error.h"
  22. #include "SDL_error_c.h"
  23. /* Routine to get the thread-specific error variable */
  24. #if SDL_THREADS_DISABLED
  25. /* The default (non-thread-safe) global error variable */
  26. static SDL_error SDL_global_error;
  27. #define SDL_GetErrBuf() (&SDL_global_error)
  28. #else
  29. extern SDL_error *SDL_GetErrBuf(void);
  30. #endif /* SDL_THREADS_DISABLED */
  31. #define SDL_ERRBUFIZE 1024
  32. /* Private functions */
  33. static const char *
  34. SDL_LookupString(const char *key)
  35. {
  36. /* FIXME: Add code to lookup key in language string hash-table */
  37. return key;
  38. }
  39. /* Public functions */
  40. static char *SDL_GetErrorMsg(char *errstr, int maxlen);
  41. int
  42. SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  43. {
  44. va_list ap;
  45. SDL_error *error;
  46. /* Ignore call if invalid format pointer was passed */
  47. if (fmt == NULL) return -1;
  48. /* Copy in the key, mark error as valid */
  49. error = SDL_GetErrBuf();
  50. error->error = 1;
  51. SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
  52. va_start(ap, fmt);
  53. error->argc = 0;
  54. while (*fmt) {
  55. if (*fmt++ == '%') {
  56. while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
  57. ++fmt;
  58. }
  59. switch (*fmt++) {
  60. case 0: /* Malformed format string.. */
  61. --fmt;
  62. break;
  63. case 'l':
  64. switch (*fmt++) {
  65. case 0: /* Malformed format string.. */
  66. --fmt;
  67. break;
  68. case 'i': case 'd': case 'u': case 'x': case 'X':
  69. error->args[error->argc++].value_l = va_arg(ap, long);
  70. break;
  71. }
  72. break;
  73. case 'c':
  74. case 'i':
  75. case 'd':
  76. case 'u':
  77. case 'o':
  78. case 'x':
  79. case 'X':
  80. error->args[error->argc++].value_i = va_arg(ap, int);
  81. break;
  82. case 'f':
  83. error->args[error->argc++].value_f = va_arg(ap, double);
  84. break;
  85. case 'p':
  86. error->args[error->argc++].value_ptr = va_arg(ap, void *);
  87. break;
  88. case 's':
  89. {
  90. int i = error->argc;
  91. const char *str = va_arg(ap, const char *);
  92. if (str == NULL)
  93. str = "(null)";
  94. SDL_strlcpy((char *) error->args[i].buf, str,
  95. ERR_MAX_STRLEN);
  96. error->argc++;
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. if (error->argc >= ERR_MAX_ARGS) {
  103. break;
  104. }
  105. }
  106. }
  107. va_end(ap);
  108. if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
  109. /* If we are in debug mode, print out an error message
  110. * Avoid stomping on the static buffer in GetError, just
  111. * in case this is called while processing a ShowMessageBox to
  112. * show an error already in that static buffer.
  113. */
  114. char errmsg[SDL_ERRBUFIZE];
  115. SDL_GetErrorMsg(errmsg, sizeof(errmsg));
  116. SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
  117. }
  118. return -1;
  119. }
  120. /* Available for backwards compatibility */
  121. const char *
  122. SDL_GetError(void)
  123. {
  124. static char errmsg[SDL_ERRBUFIZE];
  125. return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
  126. }
  127. void
  128. SDL_ClearError(void)
  129. {
  130. SDL_error *error;
  131. error = SDL_GetErrBuf();
  132. error->error = 0;
  133. }
  134. /* Very common errors go here */
  135. int
  136. SDL_Error(SDL_errorcode code)
  137. {
  138. switch (code) {
  139. case SDL_ENOMEM:
  140. return SDL_SetError("Out of memory");
  141. case SDL_EFREAD:
  142. return SDL_SetError("Error reading from datastream");
  143. case SDL_EFWRITE:
  144. return SDL_SetError("Error writing to datastream");
  145. case SDL_EFSEEK:
  146. return SDL_SetError("Error seeking in datastream");
  147. case SDL_UNSUPPORTED:
  148. return SDL_SetError("That operation is not supported");
  149. default:
  150. return SDL_SetError("Unknown SDL error");
  151. }
  152. }
  153. #ifdef TEST_ERROR
  154. int
  155. main(int argc, char *argv[])
  156. {
  157. char buffer[BUFSIZ + 1];
  158. SDL_SetError("Hi there!");
  159. printf("Error 1: %s\n", SDL_GetError());
  160. SDL_ClearError();
  161. SDL_memset(buffer, '1', BUFSIZ);
  162. buffer[BUFSIZ] = 0;
  163. SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
  164. printf("Error 2: %s\n", SDL_GetError());
  165. exit(0);
  166. }
  167. #endif
  168. /* keep this at the end of the file so it works with GCC builds that don't
  169. support "#pragma GCC diagnostic push" ... we'll just leave the warning
  170. disabled after this. */
  171. /* this pragma arrived in GCC 4.2 and causes a warning on older GCCs! Sigh. */
  172. #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 2))))
  173. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  174. #endif
  175. /* This function has a bit more overhead than most error functions
  176. so that it supports internationalization and thread-safe errors.
  177. */
  178. static char *
  179. SDL_GetErrorMsg(char *errstr, int maxlen)
  180. {
  181. SDL_error *error;
  182. /* Clear the error string */
  183. *errstr = '\0';
  184. --maxlen;
  185. /* Get the thread-safe error, and print it out */
  186. error = SDL_GetErrBuf();
  187. if (error->error) {
  188. const char *fmt;
  189. char *msg = errstr;
  190. int len;
  191. int argi;
  192. fmt = SDL_LookupString(error->key);
  193. argi = 0;
  194. while (*fmt && (maxlen > 0)) {
  195. if (*fmt == '%') {
  196. char tmp[32], *spot = tmp;
  197. *spot++ = *fmt++;
  198. while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
  199. && spot < (tmp + SDL_arraysize(tmp) - 2)) {
  200. *spot++ = *fmt++;
  201. }
  202. if (*fmt == 'l') {
  203. *spot++ = *fmt++;
  204. *spot++ = *fmt++;
  205. *spot++ = '\0';
  206. switch (spot[-2]) {
  207. case 'i': case 'd': case 'u': case 'x': case 'X':
  208. len = SDL_snprintf(msg, maxlen, tmp,
  209. error->args[argi++].value_l);
  210. if (len > 0) {
  211. msg += len;
  212. maxlen -= len;
  213. }
  214. break;
  215. }
  216. continue;
  217. }
  218. *spot++ = *fmt++;
  219. *spot++ = '\0';
  220. switch (spot[-2]) {
  221. case '%':
  222. *msg++ = '%';
  223. maxlen -= 1;
  224. break;
  225. case 'c':
  226. case 'i':
  227. case 'd':
  228. case 'u':
  229. case 'o':
  230. case 'x':
  231. case 'X':
  232. len =
  233. SDL_snprintf(msg, maxlen, tmp,
  234. error->args[argi++].value_i);
  235. if (len > 0) {
  236. msg += len;
  237. maxlen -= len;
  238. }
  239. break;
  240. case 'f':
  241. len =
  242. SDL_snprintf(msg, maxlen, tmp,
  243. error->args[argi++].value_f);
  244. if (len > 0) {
  245. msg += len;
  246. maxlen -= len;
  247. }
  248. break;
  249. case 'p':
  250. len =
  251. SDL_snprintf(msg, maxlen, tmp,
  252. error->args[argi++].value_ptr);
  253. if (len > 0) {
  254. msg += len;
  255. maxlen -= len;
  256. }
  257. break;
  258. case 's':
  259. len =
  260. SDL_snprintf(msg, maxlen, tmp,
  261. SDL_LookupString(error->args[argi++].
  262. buf));
  263. if (len > 0) {
  264. msg += len;
  265. maxlen -= len;
  266. }
  267. break;
  268. }
  269. } else {
  270. *msg++ = *fmt++;
  271. maxlen -= 1;
  272. }
  273. }
  274. /* slide back if we've overshot the end of our buffer. */
  275. if (maxlen < 0) {
  276. msg -= (-maxlen) + 1;
  277. }
  278. *msg = 0; /* NULL terminate the string */
  279. }
  280. return (errstr);
  281. }
  282. /* vi: set ts=4 sw=4 expandtab: */