1
0

SDL_log.c 12 KB

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