SDL_test_log.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. /*
  19. Used by the test framework and test cases.
  20. */
  21. /* quiet windows compiler warnings */
  22. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  23. #define _CRT_SECURE_NO_WARNINGS
  24. #endif
  25. #include <SDL3/SDL_test.h>
  26. #include <time.h> /* Needed for localtime() */
  27. /* work around compiler warning on older GCCs. */
  28. #if (defined(__GNUC__) && (__GNUC__ <= 2))
  29. static size_t
  30. strftime_gcc2_workaround(char *s, size_t max, const char *fmt, const struct tm *tm)
  31. {
  32. return strftime(s, max, fmt, tm);
  33. }
  34. #ifdef strftime
  35. #undef strftime
  36. #endif
  37. #define strftime strftime_gcc2_workaround
  38. #endif
  39. /**
  40. * Converts unix timestamp to its ascii representation in localtime
  41. *
  42. * Note: Uses a static buffer internally, so the return value
  43. * isn't valid after the next call of this function. If you
  44. * want to retain the return value, make a copy of it.
  45. *
  46. * \param timestamp A Timestamp, i.e. time(0)
  47. *
  48. * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02'
  49. */
  50. static const char *
  51. SDLTest_TimestampToString(const time_t timestamp)
  52. {
  53. time_t copy;
  54. static char buffer[64];
  55. struct tm *local;
  56. size_t result = 0;
  57. SDL_memset(buffer, 0, sizeof(buffer));
  58. copy = timestamp;
  59. local = localtime(&copy);
  60. result = strftime(buffer, sizeof buffer, "%x %X", local);
  61. if (result == 0) {
  62. return "";
  63. }
  64. return buffer;
  65. }
  66. /*
  67. * Prints given message with a timestamp in the TEST category and INFO priority.
  68. */
  69. void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  70. {
  71. va_list list;
  72. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  73. /* Print log message into a buffer */
  74. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  75. va_start(list, fmt);
  76. (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  77. va_end(list);
  78. /* Log with timestamp and newline */
  79. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, " %s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  80. }
  81. /*
  82. * Prints given message with a timestamp in the TEST category and the ERROR priority.
  83. */
  84. void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  85. {
  86. va_list list;
  87. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  88. /* Print log message into a buffer */
  89. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  90. va_start(list, fmt);
  91. (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  92. va_end(list);
  93. /* Log with timestamp and newline */
  94. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  95. }