aboutsummaryrefslogtreecommitdiff
path: root/util
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
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')
-rw-r--r--util/sokol_debugtext.h78
-rw-r--r--util/sokol_fontstash.h9
-rw-r--r--util/sokol_gl.h83
3 files changed, 143 insertions, 27 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);
diff --git a/util/sokol_fontstash.h b/util/sokol_fontstash.h
index af9f18c1..16d288ab 100644
--- a/util/sokol_fontstash.h
+++ b/util/sokol_fontstash.h
@@ -31,7 +31,6 @@
SOKOL_FONTSTASH_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_FONTSTASH_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))
Include the following headers before including sokol_fontstash.h:
@@ -273,14 +272,6 @@ SOKOL_FONTSTASH_API_DECL uint32_t sfons_rgba(uint8_t r, uint8_t g, uint8_t b, ui
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
-#ifndef SOKOL_LOG
- #ifdef SOKOL_DEBUG
- #include <stdio.h>
- #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); }
- #else
- #define SOKOL_LOG(s)
- #endif
-#endif
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
diff --git a/util/sokol_gl.h b/util/sokol_gl.h
index 5f8e565c..1f4f4d05 100644
--- a/util/sokol_gl.h
+++ b/util/sokol_gl.h
@@ -30,7 +30,6 @@
SOKOL_GL_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GL_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_gl.h is compiled as a DLL, define the following before
@@ -573,6 +572,28 @@
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("sgl says: \s\n", message);
+ }
+
+ ...
+ sgl_setup(&(sgl_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
@@ -675,6 +696,17 @@ typedef struct sgl_allocator_t {
void* user_data;
} sgl_allocator_t;
+/*
+ sgl_logger_t
+
+ Used in sgl_desc_t to provide custom log callbacks to sokol_gl.h.
+ Default behavior is SGL_LOG_DEFAULT(message).
+*/
+typedef struct sgl_logger_t {
+ void (*log_cb)(const char* message, void* user_data);
+ void* user_data;
+} sgl_logger_t;
+
typedef struct sgl_desc_t {
int max_vertices; // default: 64k
int max_commands; // default: 16k
@@ -685,6 +717,7 @@ typedef struct sgl_desc_t {
int sample_count;
sg_face_winding face_winding; // default: SG_FACEWINDING_CCW
sgl_allocator_t allocator; // optional memory allocation overrides (default: malloc/free)
+ sgl_logger_t logger; // optional memory allocation overrides (default: SGL_LOG_DEFAULT(message))
} sgl_desc_t;
/* the default context handle */
@@ -830,16 +863,33 @@ inline sgl_pipeline sgl_context_make_pipeline(sgl_context ctx, const sg_pipeline
#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 sgl_desc_t.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
-#ifndef SOKOL_UNREACHABLE
- #define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
+#if !SOKOL_NO_LOG
+ #define SGL_LOG(s) { SOKOL_ASSERT(s); _sgl_log(s); }
+ #ifndef SGL_LOG_DEFAULT
+ #if defined(__ANDROID__)
+ #include <android/log.h>
+ #define SGL_LOG_DEFAULT(s) __android_log_write(ANDROID_LOG_INFO, "SOKOL_GL", s)
+ #else
+ #include <stdio.h>
+ #define SGL_LOG_DEFAULT(s) puts(s)
+ #endif
+ #endif
+#else
+ #define SGL_LOG(s)
+#endif
+#ifndef SGL_LOG_DEFAULT
+ #define SGL_LOG_DEFAULT(s)
#endif
#define _sgl_def(val, def) (((val) == 0) ? (def) : (val))
@@ -2340,6 +2390,17 @@ static void _sgl_free(void* ptr) {
}
}
+#if !SOKOL_NO_LOG
+static void _sgl_log(const char* msg) {
+ SOKOL_ASSERT(msg);
+ if (_sgl.desc.logger.log_cb) {
+ _sgl.desc.logger.log_cb(msg, _sgl.desc.logger.user_data);
+ } else {
+ SGL_LOG_DEFAULT(msg);
+ }
+}
+#endif
+
static void _sgl_init_pool(_sgl_pool_t* pool, int num) {
SOKOL_ASSERT(pool && (num >= 1));
/* slot 0 is reserved for the 'invalid id', so bump the pool size by 1 */
@@ -2576,7 +2637,7 @@ static void _sgl_init_pipeline(sgl_pipeline pip_id, const sg_pipeline_desc* in_d
else {
pip->pip[i] = sg_make_pipeline(&desc);
if (pip->pip[i].id == SG_INVALID_ID) {
- SOKOL_LOG("sokol_gl.h: failed to create pipeline object");
+ SGL_LOG("sokol_gl.h: failed to create pipeline object");
pip->slot.state = SG_RESOURCESTATE_FAILED;
}
}
@@ -2590,7 +2651,7 @@ static sgl_pipeline _sgl_make_pipeline(const sg_pipeline_desc* desc, const sgl_c
_sgl_init_pipeline(pip_id, desc, ctx_desc);
}
else {
- SOKOL_LOG("sokol_gl.h: pipeline pool exhausted!");
+ SGL_LOG("sokol_gl.h: pipeline pool exhausted!");
}
return pip_id;
}
@@ -2717,7 +2778,7 @@ static sgl_context _sgl_make_context(const sgl_context_desc_t* desc) {
_sgl_init_context(ctx_id, desc);
}
else {
- SOKOL_LOG("sokol_gl.h: context pool exhausted!");
+ SGL_LOG("sokol_gl.h: context pool exhausted!");
}
return ctx_id;
}
@@ -3298,7 +3359,7 @@ SOKOL_API_IMPL sgl_context sgl_make_context(const sgl_context_desc_t* desc) {
SOKOL_API_IMPL void sgl_destroy_context(sgl_context ctx_id) {
SOKOL_ASSERT(_SGL_INIT_COOKIE == _sgl.init_cookie);
if (_sgl_is_default_context(ctx_id)) {
- SOKOL_LOG("sokol_gl.h: cannot destroy default context");
+ SGL_LOG("sokol_gl.h: cannot destroy default context");
return;
}
_sgl_destroy_context(ctx_id);