sokol_log.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #if defined(SOKOL_IMPL) && !defined(SOKOL_LOG_IMPL)
  2. #define SOKOL_LOG_IMPL
  3. #endif
  4. #ifndef SOKOL_LOG_INCLUDED
  5. /*
  6. sokol_log.h -- common logging callback for sokol headers
  7. Project URL: https://github.com/floooh/sokol
  8. Example code: https://github.com/floooh/sokol-samples
  9. Do this:
  10. #define SOKOL_IMPL or
  11. #define SOKOL_LOG_IMPL
  12. before you include this file in *one* C or C++ file to create the
  13. implementation.
  14. Optionally provide the following defines when building the implementation:
  15. SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
  16. SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
  17. SOKOL_LOG_API_DECL - public function declaration prefix (default: extern)
  18. SOKOL_API_DECL - same as SOKOL_GFX_API_DECL
  19. SOKOL_API_IMPL - public function implementation prefix (default: -)
  20. Optionally define the following for verbose output:
  21. SOKOL_DEBUG - by default this is defined if _DEBUG is defined
  22. OVERVIEW
  23. ========
  24. sokol_log.h provides a default logging callback for other sokol headers.
  25. To use the default log callback, just include sokol_log.h and provide
  26. a function pointer to the 'slog_func' function when setting up the
  27. sokol library:
  28. For instance with sokol_audio.h:
  29. #include "sokol_log.h"
  30. ...
  31. saudio_setup(&(saudio_desc){ .logger.func = slog_func });
  32. Logging output goes to stderr and/or a platform specific logging subsystem:
  33. - Windows: stderr + OutputDebugStringA()
  34. - macOS/iOS/Linux: stderr + syslog()
  35. - Emscripten: browser console
  36. - Android: __android_log_write()
  37. In debug mode, a log message might look like this:
  38. [saudio] [error] /Users/floh/projects/sokol/sokol_audio.h:2422:0: COREAUDIO_NEW_OUTPUT_FAILED (id:32)
  39. The source path and line number is formatted in like compiler errors, in some IDEs (like VSCode)
  40. such error messages are clickable.
  41. In release mode, logging is less verbose as to not bloat the executable with string data, but you still get
  42. enough information to identify an error:
  43. [saudio] [error] line:2422 id:32
  44. LICENSE
  45. =======
  46. zlib/libpng license
  47. Copyright (c) 2023 Andre Weissflog
  48. This software is provided 'as-is', without any express or implied warranty.
  49. In no event will the authors be held liable for any damages arising from the
  50. use of this software.
  51. Permission is granted to anyone to use this software for any purpose,
  52. including commercial applications, and to alter it and redistribute it
  53. freely, subject to the following restrictions:
  54. 1. The origin of this software must not be misrepresented; you must not
  55. claim that you wrote the original software. If you use this software in a
  56. product, an acknowledgment in the product documentation would be
  57. appreciated but is not required.
  58. 2. Altered source versions must be plainly marked as such, and must not
  59. be misrepresented as being the original software.
  60. 3. This notice may not be removed or altered from any source
  61. distribution.
  62. */
  63. #define SOKOL_LOG_INCLUDED (1)
  64. #include <stdint.h>
  65. #if defined(SOKOL_API_DECL) && !defined(SOKOL_LOG_API_DECL)
  66. #define SOKOL_LOG_API_DECL SOKOL_API_DECL
  67. #endif
  68. #ifndef SOKOL_LOG_API_DECL
  69. #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_LOG_IMPL)
  70. #define SOKOL_LOG_API_DECL __declspec(dllexport)
  71. #elif defined(_WIN32) && defined(SOKOL_DLL)
  72. #define SOKOL_LOG_API_DECL __declspec(dllimport)
  73. #else
  74. #define SOKOL_LOG_API_DECL extern
  75. #endif
  76. #endif
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /*
  81. Plug this function into the 'logger.func' struct item when initializating any of the sokol
  82. headers. For instance for sokol_audio.h it would loom like this:
  83. saudio_setup(&(saudio_desc){
  84. .logger = {
  85. .func = slog_func
  86. }
  87. });
  88. */
  89. SOKOL_LOG_API_DECL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data);
  90. #ifdef __cplusplus
  91. } // extern "C"
  92. #endif
  93. #endif // SOKOL_LOG_INCLUDED
  94. // ██╗███╗ ███╗██████╗ ██╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗
  95. // ██║████╗ ████║██╔══██╗██║ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║
  96. // ██║██╔████╔██║██████╔╝██║ █████╗ ██╔████╔██║█████╗ ██╔██╗ ██║ ██║ ███████║ ██║ ██║██║ ██║██╔██╗ ██║
  97. // ██║██║╚██╔╝██║██╔═══╝ ██║ ██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚██╗██║ ██║ ██╔══██║ ██║ ██║██║ ██║██║╚██╗██║
  98. // ██║██║ ╚═╝ ██║██║ ███████╗███████╗██║ ╚═╝ ██║███████╗██║ ╚████║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║
  99. // ╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
  100. //
  101. // >>implementation
  102. #ifdef SOKOL_LOG_IMPL
  103. #define SOKOL_LOG_IMPL_INCLUDED (1)
  104. #ifndef SOKOL_API_IMPL
  105. #define SOKOL_API_IMPL
  106. #endif
  107. #ifndef SOKOL_DEBUG
  108. #ifndef NDEBUG
  109. #define SOKOL_DEBUG
  110. #endif
  111. #endif
  112. #ifndef SOKOL_ASSERT
  113. #include <assert.h>
  114. #define SOKOL_ASSERT(c) assert(c)
  115. #endif
  116. #ifndef _SOKOL_PRIVATE
  117. #if defined(__GNUC__) || defined(__clang__)
  118. #define _SOKOL_PRIVATE __attribute__((unused)) static
  119. #else
  120. #define _SOKOL_PRIVATE static
  121. #endif
  122. #endif
  123. #ifndef _SOKOL_UNUSED
  124. #define _SOKOL_UNUSED(x) (void)(x)
  125. #endif
  126. // platform detection
  127. #if defined(__APPLE__)
  128. #define _SLOG_APPLE (1)
  129. #elif defined(__EMSCRIPTEN__)
  130. #define _SLOG_EMSCRIPTEN (1)
  131. #elif defined(_WIN32)
  132. #define _SLOG_WINDOWS (1)
  133. #elif defined(__ANDROID__)
  134. #define _SLOG_ANDROID (1)
  135. #elif defined(__linux__) || defined(__unix__)
  136. #define _SAUDIO_LINUX (1)
  137. #else
  138. #error "sokol_log.h: unknown platform"
  139. #endif
  140. #include <stdlib.h> // abort
  141. #include <stdio.h> // fputs
  142. #include <stddef.h> // size_t
  143. #if defined(_SLOG_WINDOWS)
  144. #ifndef WIN32_LEAN_AND_MEAN
  145. #define WIN32_LEAN_AND_MEAN
  146. #endif
  147. #ifndef NOMINMAX
  148. #define NOMINMAX
  149. #endif
  150. #include <windows.h>
  151. #elif defined(_SLOG_ANDROID)
  152. #include <android/log.h>
  153. #elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
  154. #include <syslog.h>
  155. #endif
  156. _SOKOL_PRIVATE char* _slog_append(const char* str, char* dst, const char* end) {
  157. if (str) {
  158. char c;
  159. while (((c = *str++) != 0) && (dst < (end - 1))) {
  160. *dst++ = c;
  161. }
  162. }
  163. *dst = 0;
  164. return dst;
  165. }
  166. _SOKOL_PRIVATE char* _slog_itoa(uint32_t x, char* buf, size_t buf_size) {
  167. const size_t max_digits_and_null = 11;
  168. if (buf_size < max_digits_and_null) {
  169. return 0;
  170. }
  171. char* p = buf + max_digits_and_null;
  172. *--p = 0;
  173. do {
  174. *--p = '0' + (x % 10);
  175. x /= 10;
  176. } while (x != 0);
  177. return p;
  178. }
  179. #if defined(_SLOG_EMSCRIPTEN)
  180. EM_JS(void, slog_js_log, (uint32_t level, const char* c_str), {
  181. const str = UTF8ToString(c_str);
  182. switch (level) {
  183. case 0: console.error(str); break;
  184. case 1: console.error(str); break;
  185. case 2: console.warn(str); break;
  186. default: console.info(str); break;
  187. }
  188. });
  189. #endif
  190. SOKOL_API_IMPL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data) {
  191. _SOKOL_UNUSED(user_data);
  192. const char* log_level_str;
  193. switch (log_level) {
  194. case 0: log_level_str = "panic"; break;
  195. case 1: log_level_str = "error"; break;
  196. case 2: log_level_str = "warning"; break;
  197. default: log_level_str = "info"; break;
  198. }
  199. // build log output line
  200. char line_buf[256];
  201. char* str = &line_buf[0];
  202. const char* end = str + sizeof(line_buf);
  203. char num_buf[32];
  204. if (tag) {
  205. str = _slog_append("[", str, end);
  206. str = _slog_append(tag, str, end);
  207. str = _slog_append("] ", str, end);
  208. }
  209. str = _slog_append("[", str, end);
  210. str = _slog_append(log_level_str, str, end);
  211. str = _slog_append("] ", str, end);
  212. // if a filename is provided, build a clickable log message that's compatible with compiler error messages
  213. if (filename) {
  214. #if defined(_MSC_VER)
  215. // MSVC compiler error format
  216. str = _slog_append(filename, str, end);
  217. str = _slog_append("(", str, end);
  218. str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
  219. str = _slog_append("): ");
  220. #else
  221. // gcc/clang compiler error format
  222. str = _slog_append(filename, str, end);
  223. str = _slog_append(":", str, end);
  224. str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
  225. str = _slog_append(":0: ", str, end);
  226. #endif
  227. if (message) {
  228. str = _slog_append(message, str, end);
  229. str = _slog_append(" ", str, end);
  230. }
  231. else {
  232. str = _slog_append("??? ", str, end);
  233. }
  234. str = _slog_append("(id:", str, end);
  235. str = _slog_append(_slog_itoa(log_item, num_buf, sizeof(num_buf)), str, end);
  236. str = _slog_append(")", str, end);
  237. }
  238. else {
  239. // no filename provided, print what we can
  240. str = _slog_append("line:", str, end);
  241. str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
  242. str = _slog_append(" id:", str, end);
  243. str = _slog_append(_slog_itoa(log_item, num_buf, sizeof(num_buf)), str, end);
  244. if (message) {
  245. str = _slog_append(" msg: ", str, end);
  246. str = _slog_append(message, str, end);
  247. }
  248. }
  249. str = _slog_append("\n", str, end);
  250. // print to stderr?
  251. #if defined(_SLOG_LINUX) || defined(_SLOG_WINDOWS) || defined(_SLOG_APPLE)
  252. fputs(line_buf, stderr);
  253. #endif
  254. // platform specific logging calls
  255. #if defined(_SLOG_WINDOWS)
  256. OutputDebugStringA(line_buf);
  257. #elif defined(_SLOG_ANDROID)
  258. int prio;
  259. switch (log_level) {
  260. case 0: prio = ANDROID_LOG_FATAL; break;
  261. case 1: prio = ANDROID_LOG_ERROR; break;
  262. case 2: prio = ANDROID_LOG_WARN; break;
  263. default: prio = ANDROID_LOG_INFO; break;
  264. }
  265. __android_log_write(prio, "SOKOL", line_buf);
  266. #elif defined(_SLOG_EMSCRIPTEN)
  267. slog_js_log(log_level, line_buf);
  268. #elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
  269. int prio;
  270. switch (log_level) {
  271. case 0: prio = LOG_CRIT; break;
  272. case 1: prio = LOG_ERR; break;
  273. case 2: prio = LOG_WARNING; break;
  274. default: prio = LOG_INFO; break;
  275. }
  276. syslog(prio, "%s", line_buf);
  277. #endif
  278. if (0 == log_level) {
  279. abort();
  280. }
  281. }
  282. #endif // SOKOL_LOG_IMPL