SDL_log.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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_WINDOWS)
  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 CleanupLogPriorities(void);
  43. static void CleanupLogPrefixes(void);
  44. static SDL_AtomicInt SDL_log_initializing;
  45. static SDL_AtomicInt SDL_log_initialized;
  46. static SDL_Mutex *SDL_log_lock;
  47. static SDL_Mutex *SDL_log_function_lock;
  48. static SDL_LogLevel *SDL_loglevels SDL_GUARDED_BY(SDL_log_lock);
  49. static SDL_LogPriority SDL_log_priorities[SDL_LOG_CATEGORY_CUSTOM] SDL_GUARDED_BY(SDL_log_lock);
  50. static SDL_LogPriority SDL_log_default_priority SDL_GUARDED_BY(SDL_log_lock);
  51. static SDL_LogOutputFunction SDL_log_function SDL_GUARDED_BY(SDL_log_function_lock) = SDL_LogOutput;
  52. static void *SDL_log_userdata SDL_GUARDED_BY(SDL_log_function_lock) = NULL;
  53. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  54. #pragma GCC diagnostic push
  55. #pragma GCC diagnostic ignored "-Wunused-variable"
  56. #endif
  57. // If this list changes, update the documentation for SDL_HINT_LOGGING
  58. static const char * const SDL_priority_names[] = {
  59. NULL,
  60. "TRACE",
  61. "VERBOSE",
  62. "DEBUG",
  63. "INFO",
  64. "WARN",
  65. "ERROR",
  66. "CRITICAL"
  67. };
  68. SDL_COMPILE_TIME_ASSERT(priority_names, SDL_arraysize(SDL_priority_names) == SDL_LOG_PRIORITY_COUNT);
  69. // This is guarded by SDL_log_function_lock because it's the logging function that calls GetLogPriorityPrefix()
  70. static char *SDL_priority_prefixes[SDL_LOG_PRIORITY_COUNT] SDL_GUARDED_BY(SDL_log_function_lock);
  71. // If this list changes, update the documentation for SDL_HINT_LOGGING
  72. static const char * const SDL_category_names[] = {
  73. "APP",
  74. "ERROR",
  75. "ASSERT",
  76. "SYSTEM",
  77. "AUDIO",
  78. "VIDEO",
  79. "RENDER",
  80. "INPUT",
  81. "TEST",
  82. "GPU"
  83. };
  84. SDL_COMPILE_TIME_ASSERT(category_names, SDL_arraysize(SDL_category_names) == SDL_LOG_CATEGORY_RESERVED2);
  85. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  86. #pragma GCC diagnostic pop
  87. #endif
  88. #ifdef SDL_PLATFORM_ANDROID
  89. static int SDL_android_priority[] = {
  90. ANDROID_LOG_UNKNOWN,
  91. ANDROID_LOG_VERBOSE,
  92. ANDROID_LOG_VERBOSE,
  93. ANDROID_LOG_DEBUG,
  94. ANDROID_LOG_INFO,
  95. ANDROID_LOG_WARN,
  96. ANDROID_LOG_ERROR,
  97. ANDROID_LOG_FATAL
  98. };
  99. SDL_COMPILE_TIME_ASSERT(android_priority, SDL_arraysize(SDL_android_priority) == SDL_LOG_PRIORITY_COUNT);
  100. #endif // SDL_PLATFORM_ANDROID
  101. static void SDLCALL SDL_LoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  102. {
  103. SDL_ResetLogPriorities();
  104. }
  105. void SDL_InitLog(void)
  106. {
  107. if (SDL_AtomicGet(&SDL_log_initialized)) {
  108. return;
  109. }
  110. // Do a little tap dance to make sure only one thread initializes logging
  111. if (!SDL_AtomicCompareAndSwap(&SDL_log_initializing, false, true)) {
  112. // Wait for the other thread to complete initialization
  113. while (!SDL_AtomicGet(&SDL_log_initialized)) {
  114. SDL_Delay(1);
  115. }
  116. return;
  117. }
  118. // If these fail we'll continue without them.
  119. SDL_log_lock = SDL_CreateMutex();
  120. SDL_log_function_lock = SDL_CreateMutex();
  121. SDL_AddHintCallback(SDL_HINT_LOGGING, SDL_LoggingChanged, NULL);
  122. SDL_AtomicSet(&SDL_log_initializing, false);
  123. SDL_AtomicSet(&SDL_log_initialized, true);
  124. }
  125. void SDL_QuitLog(void)
  126. {
  127. SDL_RemoveHintCallback(SDL_HINT_LOGGING, SDL_LoggingChanged, NULL);
  128. CleanupLogPriorities();
  129. CleanupLogPrefixes();
  130. if (SDL_log_lock) {
  131. SDL_DestroyMutex(SDL_log_lock);
  132. SDL_log_lock = NULL;
  133. }
  134. if (SDL_log_function_lock) {
  135. SDL_DestroyMutex(SDL_log_function_lock);
  136. SDL_log_function_lock = NULL;
  137. }
  138. SDL_AtomicSet(&SDL_log_initialized, false);
  139. }
  140. static void SDL_CheckInitLog()
  141. {
  142. if (!SDL_AtomicGet(&SDL_log_initialized) &&
  143. !SDL_AtomicGet(&SDL_log_initializing)) {
  144. SDL_InitLog();
  145. }
  146. }
  147. static void CleanupLogPriorities(void)
  148. {
  149. while (SDL_loglevels) {
  150. SDL_LogLevel *entry = SDL_loglevels;
  151. SDL_loglevels = entry->next;
  152. SDL_free(entry);
  153. }
  154. }
  155. void SDL_SetLogPriorities(SDL_LogPriority priority)
  156. {
  157. SDL_CheckInitLog();
  158. SDL_LockMutex(SDL_log_lock);
  159. {
  160. CleanupLogPriorities();
  161. SDL_log_default_priority = priority;
  162. for (int i = 0; i < SDL_arraysize(SDL_log_priorities); ++i) {
  163. SDL_log_priorities[i] = priority;
  164. }
  165. }
  166. SDL_UnlockMutex(SDL_log_lock);
  167. }
  168. void SDL_SetLogPriority(int category, SDL_LogPriority priority)
  169. {
  170. SDL_LogLevel *entry;
  171. SDL_CheckInitLog();
  172. SDL_LockMutex(SDL_log_lock);
  173. {
  174. if (category >= 0 && category < SDL_arraysize(SDL_log_priorities)) {
  175. SDL_log_priorities[category] = priority;
  176. } else {
  177. for (entry = SDL_loglevels; entry; entry = entry->next) {
  178. if (entry->category == category) {
  179. entry->priority = priority;
  180. break;
  181. }
  182. }
  183. if (!entry) {
  184. entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
  185. if (entry) {
  186. entry->category = category;
  187. entry->priority = priority;
  188. entry->next = SDL_loglevels;
  189. SDL_loglevels = entry;
  190. }
  191. }
  192. }
  193. }
  194. SDL_UnlockMutex(SDL_log_lock);
  195. }
  196. SDL_LogPriority SDL_GetLogPriority(int category)
  197. {
  198. SDL_LogLevel *entry;
  199. SDL_LogPriority priority = SDL_LOG_PRIORITY_INVALID;
  200. SDL_CheckInitLog();
  201. // Bypass the lock for known categories
  202. // Technically if the priority was set on a different CPU the value might not
  203. // be visible on this CPU for a while, but in practice it's fast enough that
  204. // this performance improvement is worthwhile.
  205. if (category >= 0 && category < SDL_arraysize(SDL_log_priorities)) {
  206. return SDL_log_priorities[category];
  207. }
  208. SDL_LockMutex(SDL_log_lock);
  209. {
  210. if (category >= 0 && category < SDL_arraysize(SDL_log_priorities)) {
  211. priority = SDL_log_priorities[category];
  212. } else {
  213. for (entry = SDL_loglevels; entry; entry = entry->next) {
  214. if (entry->category == category) {
  215. priority = entry->priority;
  216. break;
  217. }
  218. }
  219. if (priority == SDL_LOG_PRIORITY_INVALID) {
  220. priority = SDL_log_default_priority;
  221. }
  222. }
  223. }
  224. SDL_UnlockMutex(SDL_log_lock);
  225. return priority;
  226. }
  227. static bool ParseLogCategory(const char *string, size_t length, int *category)
  228. {
  229. int i;
  230. if (SDL_isdigit(*string)) {
  231. *category = SDL_atoi(string);
  232. return true;
  233. }
  234. if (*string == '*') {
  235. *category = DEFAULT_CATEGORY;
  236. return true;
  237. }
  238. for (i = 0; i < SDL_arraysize(SDL_category_names); ++i) {
  239. if (SDL_strncasecmp(string, SDL_category_names[i], length) == 0) {
  240. *category = i;
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. static bool ParseLogPriority(const char *string, size_t length, SDL_LogPriority *priority)
  247. {
  248. int i;
  249. if (SDL_isdigit(*string)) {
  250. i = SDL_atoi(string);
  251. if (i == 0) {
  252. // 0 has a special meaning of "disable this category"
  253. *priority = SDL_LOG_PRIORITY_COUNT;
  254. return true;
  255. }
  256. if (i > SDL_LOG_PRIORITY_INVALID && i < SDL_LOG_PRIORITY_COUNT) {
  257. *priority = (SDL_LogPriority)i;
  258. return true;
  259. }
  260. return false;
  261. }
  262. if (SDL_strncasecmp(string, "quiet", length) == 0) {
  263. *priority = SDL_LOG_PRIORITY_COUNT;
  264. return true;
  265. }
  266. for (i = SDL_LOG_PRIORITY_INVALID + 1; i < SDL_LOG_PRIORITY_COUNT; ++i) {
  267. if (SDL_strncasecmp(string, SDL_priority_names[i], length) == 0) {
  268. *priority = (SDL_LogPriority)i;
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. static void ParseLogPriorities(const char *hint)
  275. {
  276. const char *name, *next;
  277. int category = DEFAULT_CATEGORY;
  278. SDL_LogPriority priority = SDL_LOG_PRIORITY_INVALID;
  279. if (SDL_strchr(hint, '=') == NULL) {
  280. if (ParseLogPriority(hint, SDL_strlen(hint), &priority)) {
  281. SDL_SetLogPriorities(priority);
  282. }
  283. return;
  284. }
  285. for (name = hint; name; name = next) {
  286. const char *sep = SDL_strchr(name, '=');
  287. if (!sep) {
  288. break;
  289. }
  290. next = SDL_strchr(sep, ',');
  291. if (next) {
  292. ++next;
  293. }
  294. if (ParseLogCategory(name, (sep - name), &category)) {
  295. const char *value = sep + 1;
  296. size_t len;
  297. if (next) {
  298. len = (next - value - 1);
  299. } else {
  300. len = SDL_strlen(value);
  301. }
  302. if (ParseLogPriority(value, len, &priority)) {
  303. if (category == DEFAULT_CATEGORY) {
  304. for (int i = 0; i < SDL_arraysize(SDL_log_priorities); ++i) {
  305. if (SDL_log_priorities[i] == SDL_LOG_PRIORITY_INVALID) {
  306. SDL_log_priorities[i] = priority;
  307. }
  308. }
  309. SDL_log_default_priority = priority;
  310. } else {
  311. SDL_SetLogPriority(category, priority);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. void SDL_ResetLogPriorities(void)
  318. {
  319. SDL_CheckInitLog();
  320. SDL_LockMutex(SDL_log_lock);
  321. {
  322. CleanupLogPriorities();
  323. SDL_log_default_priority = SDL_LOG_PRIORITY_INVALID;
  324. for (int i = 0; i < SDL_arraysize(SDL_log_priorities); ++i) {
  325. SDL_log_priorities[i] = SDL_LOG_PRIORITY_INVALID;
  326. }
  327. const char *hint = SDL_GetHint(SDL_HINT_LOGGING);
  328. if (hint) {
  329. ParseLogPriorities(hint);
  330. }
  331. if (SDL_log_default_priority == SDL_LOG_PRIORITY_INVALID) {
  332. SDL_log_default_priority = SDL_LOG_PRIORITY_ERROR;
  333. }
  334. for (int i = 0; i < SDL_arraysize(SDL_log_priorities); ++i) {
  335. if (SDL_log_priorities[i] != SDL_LOG_PRIORITY_INVALID) {
  336. continue;
  337. }
  338. switch (i) {
  339. case SDL_LOG_CATEGORY_APPLICATION:
  340. SDL_log_priorities[i] = SDL_LOG_PRIORITY_INFO;
  341. break;
  342. case SDL_LOG_CATEGORY_ASSERT:
  343. SDL_log_priorities[i] = SDL_LOG_PRIORITY_WARN;
  344. break;
  345. case SDL_LOG_CATEGORY_TEST:
  346. SDL_log_priorities[i] = SDL_LOG_PRIORITY_VERBOSE;
  347. break;
  348. default:
  349. SDL_log_priorities[i] = SDL_LOG_PRIORITY_ERROR;
  350. break;
  351. }
  352. }
  353. }
  354. SDL_UnlockMutex(SDL_log_lock);
  355. }
  356. static void CleanupLogPrefixes(void)
  357. {
  358. for (int i = 0; i < SDL_arraysize(SDL_priority_prefixes); ++i) {
  359. if (SDL_priority_prefixes[i]) {
  360. SDL_free(SDL_priority_prefixes[i]);
  361. SDL_priority_prefixes[i] = NULL;
  362. }
  363. }
  364. }
  365. static const char *GetLogPriorityPrefix(SDL_LogPriority priority)
  366. {
  367. if (priority <= SDL_LOG_PRIORITY_INVALID || priority >= SDL_LOG_PRIORITY_COUNT) {
  368. return "";
  369. }
  370. if (SDL_priority_prefixes[priority]) {
  371. return SDL_priority_prefixes[priority];
  372. }
  373. switch (priority) {
  374. case SDL_LOG_PRIORITY_WARN:
  375. return "WARNING: ";
  376. case SDL_LOG_PRIORITY_ERROR:
  377. return "ERROR: ";
  378. case SDL_LOG_PRIORITY_CRITICAL:
  379. return "ERROR: ";
  380. default:
  381. return "";
  382. }
  383. }
  384. SDL_bool SDL_SetLogPriorityPrefix(SDL_LogPriority priority, const char *prefix)
  385. {
  386. char *prefix_copy;
  387. if (priority <= SDL_LOG_PRIORITY_INVALID || priority >= SDL_LOG_PRIORITY_COUNT) {
  388. return SDL_InvalidParamError("priority");
  389. }
  390. if (!prefix || !*prefix) {
  391. prefix_copy = SDL_strdup("");
  392. } else {
  393. prefix_copy = SDL_strdup(prefix);
  394. }
  395. if (!prefix_copy) {
  396. return false;
  397. }
  398. SDL_LockMutex(SDL_log_function_lock);
  399. {
  400. if (SDL_priority_prefixes[priority]) {
  401. SDL_free(SDL_priority_prefixes[priority]);
  402. }
  403. SDL_priority_prefixes[priority] = prefix_copy;
  404. }
  405. SDL_UnlockMutex(SDL_log_function_lock);
  406. return true;
  407. }
  408. void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  409. {
  410. va_list ap;
  411. va_start(ap, fmt);
  412. SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  413. va_end(ap);
  414. }
  415. void SDL_LogTrace(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  416. {
  417. va_list ap;
  418. va_start(ap, fmt);
  419. SDL_LogMessageV(category, SDL_LOG_PRIORITY_TRACE, fmt, ap);
  420. va_end(ap);
  421. }
  422. void SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  423. {
  424. va_list ap;
  425. va_start(ap, fmt);
  426. SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
  427. va_end(ap);
  428. }
  429. void SDL_LogDebug(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  430. {
  431. va_list ap;
  432. va_start(ap, fmt);
  433. SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
  434. va_end(ap);
  435. }
  436. void SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  437. {
  438. va_list ap;
  439. va_start(ap, fmt);
  440. SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
  441. va_end(ap);
  442. }
  443. void SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  444. {
  445. va_list ap;
  446. va_start(ap, fmt);
  447. SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
  448. va_end(ap);
  449. }
  450. void SDL_LogError(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  451. {
  452. va_list ap;
  453. va_start(ap, fmt);
  454. SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
  455. va_end(ap);
  456. }
  457. void SDL_LogCritical(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  458. {
  459. va_list ap;
  460. va_start(ap, fmt);
  461. SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
  462. va_end(ap);
  463. }
  464. void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  465. {
  466. va_list ap;
  467. va_start(ap, fmt);
  468. SDL_LogMessageV(category, priority, fmt, ap);
  469. va_end(ap);
  470. }
  471. #ifdef SDL_PLATFORM_ANDROID
  472. static const char *GetCategoryPrefix(int category)
  473. {
  474. if (category < SDL_LOG_CATEGORY_RESERVED2) {
  475. return SDL_category_names[category];
  476. }
  477. if (category < SDL_LOG_CATEGORY_CUSTOM) {
  478. return "RESERVED";
  479. }
  480. return "CUSTOM";
  481. }
  482. #endif // SDL_PLATFORM_ANDROID
  483. void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  484. {
  485. char *message = NULL;
  486. char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
  487. size_t len_plus_term;
  488. int len;
  489. va_list aq;
  490. // Nothing to do if we don't have an output function
  491. if (!SDL_log_function) {
  492. return;
  493. }
  494. // See if we want to do anything with this message
  495. if (priority < SDL_GetLogPriority(category)) {
  496. return;
  497. }
  498. // Render into stack buffer
  499. va_copy(aq, ap);
  500. len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
  501. va_end(aq);
  502. if (len < 0) {
  503. return;
  504. }
  505. // If message truncated, allocate and re-render
  506. if (len >= sizeof(stack_buf) && SDL_size_add_check_overflow(len, 1, &len_plus_term)) {
  507. // Allocate exactly what we need, including the zero-terminator
  508. message = (char *)SDL_malloc(len_plus_term);
  509. if (!message) {
  510. return;
  511. }
  512. va_copy(aq, ap);
  513. len = SDL_vsnprintf(message, len_plus_term, fmt, aq);
  514. va_end(aq);
  515. } else {
  516. message = stack_buf;
  517. }
  518. // Chop off final endline.
  519. if ((len > 0) && (message[len - 1] == '\n')) {
  520. message[--len] = '\0';
  521. if ((len > 0) && (message[len - 1] == '\r')) { // catch "\r\n", too.
  522. message[--len] = '\0';
  523. }
  524. }
  525. SDL_LockMutex(SDL_log_function_lock);
  526. {
  527. SDL_log_function(SDL_log_userdata, category, priority, message);
  528. }
  529. SDL_UnlockMutex(SDL_log_function_lock);
  530. // Free only if dynamically allocated
  531. if (message != stack_buf) {
  532. SDL_free(message);
  533. }
  534. }
  535. #if defined(SDL_PLATFORM_WIN32) && !defined(SDL_PLATFORM_GDK)
  536. enum {
  537. CONSOLE_UNATTACHED = 0,
  538. CONSOLE_ATTACHED_CONSOLE = 1,
  539. CONSOLE_ATTACHED_FILE = 2,
  540. CONSOLE_ATTACHED_ERROR = -1,
  541. } consoleAttached = CONSOLE_UNATTACHED;
  542. // Handle to stderr output of console.
  543. static HANDLE stderrHandle = NULL;
  544. #endif
  545. static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
  546. const char *message)
  547. {
  548. #if defined(SDL_PLATFORM_WINDOWS)
  549. // Way too many allocations here, urgh
  550. // Note: One can't call SDL_SetError here, since that function itself logs.
  551. {
  552. char *output;
  553. size_t length;
  554. LPTSTR tstr;
  555. bool isstack;
  556. #if !defined(SDL_PLATFORM_GDK)
  557. BOOL attachResult;
  558. DWORD attachError;
  559. DWORD consoleMode;
  560. DWORD charsWritten;
  561. // Maybe attach console and get stderr handle
  562. if (consoleAttached == CONSOLE_UNATTACHED) {
  563. attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
  564. if (!attachResult) {
  565. attachError = GetLastError();
  566. if (attachError == ERROR_INVALID_HANDLE) {
  567. // This is expected when running from Visual Studio
  568. // OutputDebugString(TEXT("Parent process has no console\r\n"));
  569. consoleAttached = CONSOLE_ATTACHED_ERROR;
  570. } else if (attachError == ERROR_GEN_FAILURE) {
  571. OutputDebugString(TEXT("Could not attach to console of parent process\r\n"));
  572. consoleAttached = CONSOLE_ATTACHED_ERROR;
  573. } else if (attachError == ERROR_ACCESS_DENIED) {
  574. // Already attached
  575. consoleAttached = CONSOLE_ATTACHED_CONSOLE;
  576. } else {
  577. OutputDebugString(TEXT("Error attaching console\r\n"));
  578. consoleAttached = CONSOLE_ATTACHED_ERROR;
  579. }
  580. } else {
  581. // Newly attached
  582. consoleAttached = CONSOLE_ATTACHED_CONSOLE;
  583. }
  584. if (consoleAttached == CONSOLE_ATTACHED_CONSOLE) {
  585. stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
  586. if (GetConsoleMode(stderrHandle, &consoleMode) == 0) {
  587. // WriteConsole fails if the output is redirected to a file. Must use WriteFile instead.
  588. consoleAttached = CONSOLE_ATTACHED_FILE;
  589. }
  590. }
  591. }
  592. #endif // !defined(SDL_PLATFORM_GDK)
  593. length = SDL_strlen(GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
  594. output = SDL_small_alloc(char, length, &isstack);
  595. (void)SDL_snprintf(output, length, "%s%s\r\n", GetLogPriorityPrefix(priority), message);
  596. tstr = WIN_UTF8ToString(output);
  597. // Output to debugger
  598. OutputDebugString(tstr);
  599. #if !defined(SDL_PLATFORM_GDK)
  600. // Screen output to stderr, if console was attached.
  601. if (consoleAttached == CONSOLE_ATTACHED_CONSOLE) {
  602. if (!WriteConsole(stderrHandle, tstr, (DWORD)SDL_tcslen(tstr), &charsWritten, NULL)) {
  603. OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
  604. if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  605. OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
  606. }
  607. }
  608. } else if (consoleAttached == CONSOLE_ATTACHED_FILE) {
  609. if (!WriteFile(stderrHandle, output, (DWORD)SDL_strlen(output), &charsWritten, NULL)) {
  610. OutputDebugString(TEXT("Error calling WriteFile\r\n"));
  611. }
  612. }
  613. #endif // !defined(SDL_PLATFORM_GDK)
  614. SDL_free(tstr);
  615. SDL_small_free(output, isstack);
  616. }
  617. #elif defined(SDL_PLATFORM_ANDROID)
  618. {
  619. char tag[32];
  620. SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
  621. __android_log_write(SDL_android_priority[priority], tag, message);
  622. }
  623. #elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
  624. /* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
  625. */
  626. extern void SDL_NSLog(const char *prefix, const char *text);
  627. {
  628. SDL_NSLog(GetLogPriorityPrefix(priority), message);
  629. return;
  630. }
  631. #elif defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_PS2)
  632. {
  633. FILE *pFile;
  634. pFile = fopen("SDL_Log.txt", "a");
  635. if (pFile) {
  636. (void)fprintf(pFile, "%s%s\n", GetLogPriorityPrefix(priority), message);
  637. (void)fclose(pFile);
  638. }
  639. }
  640. #elif defined(SDL_PLATFORM_VITA)
  641. {
  642. FILE *pFile;
  643. pFile = fopen("ux0:/data/SDL_Log.txt", "a");
  644. if (pFile) {
  645. (void)fprintf(pFile, "%s%s\n", GetLogPriorityPrefix(priority), message);
  646. (void)fclose(pFile);
  647. }
  648. }
  649. #elif defined(SDL_PLATFORM_3DS)
  650. {
  651. FILE *pFile;
  652. pFile = fopen("sdmc:/3ds/SDL_Log.txt", "a");
  653. if (pFile) {
  654. (void)fprintf(pFile, "%s%s\n", GetLogPriorityPrefix(priority), message);
  655. (void)fclose(pFile);
  656. }
  657. }
  658. #endif
  659. #if defined(HAVE_STDIO_H) && \
  660. !(defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))) && \
  661. !(defined(SDL_PLATFORM_WIN32))
  662. (void)fprintf(stderr, "%s%s\n", GetLogPriorityPrefix(priority), message);
  663. #endif
  664. }
  665. void SDL_GetLogOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
  666. {
  667. SDL_LockMutex(SDL_log_function_lock);
  668. {
  669. if (callback) {
  670. *callback = SDL_log_function;
  671. }
  672. if (userdata) {
  673. *userdata = SDL_log_userdata;
  674. }
  675. }
  676. SDL_UnlockMutex(SDL_log_function_lock);
  677. }
  678. void SDL_SetLogOutputFunction(SDL_LogOutputFunction callback, void *userdata)
  679. {
  680. SDL_LockMutex(SDL_log_function_lock);
  681. {
  682. SDL_log_function = callback;
  683. SDL_log_userdata = userdata;
  684. }
  685. SDL_UnlockMutex(SDL_log_function_lock);
  686. }