SDL_log.c 12 KB

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