SDL_log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. #define DEFAULT_CATEGORY -1
  34. typedef struct SDL_LogLevel
  35. {
  36. int category;
  37. SDL_LogPriority priority;
  38. struct SDL_LogLevel *next;
  39. } SDL_LogLevel;
  40. // The default log output function
  41. static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message);
  42. static void SDL_ResetLogPrefixes(void);
  43. static SDL_LogLevel *SDL_loglevels;
  44. static bool SDL_forced_priority = false;
  45. static SDL_LogPriority SDL_forced_priority_level;
  46. static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
  47. static void *SDL_log_userdata = NULL;
  48. static SDL_Mutex *log_function_mutex = NULL;
  49. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  50. #pragma GCC diagnostic push
  51. #pragma GCC diagnostic ignored "-Wunused-variable"
  52. #endif
  53. // If this list changes, update the documentation for SDL_HINT_LOGGING
  54. static const char * const SDL_priority_names[] = {
  55. NULL,
  56. "VERBOSE",
  57. "DEBUG",
  58. "INFO",
  59. "WARN",
  60. "ERROR",
  61. "CRITICAL"
  62. };
  63. SDL_COMPILE_TIME_ASSERT(priority_names, SDL_arraysize(SDL_priority_names) == SDL_NUM_LOG_PRIORITIES);
  64. static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES];
  65. // If this list changes, update the documentation for SDL_HINT_LOGGING
  66. static const char * const SDL_category_names[] = {
  67. "APP",
  68. "ERROR",
  69. "ASSERT",
  70. "SYSTEM",
  71. "AUDIO",
  72. "VIDEO",
  73. "RENDER",
  74. "GPU",
  75. "INPUT",
  76. "TEST"
  77. };
  78. SDL_COMPILE_TIME_ASSERT(category_names, SDL_arraysize(SDL_category_names) == SDL_LOG_CATEGORY_RESERVED1);
  79. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  80. #pragma GCC diagnostic pop
  81. #endif
  82. #ifdef SDL_PLATFORM_ANDROID
  83. static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
  84. ANDROID_LOG_UNKNOWN,
  85. ANDROID_LOG_VERBOSE,
  86. ANDROID_LOG_DEBUG,
  87. ANDROID_LOG_INFO,
  88. ANDROID_LOG_WARN,
  89. ANDROID_LOG_ERROR,
  90. ANDROID_LOG_FATAL
  91. };
  92. #endif // SDL_PLATFORM_ANDROID
  93. void SDL_InitLog(void)
  94. {
  95. if (!log_function_mutex) {
  96. // if this fails we'll try to continue without it.
  97. log_function_mutex = SDL_CreateMutex();
  98. }
  99. }
  100. void SDL_QuitLog(void)
  101. {
  102. SDL_ResetLogPriorities();
  103. SDL_ResetLogPrefixes();
  104. if (log_function_mutex) {
  105. SDL_DestroyMutex(log_function_mutex);
  106. log_function_mutex = NULL;
  107. }
  108. }
  109. void SDL_SetLogPriorities(SDL_LogPriority priority)
  110. {
  111. SDL_LogLevel *entry;
  112. for (entry = SDL_loglevels; entry; entry = entry->next) {
  113. entry->priority = priority;
  114. }
  115. SDL_forced_priority = true;
  116. SDL_forced_priority_level = priority;
  117. }
  118. void SDL_SetLogPriority(int category, SDL_LogPriority priority)
  119. {
  120. SDL_LogLevel *entry;
  121. for (entry = SDL_loglevels; entry; entry = entry->next) {
  122. if (entry->category == category) {
  123. entry->priority = priority;
  124. return;
  125. }
  126. }
  127. // Create a new entry
  128. entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
  129. if (entry) {
  130. entry->category = category;
  131. entry->priority = priority;
  132. entry->next = SDL_loglevels;
  133. SDL_loglevels = entry;
  134. }
  135. }
  136. static bool SDL_ParseLogCategory(const char *string, size_t length, int *category)
  137. {
  138. int i;
  139. if (SDL_isdigit(*string)) {
  140. *category = SDL_atoi(string);
  141. return true;
  142. }
  143. if (*string == '*') {
  144. *category = DEFAULT_CATEGORY;
  145. return true;
  146. }
  147. for (i = 0; i < SDL_arraysize(SDL_category_names); ++i) {
  148. if (SDL_strncasecmp(string, SDL_category_names[i], length) == 0) {
  149. *category = i;
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. static bool SDL_ParseLogPriority(const char *string, size_t length, SDL_LogPriority *priority)
  156. {
  157. int i;
  158. if (SDL_isdigit(*string)) {
  159. i = SDL_atoi(string);
  160. if (i == 0) {
  161. // 0 has a special meaning of "disable this category"
  162. *priority = SDL_NUM_LOG_PRIORITIES;
  163. return true;
  164. }
  165. if (i >= SDL_LOG_PRIORITY_VERBOSE && i < SDL_NUM_LOG_PRIORITIES) {
  166. *priority = (SDL_LogPriority)i;
  167. return true;
  168. }
  169. return false;
  170. }
  171. if (SDL_strncasecmp(string, "quiet", length) == 0) {
  172. *priority = SDL_NUM_LOG_PRIORITIES;
  173. return true;
  174. }
  175. for (i = SDL_LOG_PRIORITY_VERBOSE; i < SDL_NUM_LOG_PRIORITIES; ++i) {
  176. if (SDL_strncasecmp(string, SDL_priority_names[i], length) == 0) {
  177. *priority = (SDL_LogPriority)i;
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. static bool SDL_ParseLogCategoryPriority(const char *hint, int category, SDL_LogPriority *priority)
  184. {
  185. const char *name, *next;
  186. int current_category;
  187. if (category == DEFAULT_CATEGORY && SDL_strchr(hint, '=') == NULL) {
  188. return SDL_ParseLogPriority(hint, SDL_strlen(hint), priority);
  189. }
  190. for (name = hint; name; name = next) {
  191. const char *sep = SDL_strchr(name, '=');
  192. if (!sep) {
  193. break;
  194. }
  195. next = SDL_strchr(sep, ',');
  196. if (next) {
  197. ++next;
  198. }
  199. if (SDL_ParseLogCategory(name, (sep - name), &current_category)) {
  200. if (current_category == category) {
  201. const char *value = sep + 1;
  202. size_t len;
  203. if (next) {
  204. len = (next - value - 1);
  205. } else {
  206. len = SDL_strlen(value);
  207. }
  208. return SDL_ParseLogPriority(value, len, priority);
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. static SDL_LogPriority SDL_GetDefaultLogPriority(int category)
  215. {
  216. const char *hint = SDL_GetHint(SDL_HINT_LOGGING);
  217. if (hint) {
  218. SDL_LogPriority priority;
  219. if (SDL_ParseLogCategoryPriority(hint, category, &priority)) {
  220. return priority;
  221. }
  222. if (SDL_ParseLogCategoryPriority(hint, DEFAULT_CATEGORY, &priority)) {
  223. return priority;
  224. }
  225. }
  226. switch (category) {
  227. case SDL_LOG_CATEGORY_APPLICATION:
  228. return SDL_LOG_PRIORITY_INFO;
  229. case SDL_LOG_CATEGORY_ASSERT:
  230. return SDL_LOG_PRIORITY_WARN;
  231. case SDL_LOG_CATEGORY_TEST:
  232. return SDL_LOG_PRIORITY_VERBOSE;
  233. default:
  234. return SDL_LOG_PRIORITY_ERROR;
  235. }
  236. }
  237. SDL_LogPriority SDL_GetLogPriority(int category)
  238. {
  239. SDL_LogLevel *entry;
  240. for (entry = SDL_loglevels; entry; entry = entry->next) {
  241. if (entry->category == category) {
  242. return entry->priority;
  243. }
  244. }
  245. if (SDL_forced_priority) {
  246. return SDL_forced_priority_level;
  247. }
  248. return SDL_GetDefaultLogPriority(category);
  249. }
  250. void SDL_ResetLogPriorities(void)
  251. {
  252. SDL_LogLevel *entry;
  253. while (SDL_loglevels) {
  254. entry = SDL_loglevels;
  255. SDL_loglevels = entry->next;
  256. SDL_free(entry);
  257. }
  258. SDL_forced_priority = false;
  259. }
  260. static void SDL_ResetLogPrefixes(void)
  261. {
  262. for (int i = 0; i < SDL_arraysize(SDL_priority_prefixes); ++i) {
  263. SDL_priority_prefixes[i] = NULL;
  264. }
  265. }
  266. static const char *SDL_GetLogPriorityPrefix(SDL_LogPriority priority)
  267. {
  268. if (priority < SDL_LOG_PRIORITY_VERBOSE || priority >= SDL_NUM_LOG_PRIORITIES) {
  269. return "";
  270. }
  271. if (SDL_priority_prefixes[priority]) {
  272. return SDL_priority_prefixes[priority];
  273. }
  274. switch (priority) {
  275. case SDL_LOG_PRIORITY_WARN:
  276. return "WARNING: ";
  277. case SDL_LOG_PRIORITY_ERROR:
  278. return "ERROR: ";
  279. case SDL_LOG_PRIORITY_CRITICAL:
  280. return "ERROR: ";
  281. default:
  282. return "";
  283. }
  284. }
  285. SDL_bool SDL_SetLogPriorityPrefix(SDL_LogPriority priority, const char *prefix)
  286. {
  287. if (priority < SDL_LOG_PRIORITY_VERBOSE || priority >= SDL_NUM_LOG_PRIORITIES) {
  288. return SDL_InvalidParamError("priority");
  289. }
  290. if (!prefix) {
  291. prefix = "";
  292. } else {
  293. prefix = SDL_GetPersistentString(prefix);
  294. if (!prefix) {
  295. return false;
  296. }
  297. }
  298. SDL_priority_prefixes[priority] = prefix;
  299. return true;
  300. }
  301. void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  302. {
  303. va_list ap;
  304. va_start(ap, fmt);
  305. SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  306. va_end(ap);
  307. }
  308. void SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  309. {
  310. va_list ap;
  311. va_start(ap, fmt);
  312. SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
  313. va_end(ap);
  314. }
  315. void SDL_LogDebug(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  316. {
  317. va_list ap;
  318. va_start(ap, fmt);
  319. SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
  320. va_end(ap);
  321. }
  322. void SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  323. {
  324. va_list ap;
  325. va_start(ap, fmt);
  326. SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
  327. va_end(ap);
  328. }
  329. void SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  330. {
  331. va_list ap;
  332. va_start(ap, fmt);
  333. SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
  334. va_end(ap);
  335. }
  336. void SDL_LogError(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  337. {
  338. va_list ap;
  339. va_start(ap, fmt);
  340. SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
  341. va_end(ap);
  342. }
  343. void SDL_LogCritical(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  344. {
  345. va_list ap;
  346. va_start(ap, fmt);
  347. SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
  348. va_end(ap);
  349. }
  350. void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  351. {
  352. va_list ap;
  353. va_start(ap, fmt);
  354. SDL_LogMessageV(category, priority, fmt, ap);
  355. va_end(ap);
  356. }
  357. #ifdef SDL_PLATFORM_ANDROID
  358. static const char *GetCategoryPrefix(int category)
  359. {
  360. if (category < SDL_LOG_CATEGORY_RESERVED1) {
  361. return SDL_category_names[category];
  362. }
  363. if (category < SDL_LOG_CATEGORY_CUSTOM) {
  364. return "RESERVED";
  365. }
  366. return "CUSTOM";
  367. }
  368. #endif // SDL_PLATFORM_ANDROID
  369. void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  370. {
  371. char *message = NULL;
  372. char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
  373. size_t len_plus_term;
  374. int len;
  375. va_list aq;
  376. // Nothing to do if we don't have an output function
  377. if (!SDL_log_function) {
  378. return;
  379. }
  380. // Make sure we don't exceed array bounds
  381. if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
  382. return;
  383. }
  384. // See if we want to do anything with this message
  385. if (priority < SDL_GetLogPriority(category)) {
  386. return;
  387. }
  388. if (!log_function_mutex) {
  389. // this mutex creation can race if you log from two threads at startup. You should have called SDL_Init first!
  390. log_function_mutex = SDL_CreateMutex();
  391. }
  392. // Render into stack buffer
  393. va_copy(aq, ap);
  394. len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
  395. va_end(aq);
  396. if (len < 0) {
  397. return;
  398. }
  399. // If message truncated, allocate and re-render
  400. if (len >= sizeof(stack_buf) && SDL_size_add_check_overflow(len, 1, &len_plus_term)) {
  401. // Allocate exactly what we need, including the zero-terminator
  402. message = (char *)SDL_malloc(len_plus_term);
  403. if (!message) {
  404. return;
  405. }
  406. va_copy(aq, ap);
  407. len = SDL_vsnprintf(message, len_plus_term, fmt, aq);
  408. va_end(aq);
  409. } else {
  410. message = stack_buf;
  411. }
  412. // Chop off final endline.
  413. if ((len > 0) && (message[len - 1] == '\n')) {
  414. message[--len] = '\0';
  415. if ((len > 0) && (message[len - 1] == '\r')) { // catch "\r\n", too.
  416. message[--len] = '\0';
  417. }
  418. }
  419. SDL_LockMutex(log_function_mutex);
  420. SDL_log_function(SDL_log_userdata, category, priority, message);
  421. SDL_UnlockMutex(log_function_mutex);
  422. // Free only if dynamically allocated
  423. if (message != stack_buf) {
  424. SDL_free(message);
  425. }
  426. }
  427. #if defined(SDL_PLATFORM_WIN32) && !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  428. enum {
  429. CONSOLE_UNATTACHED = 0,
  430. CONSOLE_ATTACHED_CONSOLE = 1,
  431. CONSOLE_ATTACHED_FILE = 2,
  432. CONSOLE_ATTACHED_ERROR = -1,
  433. } consoleAttached = CONSOLE_UNATTACHED;
  434. // Handle to stderr output of console.
  435. static HANDLE stderrHandle = NULL;
  436. #endif
  437. static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
  438. const char *message)
  439. {
  440. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_GDK)
  441. // Way too many allocations here, urgh
  442. // Note: One can't call SDL_SetError here, since that function itself logs.
  443. {
  444. char *output;
  445. size_t length;
  446. LPTSTR tstr;
  447. bool isstack;
  448. #if !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  449. BOOL attachResult;
  450. DWORD attachError;
  451. DWORD consoleMode;
  452. DWORD charsWritten;
  453. // Maybe attach console and get stderr handle
  454. if (consoleAttached == CONSOLE_UNATTACHED) {
  455. attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
  456. if (!attachResult) {
  457. attachError = GetLastError();
  458. if (attachError == ERROR_INVALID_HANDLE) {
  459. // This is expected when running from Visual Studio
  460. // OutputDebugString(TEXT("Parent process has no console\r\n"));
  461. consoleAttached = CONSOLE_ATTACHED_ERROR;
  462. } else if (attachError == ERROR_GEN_FAILURE) {
  463. OutputDebugString(TEXT("Could not attach to console of parent process\r\n"));
  464. consoleAttached = CONSOLE_ATTACHED_ERROR;
  465. } else if (attachError == ERROR_ACCESS_DENIED) {
  466. // Already attached
  467. consoleAttached = CONSOLE_ATTACHED_CONSOLE;
  468. } else {
  469. OutputDebugString(TEXT("Error attaching console\r\n"));
  470. consoleAttached = CONSOLE_ATTACHED_ERROR;
  471. }
  472. } else {
  473. // Newly attached
  474. consoleAttached = CONSOLE_ATTACHED_CONSOLE;
  475. }
  476. if (consoleAttached == CONSOLE_ATTACHED_CONSOLE) {
  477. stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
  478. if (GetConsoleMode(stderrHandle, &consoleMode) == 0) {
  479. // WriteConsole fails if the output is redirected to a file. Must use WriteFile instead.
  480. consoleAttached = CONSOLE_ATTACHED_FILE;
  481. }
  482. }
  483. }
  484. #endif // !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  485. length = SDL_strlen(SDL_GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
  486. output = SDL_small_alloc(char, length, &isstack);
  487. (void)SDL_snprintf(output, length, "%s%s\r\n", SDL_GetLogPriorityPrefix(priority), message);
  488. tstr = WIN_UTF8ToString(output);
  489. // Output to debugger
  490. OutputDebugString(tstr);
  491. #if !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  492. // Screen output to stderr, if console was attached.
  493. if (consoleAttached == CONSOLE_ATTACHED_CONSOLE) {
  494. if (!WriteConsole(stderrHandle, tstr, (DWORD)SDL_tcslen(tstr), &charsWritten, NULL)) {
  495. OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
  496. if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  497. OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
  498. }
  499. }
  500. } else if (consoleAttached == CONSOLE_ATTACHED_FILE) {
  501. if (!WriteFile(stderrHandle, output, (DWORD)SDL_strlen(output), &charsWritten, NULL)) {
  502. OutputDebugString(TEXT("Error calling WriteFile\r\n"));
  503. }
  504. }
  505. #endif // !defined(SDL_PLATFORM_WINRT) && !defined(SDL_PLATFORM_GDK)
  506. SDL_free(tstr);
  507. SDL_small_free(output, isstack);
  508. }
  509. #elif defined(SDL_PLATFORM_ANDROID)
  510. {
  511. char tag[32];
  512. SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
  513. __android_log_write(SDL_android_priority[priority], tag, message);
  514. }
  515. #elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
  516. /* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
  517. */
  518. extern void SDL_NSLog(const char *prefix, const char *text);
  519. {
  520. SDL_NSLog(SDL_GetLogPriorityPrefix(priority), message);
  521. return;
  522. }
  523. #elif defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_PS2)
  524. {
  525. FILE *pFile;
  526. pFile = fopen("SDL_Log.txt", "a");
  527. if (pFile) {
  528. (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
  529. (void)fclose(pFile);
  530. }
  531. }
  532. #elif defined(SDL_PLATFORM_VITA)
  533. {
  534. FILE *pFile;
  535. pFile = fopen("ux0:/data/SDL_Log.txt", "a");
  536. if (pFile) {
  537. (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
  538. (void)fclose(pFile);
  539. }
  540. }
  541. #elif defined(SDL_PLATFORM_3DS)
  542. {
  543. FILE *pFile;
  544. pFile = fopen("sdmc:/3ds/SDL_Log.txt", "a");
  545. if (pFile) {
  546. (void)fprintf(pFile, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
  547. (void)fclose(pFile);
  548. }
  549. }
  550. #endif
  551. #if defined(HAVE_STDIO_H) && \
  552. !(defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))) && \
  553. !(defined(SDL_PLATFORM_WIN32))
  554. (void)fprintf(stderr, "%s%s\n", SDL_GetLogPriorityPrefix(priority), message);
  555. #endif
  556. }
  557. void SDL_GetLogOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
  558. {
  559. if (callback) {
  560. *callback = SDL_log_function;
  561. }
  562. if (userdata) {
  563. *userdata = SDL_log_userdata;
  564. }
  565. }
  566. void SDL_SetLogOutputFunction(SDL_LogOutputFunction callback, void *userdata)
  567. {
  568. SDL_log_function = callback;
  569. SDL_log_userdata = userdata;
  570. }