aboutsummaryrefslogtreecommitdiff
path: root/util/sokol_debugtext.h
diff options
context:
space:
mode:
authorManuel Floruß <manuel.floruss@gmail.com>2022-10-03 10:49:47 +0200
committerManuel Floruß <manuel.floruss@gmail.com>2022-10-03 10:49:47 +0200
commit3797da8c7f140f1406671a8ddcac1f7a2d4fc354 (patch)
tree6cb39b6cf0d3a931a58137a87e733c85ab8f9d00 /util/sokol_debugtext.h
parent1ad3278a233b75c19d1ec63c9118d33f5c1947c8 (diff)
Replace SOKOL_LOG with runtime callbacks.
Also removes SOKOL_LOG from sokol_fontstash.h and sokol_args.h because it's not used there.
Diffstat (limited to 'util/sokol_debugtext.h')
-rw-r--r--util/sokol_debugtext.h78
1 files changed, 71 insertions, 7 deletions
diff --git a/util/sokol_debugtext.h b/util/sokol_debugtext.h
index ae69904d..3201d9e3 100644
--- a/util/sokol_debugtext.h
+++ b/util/sokol_debugtext.h
@@ -31,7 +31,6 @@
SOKOL_DEBUGTEXT_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_DEBUGTEXT_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
- SOKOL_LOG(msg) - your own logging function (default: puts(msg))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
If sokol_debugtext.h is compiled as a DLL, define the following before
@@ -396,6 +395,27 @@
If no overrides are provided, malloc and free will be used.
+ LOG FUNCTION OVERRIDE
+ =====================
+ You can override the log function at initialization time like this:
+
+ void my_log(const char* message, void* user_data) {
+ printf("sdtx says: \s\n", message);
+ }
+
+ ...
+ sdtx_setup(&(sdtx_desc_t){
+ // ...
+ .logger = {
+ .log_cb = my_log,
+ .user_data = ...,
+ }
+ });
+ ...
+
+ If no overrides are provided, puts will be used on most platforms.
+ On Android, __android_log_write will be used instead.
+
LICENSE
=======
zlib/libpng license
@@ -544,6 +564,17 @@ typedef struct sdtx_allocator_t {
} sdtx_allocator_t;
/*
+ sdtx_logger_t
+
+ Used in sdtx_desc_t to provide custom log callbacks to sokol_debugtext.h.
+ Default behavior is SDTX_LOG_DEFAULT(message).
+*/
+typedef struct sdtx_logger_t {
+ void (*log_cb)(const char* message, void* user_data);
+ void* user_data;
+} sdtx_logger_t;
+
+/*
sdtx_desc_t
Describes the sokol-debugtext API initialization parameters. Passed
@@ -565,6 +596,7 @@ typedef struct sdtx_desc_t {
sdtx_font_desc_t fonts[SDTX_MAX_FONTS]; // up to 8 fonts descriptions
sdtx_context_desc_t context; // the default context creation parameters
sdtx_allocator_t allocator; // optional memory allocation overrides (default: malloc/free)
+ sdtx_logger_t logger; // optional log override functions (default: SDTX_LOG_DEFAULT(message))
} sdtx_desc_t;
/* initialization/shutdown */
@@ -655,14 +687,35 @@ inline sdtx_context sdtx_make_context(const sdtx_context_desc_t& desc) { return
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
-#ifndef SOKOL_LOG
+
+#if defined(SOKOL_LOG)
+#error "SOKOL_LOG macro is no longer supported, please use sg_desc.logger to override log functions"
+#endif
+#ifndef SOKOL_NO_LOG
#ifdef SOKOL_DEBUG
- #include <stdio.h>
- #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); }
+ #define SOKOL_NO_LOG 0
#else
- #define SOKOL_LOG(s)
+ #define SOKOL_NO_LOG 1
+ #endif
+#endif
+#if !SOKOL_NO_LOG
+ #define SDTX_LOG(s) { SOKOL_ASSERT(s); _sdtx_log(s); }
+ #ifndef SDTX_LOG_DEFAULT
+ #if defined(__ANDROID__)
+ #include <android/log.h>
+ #define SDTX_LOG_DEFAULT(s) __android_log_write(ANDROID_LOG_INFO, "SOKOL_DEBUGTEXT", s)
+ #else
+ #include <stdio.h>
+ #define SDTX_LOG_DEFAULT(s) puts(s)
+ #endif
#endif
+#else
+ #define SDTX_LOG(s)
+#endif
+#ifndef SDTX_LOG_DEFAULT
+ #define SDTX_LOG_DEFAULT(s)
#endif
+
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
@@ -3510,6 +3563,17 @@ static void _sdtx_free(void* ptr) {
}
}
+#if !SOKOL_NO_LOG
+static void _sdtx_log(const char* msg) {
+ SOKOL_ASSERT(msg);
+ if (_sdtx.desc.logger.log_cb) {
+ _sdtx.desc.logger.log_cb(msg, _sdtx.desc.logger.user_data);
+ } else {
+ SDTX_LOG_DEFAULT(msg);
+ }
+}
+#endif
+
/*=== CONTEXT POOL ===========================================================*/
static void _sdtx_init_pool(_sdtx_pool_t* pool, int num) {
SOKOL_ASSERT(pool && (num >= 1));
@@ -4017,7 +4081,7 @@ SOKOL_API_IMPL sdtx_context sdtx_make_context(const sdtx_context_desc_t* desc) {
_sdtx_init_context(ctx_id, desc);
}
else {
- SOKOL_LOG("sokol_debugtext.h: context pool exhausted!");
+ SDTX_LOG("sokol_debugtext.h: context pool exhausted!");
}
return ctx_id;
}
@@ -4025,7 +4089,7 @@ SOKOL_API_IMPL sdtx_context sdtx_make_context(const sdtx_context_desc_t* desc) {
SOKOL_API_IMPL void sdtx_destroy_context(sdtx_context ctx_id) {
SOKOL_ASSERT(_SDTX_INIT_COOKIE == _sdtx.init_cookie);
if (_sdtx_is_default_context(ctx_id)) {
- SOKOL_LOG("sokol_debugtext.h: cannot destroy default context");
+ SDTX_LOG("sokol_debugtext.h: cannot destroy default context");
return;
}
_sdtx_destroy_context(ctx_id);