SDL_log.c 14 KB

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