SDL_log.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_GDK)
  20. #include "core/windows/SDL_windows.h"
  21. #endif
  22. /* Simple log messages in SDL */
  23. #include "SDL_log_c.h"
  24. #ifdef HAVE_STDIO_H
  25. #include <stdio.h>
  26. #endif
  27. #ifdef SDL_PLATFORM_ANDROID
  28. #include <android/log.h>
  29. #endif
  30. #include "stdlib/SDL_vacopy.h"
  31. /* The size of the stack buffer to use for rendering log messages. */
  32. #define SDL_MAX_LOG_MESSAGE_STACK 256
  33. typedef struct SDL_LogLevel
  34. {
  35. int category;
  36. SDL_LogPriority priority;
  37. struct SDL_LogLevel *next;
  38. } SDL_LogLevel;
  39. /* The default log output function */
  40. static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message);
  41. static SDL_LogLevel *SDL_loglevels;
  42. static SDL_bool SDL_forced_priority = SDL_FALSE;
  43. static SDL_LogPriority SDL_forced_priority_level;
  44. static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
  45. static void *SDL_log_userdata = NULL;
  46. static SDL_Mutex *log_function_mutex = NULL;
  47. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  48. #pragma GCC diagnostic push
  49. #pragma GCC diagnostic ignored "-Wunused-variable"
  50. #endif
  51. static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
  52. NULL,
  53. "VERBOSE",
  54. "DEBUG",
  55. "INFO",
  56. "WARN",
  57. "ERROR",
  58. "CRITICAL"
  59. };
  60. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  61. #pragma GCC diagnostic pop
  62. #endif
  63. #ifdef SDL_PLATFORM_ANDROID
  64. static const char *SDL_category_prefixes[] = {
  65. "APP",
  66. "ERROR",
  67. "ASSERT",
  68. "SYSTEM",
  69. "AUDIO",
  70. "VIDEO",
  71. "RENDER",
  72. "INPUT",
  73. "TEST"
  74. };
  75. SDL_COMPILE_TIME_ASSERT(category_prefixes_enum, SDL_TABLESIZE(SDL_category_prefixes) == SDL_LOG_CATEGORY_RESERVED1);
  76. static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
  77. ANDROID_LOG_UNKNOWN,
  78. ANDROID_LOG_VERBOSE,
  79. ANDROID_LOG_DEBUG,
  80. ANDROID_LOG_INFO,
  81. ANDROID_LOG_WARN,
  82. ANDROID_LOG_ERROR,
  83. ANDROID_LOG_FATAL
  84. };
  85. #endif /* SDL_PLATFORM_ANDROID */
  86. void SDL_InitLog(void)
  87. {
  88. if (!log_function_mutex) {
  89. /* if this fails we'll try to continue without it. */
  90. log_function_mutex = SDL_CreateMutex();
  91. }
  92. }
  93. void SDL_QuitLog(void)
  94. {
  95. SDL_LogResetPriorities();
  96. if (log_function_mutex) {
  97. SDL_DestroyMutex(log_function_mutex);
  98. log_function_mutex = NULL;
  99. }
  100. }
  101. void SDL_LogSetAllPriority(SDL_LogPriority priority)
  102. {
  103. SDL_LogLevel *entry;
  104. for (entry = SDL_loglevels; entry; entry = entry->next) {
  105. entry->priority = priority;
  106. }
  107. SDL_forced_priority = SDL_TRUE;
  108. SDL_forced_priority_level = priority;
  109. }
  110. void SDL_LogSetPriority(int category, SDL_LogPriority priority)
  111. {
  112. SDL_LogLevel *entry;
  113. for (entry = SDL_loglevels; entry; entry = entry->next) {
  114. if (entry->category == category) {
  115. entry->priority = priority;
  116. return;
  117. }
  118. }
  119. /* Create a new entry */
  120. entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
  121. if (entry) {
  122. entry->category = category;
  123. entry->priority = priority;
  124. entry->next = SDL_loglevels;
  125. SDL_loglevels = entry;
  126. }
  127. }
  128. SDL_LogPriority SDL_LogGetPriority(int category)
  129. {
  130. SDL_LogLevel *entry;
  131. for (entry = SDL_loglevels; entry; entry = entry->next) {
  132. if (entry->category == category) {
  133. return entry->priority;
  134. }
  135. }
  136. if (SDL_forced_priority) {
  137. return SDL_forced_priority_level;
  138. }
  139. switch (category) {
  140. case SDL_LOG_CATEGORY_APPLICATION:
  141. return SDL_LOG_PRIORITY_INFO;
  142. case SDL_LOG_CATEGORY_ASSERT:
  143. return SDL_LOG_PRIORITY_WARN;
  144. case SDL_LOG_CATEGORY_TEST:
  145. return SDL_LOG_PRIORITY_VERBOSE;
  146. default:
  147. return SDL_LOG_PRIORITY_ERROR;
  148. }
  149. }
  150. void SDL_LogResetPriorities(void)
  151. {
  152. SDL_LogLevel *entry;
  153. while (SDL_loglevels) {
  154. entry = SDL_loglevels;
  155. SDL_loglevels = entry->next;
  156. SDL_free(entry);
  157. }
  158. SDL_forced_priority = SDL_FALSE;
  159. }
  160. void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  161. {
  162. va_list ap;
  163. va_start(ap, fmt);
  164. SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  165. va_end(ap);
  166. }
  167. void SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  168. {
  169. va_list ap;
  170. va_start(ap, fmt);
  171. SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
  172. va_end(ap);
  173. }
  174. void SDL_LogDebug(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  175. {
  176. va_list ap;
  177. va_start(ap, fmt);
  178. SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
  179. va_end(ap);
  180. }
  181. void SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  182. {
  183. va_list ap;
  184. va_start(ap, fmt);
  185. SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
  186. va_end(ap);
  187. }
  188. void SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  189. {
  190. va_list ap;
  191. va_start(ap, fmt);
  192. SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
  193. va_end(ap);
  194. }
  195. void SDL_LogError(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  196. {
  197. va_list ap;
  198. va_start(ap, fmt);
  199. SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
  200. va_end(ap);
  201. }
  202. void SDL_LogCritical(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  203. {
  204. va_list ap;
  205. va_start(ap, fmt);
  206. SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
  207. va_end(ap);
  208. }
  209. void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  210. {
  211. va_list ap;
  212. va_start(ap, fmt);
  213. SDL_LogMessageV(category, priority, fmt, ap);
  214. va_end(ap);
  215. }
  216. #ifdef SDL_PLATFORM_ANDROID
  217. static const char *GetCategoryPrefix(int category)
  218. {
  219. if (category < SDL_LOG_CATEGORY_RESERVED1) {
  220. return SDL_category_prefixes[category];
  221. }
  222. if (category < SDL_LOG_CATEGORY_CUSTOM) {
  223. return "RESERVED";
  224. }
  225. return "CUSTOM";
  226. }
  227. #endif /* SDL_PLATFORM_ANDROID */
  228. void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  229. {
  230. char *message = NULL;
  231. char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
  232. size_t len_plus_term;
  233. int len;
  234. va_list aq;
  235. /* Nothing to do if we don't have an output function */
  236. if (!SDL_log_function) {
  237. return;
  238. }
  239. /* Make sure we don't exceed array bounds */
  240. if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
  241. return;
  242. }
  243. /* See if we want to do anything with this message */
  244. if (priority < SDL_LogGetPriority(category)) {
  245. return;
  246. }
  247. if (!log_function_mutex) {
  248. /* this mutex creation can race if you log from two threads at startup. You should have called SDL_Init first! */
  249. log_function_mutex = SDL_CreateMutex();
  250. }
  251. /* Render into stack buffer */
  252. va_copy(aq, ap);
  253. len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
  254. va_end(aq);
  255. if (len < 0) {
  256. return;
  257. }
  258. /* If message truncated, allocate and re-render */
  259. if (len >= sizeof(stack_buf) && SDL_size_add_overflow(len, 1, &len_plus_term) == 0) {
  260. /* Allocate exactly what we need, including the zero-terminator */
  261. message = (char *)SDL_malloc(len_plus_term);
  262. if (!message) {
  263. return;
  264. }
  265. va_copy(aq, ap);
  266. len = SDL_vsnprintf(message, len_plus_term, fmt, aq);
  267. va_end(aq);
  268. } else {
  269. message = stack_buf;
  270. }
  271. /* Chop off final endline. */
  272. if ((len > 0) && (message[len - 1] == '\n')) {
  273. message[--len] = '\0';
  274. if ((len > 0) && (message[len - 1] == '\r')) { /* catch "\r\n", too. */
  275. message[--len] = '\0';
  276. }
  277. }
  278. SDL_LockMutex(log_function_mutex);
  279. SDL_log_function(SDL_log_userdata, category, priority, message);
  280. SDL_UnlockMutex(log_function_mutex);
  281. /* Free only if dynamically allocated */
  282. if (message != stack_buf) {
  283. SDL_free(message);
  284. }
  285. }
  286. #if defined(SDL_PLATFORM_WIN32) && !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  287. /* Flag tracking the attachment of the console: 0=unattached, 1=attached to a console, 2=attached to a file, -1=error */
  288. static int consoleAttached = 0;
  289. /* Handle to stderr output of console. */
  290. static HANDLE stderrHandle = NULL;
  291. #endif
  292. static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
  293. const char *message)
  294. {
  295. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_GDK)
  296. /* Way too many allocations here, urgh */
  297. /* Note: One can't call SDL_SetError here, since that function itself logs. */
  298. {
  299. char *output;
  300. size_t length;
  301. LPTSTR tstr;
  302. SDL_bool isstack;
  303. #if !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  304. BOOL attachResult;
  305. DWORD attachError;
  306. DWORD charsWritten;
  307. DWORD consoleMode;
  308. /* Maybe attach console and get stderr handle */
  309. if (consoleAttached == 0) {
  310. attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
  311. if (!attachResult) {
  312. attachError = GetLastError();
  313. if (attachError == ERROR_INVALID_HANDLE) {
  314. /* This is expected when running from Visual Studio */
  315. /*OutputDebugString(TEXT("Parent process has no console\r\n"));*/
  316. consoleAttached = -1;
  317. } else if (attachError == ERROR_GEN_FAILURE) {
  318. OutputDebugString(TEXT("Could not attach to console of parent process\r\n"));
  319. consoleAttached = -1;
  320. } else if (attachError == ERROR_ACCESS_DENIED) {
  321. /* Already attached */
  322. consoleAttached = 1;
  323. } else {
  324. OutputDebugString(TEXT("Error attaching console\r\n"));
  325. consoleAttached = -1;
  326. }
  327. } else {
  328. /* Newly attached */
  329. consoleAttached = 1;
  330. }
  331. if (consoleAttached == 1) {
  332. stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
  333. if (GetConsoleMode(stderrHandle, &consoleMode) == 0) {
  334. /* WriteConsole fails if the output is redirected to a file. Must use WriteFile instead. */
  335. consoleAttached = 2;
  336. }
  337. }
  338. }
  339. #endif /* !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK) */
  340. length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
  341. output = SDL_small_alloc(char, length, &isstack);
  342. (void)SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message);
  343. tstr = WIN_UTF8ToString(output);
  344. /* Output to debugger */
  345. OutputDebugString(tstr);
  346. #if !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  347. /* Screen output to stderr, if console was attached. */
  348. if (consoleAttached == 1) {
  349. if (!WriteConsole(stderrHandle, tstr, (DWORD)SDL_tcslen(tstr), &charsWritten, NULL)) {
  350. OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
  351. if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  352. OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
  353. }
  354. }
  355. } else if (consoleAttached == 2) {
  356. if (!WriteFile(stderrHandle, output, (DWORD)SDL_strlen(output), &charsWritten, NULL)) {
  357. OutputDebugString(TEXT("Error calling WriteFile\r\n"));
  358. }
  359. }
  360. #endif /* !defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK) */
  361. SDL_free(tstr);
  362. SDL_small_free(output, isstack);
  363. }
  364. #elif defined(SDL_PLATFORM_ANDROID)
  365. {
  366. char tag[32];
  367. SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
  368. __android_log_write(SDL_android_priority[priority], tag, message);
  369. }
  370. #elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
  371. /* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
  372. */
  373. extern void SDL_NSLog(const char *prefix, const char *text);
  374. {
  375. SDL_NSLog(SDL_priority_prefixes[priority], message);
  376. return;
  377. }
  378. #elif defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_PS2)
  379. {
  380. FILE *pFile;
  381. pFile = fopen("SDL_Log.txt", "a");
  382. if (pFile) {
  383. (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
  384. (void)fclose(pFile);
  385. }
  386. }
  387. #elif defined(SDL_PLATFORM_VITA)
  388. {
  389. FILE *pFile;
  390. pFile = fopen("ux0:/data/SDL_Log.txt", "a");
  391. if (pFile) {
  392. (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
  393. (void)fclose(pFile);
  394. }
  395. }
  396. #elif defined(SDL_PLATFORM_3DS)
  397. {
  398. FILE *pFile;
  399. pFile = fopen("sdmc:/3ds/SDL_Log.txt", "a");
  400. if (pFile) {
  401. (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
  402. (void)fclose(pFile);
  403. }
  404. }
  405. #endif
  406. #if defined(HAVE_STDIO_H) && \
  407. !(defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT)))
  408. (void)fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
  409. #endif
  410. }
  411. void SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
  412. {
  413. if (callback) {
  414. *callback = SDL_log_function;
  415. }
  416. if (userdata) {
  417. *userdata = SDL_log_userdata;
  418. }
  419. }
  420. void SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
  421. {
  422. SDL_log_function = callback;
  423. SDL_log_userdata = userdata;
  424. }