SDL_log.c 14 KB

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