Atlas - SDL_test_log.c

Home / ext / SDL / src / test Lines: 2 | Size: 7029 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22/* 23 Used by the test framework and test cases. 24*/ 25 26/* quiet windows compiler warnings */ 27#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) 28#define _CRT_SECURE_NO_WARNINGS 29#endif 30#include <SDL3/SDL_test.h> 31 32#include <time.h> /* Needed for localtime() */ 33 34/* work around compiler warning on older GCCs. */ 35#if (defined(__GNUC__) && (__GNUC__ <= 2)) 36static size_t strftime_gcc2_workaround(char *s, size_t max, const char *fmt, const struct tm *tm) 37{ 38 return strftime(s, max, fmt, tm); 39} 40#ifdef strftime 41#undef strftime 42#endif 43#define strftime strftime_gcc2_workaround 44#endif 45 46/** 47 * Converts unix timestamp to its ascii representation in localtime 48 * 49 * Note: Uses a static buffer internally, so the return value 50 * isn't valid after the next call of this function. If you 51 * want to retain the return value, make a copy of it. 52 * 53 * \param timestamp A Timestamp, i.e. time(0) 54 * 55 * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02' 56 */ 57static const char *SDLTest_TimestampToString(const time_t timestamp) 58{ 59 time_t copy; 60 static char buffer[64]; 61 struct tm *local; 62 size_t result = 0; 63 64 SDL_memset(buffer, 0, sizeof(buffer)); 65 copy = timestamp; 66 local = localtime(&copy); 67 result = strftime(buffer, sizeof(buffer), "%x %X", local); 68 if (result == 0) { 69 return ""; 70 } 71 72 return buffer; 73} 74 75/* 76 * Prints given message with a timestamp in the TEST category and given priority. 77 */ 78static void SDLTest_LogMessageV(SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) 79{ 80 char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; 81 82 /* Print log message into a buffer */ 83 SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); 84 (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, ap); 85 86 /* Log with timestamp and newline. Messages with lower priority are slightly indented. */ 87 if (priority > SDL_LOG_PRIORITY_INFO) { 88 SDL_LogMessage(SDL_LOG_CATEGORY_TEST, priority, "%s: %s", SDLTest_TimestampToString(time(NULL)), logMessage); 89 } else { 90 SDL_LogMessage(SDL_LOG_CATEGORY_TEST, priority, " %s: %s", SDLTest_TimestampToString(time(NULL)), logMessage); 91 } 92} 93 94/* 95 * Prints given message with a timestamp in the TEST category and given priority. 96 */ 97void SDLTest_LogMessage(SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) 98{ 99 va_list ap; 100 101 va_start(ap, fmt); 102 SDLTest_LogMessageV(priority, fmt, ap); 103 va_end(ap); 104} 105 106/* 107 * Prints given message with a timestamp in the TEST category and INFO priority. 108 */ 109void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) 110{ 111 va_list ap; 112 113 va_start(ap, fmt); 114 SDLTest_LogMessageV(SDL_LOG_PRIORITY_INFO, fmt, ap); 115 va_end(ap); 116} 117 118/* 119 * Prints given message with a timestamp in the TEST category and the ERROR priority. 120 */ 121void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) 122{ 123 va_list ap; 124 125 va_start(ap, fmt); 126 SDLTest_LogMessageV(SDL_LOG_PRIORITY_ERROR, fmt, ap); 127 va_end(ap); 128} 129 130static char nibble_to_char(Uint8 nibble) 131{ 132 if (nibble < 0xa) { 133 return '0' + nibble; 134 } else { 135 return 'a' + nibble - 10; 136 } 137} 138 139void SDLTest_LogEscapedString(const char *prefix, const void *buffer, size_t size) 140{ 141 const Uint8 *data = buffer; 142 char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; 143 144 if (data) { 145 size_t i; 146 size_t pos = 0; 147 #define NEED_X_CHARS(N) \ 148 if (pos + (N) > sizeof(logMessage) - 2) { \ 149 break; \ 150 } 151 152 logMessage[pos++] = '"'; 153 for (i = 0; i < size; i++) { 154 Uint8 c = data[i]; 155 size_t pos_start = pos; 156 switch (c) { 157 case '\0': 158 NEED_X_CHARS(2); 159 logMessage[pos++] = '\\'; 160 logMessage[pos++] = '0'; 161 break; 162 case '"': 163 NEED_X_CHARS(2); 164 logMessage[pos++] = '\\'; 165 logMessage[pos++] = '"'; 166 break; 167 case '\n': 168 NEED_X_CHARS(2); 169 logMessage[pos++] = '\\'; 170 logMessage[pos++] = 'n'; 171 break; 172 case '\r': 173 NEED_X_CHARS(2); 174 logMessage[pos++] = '\\'; 175 logMessage[pos++] = 'r'; 176 break; 177 case '\t': 178 NEED_X_CHARS(2); 179 logMessage[pos++] = '\\'; 180 logMessage[pos++] = 't'; 181 break; 182 case '\f': 183 NEED_X_CHARS(2); 184 logMessage[pos++] = '\\'; 185 logMessage[pos++] = 'f'; 186 break; 187 case '\b': 188 NEED_X_CHARS(2); 189 logMessage[pos++] = '\\'; 190 logMessage[pos++] = 'b'; 191 break; 192 case '\\': 193 NEED_X_CHARS(2); 194 logMessage[pos++] = '\\'; 195 logMessage[pos++] = '\\'; 196 break; 197 default: 198 if (SDL_isprint(c)) { 199 NEED_X_CHARS(1); 200 logMessage[pos++] = c; 201 } else { 202 NEED_X_CHARS(4); 203 logMessage[pos++] = '\\'; 204 logMessage[pos++] = 'x'; 205 logMessage[pos++] = nibble_to_char(c >> 4); 206 logMessage[pos++] = nibble_to_char(c & 0xf); 207 } 208 break; 209 } 210 if (pos == pos_start) { 211 break; 212 } 213 } 214 if (i < size) { 215 logMessage[sizeof(logMessage) - 4] = '.'; 216 logMessage[sizeof(logMessage) - 3] = '.'; 217 logMessage[sizeof(logMessage) - 2] = '.'; 218 logMessage[sizeof(logMessage) - 1] = '\0'; 219 } else { 220 logMessage[pos++] = '"'; 221 logMessage[pos] = '\0'; 222 } 223 } else { 224 SDL_strlcpy(logMessage, "(nil)", sizeof(logMessage)); 225 } 226 227 SDLTest_Log("%s%s", prefix, logMessage); 228} 229
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.