SDL_test_log.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /*
  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 strftime_gcc2_workaround(char *s, size_t max, const char *fmt, const struct tm *tm)
  30. {
  31. return strftime(s, max, fmt, tm);
  32. }
  33. #ifdef strftime
  34. #undef strftime
  35. #endif
  36. #define strftime strftime_gcc2_workaround
  37. #endif
  38. /**
  39. * Converts unix timestamp to its ascii representation in localtime
  40. *
  41. * Note: Uses a static buffer internally, so the return value
  42. * isn't valid after the next call of this function. If you
  43. * want to retain the return value, make a copy of it.
  44. *
  45. * \param timestamp A Timestamp, i.e. time(0)
  46. *
  47. * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02'
  48. */
  49. static const char *SDLTest_TimestampToString(const time_t timestamp)
  50. {
  51. time_t copy;
  52. static char buffer[64];
  53. struct tm *local;
  54. size_t result = 0;
  55. SDL_memset(buffer, 0, sizeof(buffer));
  56. copy = timestamp;
  57. local = localtime(&copy);
  58. result = strftime(buffer, sizeof(buffer), "%x %X", local);
  59. if (result == 0) {
  60. return "";
  61. }
  62. return buffer;
  63. }
  64. /*
  65. * Prints given message with a timestamp in the TEST category and INFO priority.
  66. */
  67. void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  68. {
  69. va_list list;
  70. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  71. /* Print log message into a buffer */
  72. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  73. va_start(list, fmt);
  74. (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  75. va_end(list);
  76. /* Log with timestamp and newline */
  77. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, " %s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  78. }
  79. /*
  80. * Prints given message with a timestamp in the TEST category and the ERROR priority.
  81. */
  82. void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  83. {
  84. va_list list;
  85. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  86. /* Print log message into a buffer */
  87. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  88. va_start(list, fmt);
  89. (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  90. va_end(list);
  91. /* Log with timestamp and newline */
  92. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  93. }
  94. static char nibble_to_char(Uint8 nibble)
  95. {
  96. if (nibble < 0xa) {
  97. return '0' + nibble;
  98. } else {
  99. return 'a' + nibble - 10;
  100. }
  101. }
  102. void SDLTest_LogEscapedString(const char *prefix, const void *buffer, size_t size)
  103. {
  104. const Uint8 *data = buffer;
  105. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  106. if (data) {
  107. size_t i;
  108. size_t pos = 0;
  109. #define NEED_X_CHARS(N) \
  110. if (pos + (N) > sizeof(logMessage) - 2) { \
  111. break; \
  112. }
  113. logMessage[pos++] = '"';
  114. for (i = 0; i < size; i++) {
  115. Uint8 c = data[i];
  116. size_t pos_start = pos;
  117. switch (c) {
  118. case '\0':
  119. NEED_X_CHARS(2);
  120. logMessage[pos++] = '\\';
  121. logMessage[pos++] = '0';
  122. break;
  123. case '"':
  124. NEED_X_CHARS(2);
  125. logMessage[pos++] = '\\';
  126. logMessage[pos++] = '"';
  127. break;
  128. case '\n':
  129. NEED_X_CHARS(2);
  130. logMessage[pos++] = '\\';
  131. logMessage[pos++] = 'n';
  132. break;
  133. case '\r':
  134. NEED_X_CHARS(2);
  135. logMessage[pos++] = '\\';
  136. logMessage[pos++] = 'r';
  137. break;
  138. case '\t':
  139. NEED_X_CHARS(2);
  140. logMessage[pos++] = '\\';
  141. logMessage[pos++] = 't';
  142. break;
  143. case '\f':
  144. NEED_X_CHARS(2);
  145. logMessage[pos++] = '\\';
  146. logMessage[pos++] = 'f';
  147. break;
  148. case '\b':
  149. NEED_X_CHARS(2);
  150. logMessage[pos++] = '\\';
  151. logMessage[pos++] = 'b';
  152. break;
  153. case '\\':
  154. NEED_X_CHARS(2);
  155. logMessage[pos++] = '\\';
  156. logMessage[pos++] = '\\';
  157. break;
  158. default:
  159. if (SDL_isprint(c)) {
  160. NEED_X_CHARS(1);
  161. logMessage[pos++] = c;
  162. } else {
  163. NEED_X_CHARS(4);
  164. logMessage[pos++] = '\\';
  165. logMessage[pos++] = 'x';
  166. logMessage[pos++] = nibble_to_char(c >> 4);
  167. logMessage[pos++] = nibble_to_char(c & 0xf);
  168. }
  169. break;
  170. }
  171. if (pos == pos_start) {
  172. break;
  173. }
  174. }
  175. if (i < size) {
  176. logMessage[sizeof(logMessage) - 4] = '.';
  177. logMessage[sizeof(logMessage) - 3] = '.';
  178. logMessage[sizeof(logMessage) - 2] = '.';
  179. logMessage[sizeof(logMessage) - 1] = '\0';
  180. } else {
  181. logMessage[pos++] = '"';
  182. logMessage[pos] = '\0';
  183. }
  184. } else {
  185. SDL_strlcpy(logMessage, "(nil)", sizeof(logMessage));
  186. }
  187. SDLTest_Log("%s%s", prefix, logMessage);
  188. }