SDL_error.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_error_c.h"
  21. bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  22. {
  23. va_list ap;
  24. bool result;
  25. va_start(ap, fmt);
  26. result = SDL_SetErrorV(fmt, ap);
  27. va_end(ap);
  28. return result;
  29. }
  30. bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  31. {
  32. // Ignore call if invalid format pointer was passed
  33. if (fmt) {
  34. int result;
  35. SDL_error *error = SDL_GetErrBuf(true);
  36. va_list ap2;
  37. error->error = SDL_ErrorCodeGeneric;
  38. va_copy(ap2, ap);
  39. result = SDL_vsnprintf(error->str, error->len, fmt, ap2);
  40. va_end(ap2);
  41. if (result >= 0 && (size_t)result >= error->len && error->realloc_func) {
  42. size_t len = (size_t)result + 1;
  43. char *str = (char *)error->realloc_func(error->str, len);
  44. if (str) {
  45. error->str = str;
  46. error->len = len;
  47. va_copy(ap2, ap);
  48. (void)SDL_vsnprintf(error->str, error->len, fmt, ap2);
  49. va_end(ap2);
  50. }
  51. }
  52. // Enable this if you want to see all errors printed as they occur.
  53. // Note that there are many recoverable errors that may happen internally and
  54. // can be safely ignored if the public API doesn't return an error code.
  55. #if 0
  56. SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", error->str);
  57. #endif
  58. }
  59. return false;
  60. }
  61. const char *SDL_GetError(void)
  62. {
  63. const SDL_error *error = SDL_GetErrBuf(false);
  64. if (!error) {
  65. return "";
  66. }
  67. switch (error->error) {
  68. case SDL_ErrorCodeGeneric:
  69. return error->str;
  70. case SDL_ErrorCodeOutOfMemory:
  71. return "Out of memory";
  72. default:
  73. return "";
  74. }
  75. }
  76. bool SDL_ClearError(void)
  77. {
  78. SDL_error *error = SDL_GetErrBuf(false);
  79. if (error) {
  80. error->error = SDL_ErrorCodeNone;
  81. }
  82. return true;
  83. }
  84. bool SDL_OutOfMemory(void)
  85. {
  86. SDL_error *error = SDL_GetErrBuf(true);
  87. if (error) {
  88. error->error = SDL_ErrorCodeOutOfMemory;
  89. }
  90. return false;
  91. }