SDL_error.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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. // Ignore call if invalid format pointer was passed
  24. if (fmt) {
  25. va_list ap;
  26. int result;
  27. SDL_error *error = SDL_GetErrBuf(true);
  28. error->error = SDL_ErrorCodeGeneric;
  29. va_start(ap, fmt);
  30. result = SDL_vsnprintf(error->str, error->len, fmt, ap);
  31. va_end(ap);
  32. if (result >= 0 && (size_t)result >= error->len && error->realloc_func) {
  33. size_t len = (size_t)result + 1;
  34. char *str = (char *)error->realloc_func(error->str, len);
  35. if (str) {
  36. error->str = str;
  37. error->len = len;
  38. va_start(ap, fmt);
  39. (void)SDL_vsnprintf(error->str, error->len, fmt, ap);
  40. va_end(ap);
  41. }
  42. }
  43. if (SDL_GetLogPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
  44. // If we are in debug mode, print out the error message
  45. SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", error->str);
  46. }
  47. }
  48. return false;
  49. }
  50. const char *SDL_GetError(void)
  51. {
  52. const SDL_error *error = SDL_GetErrBuf(false);
  53. if (!error) {
  54. return "";
  55. }
  56. switch (error->error) {
  57. case SDL_ErrorCodeGeneric:
  58. return error->str;
  59. case SDL_ErrorCodeOutOfMemory:
  60. return "Out of memory";
  61. default:
  62. return "";
  63. }
  64. }
  65. bool SDL_ClearError(void)
  66. {
  67. SDL_error *error = SDL_GetErrBuf(false);
  68. if (error) {
  69. error->error = SDL_ErrorCodeNone;
  70. }
  71. return true;
  72. }
  73. bool SDL_OutOfMemory(void)
  74. {
  75. SDL_error *error = SDL_GetErrBuf(true);
  76. if (error) {
  77. error->error = SDL_ErrorCodeOutOfMemory;
  78. }
  79. return false;
  80. }