🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
13 KiB

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