aboutsummaryrefslogtreecommitdiff
path: root/sokol_fetch.h
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2022-10-22 14:23:00 +0200
committerAndre Weissflog <floooh@gmail.com>2022-10-22 14:23:00 +0200
commite4b2aad7f27929b6ca58777c4f6884a23b1b0fbb (patch)
tree8717a18f409c9bdcc46a9b4d14a51d223143f472 /sokol_fetch.h
parent2057da7e15367ba9cf1deee82f8660a19aefe154 (diff)
parent3797da8c7f140f1406671a8ddcac1f7a2d4fc354 (diff)
Merge branch 'log-callback' of github.com:Manuzor/sokol into Manuzor-log-callback
Diffstat (limited to 'sokol_fetch.h')
-rw-r--r--sokol_fetch.h99
1 files changed, 81 insertions, 18 deletions
diff --git a/sokol_fetch.h b/sokol_fetch.h
index 07af0c27..0c2e12e6 100644
--- a/sokol_fetch.h
+++ b/sokol_fetch.h
@@ -16,7 +16,6 @@
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
- SOKOL_LOG(msg) - your own logging function (default: puts(msg))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_FETCH_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_FETCH_API_DECL
@@ -810,7 +809,7 @@
.allocator = {
.alloc = my_alloc,
.free = my_free,
- .user_data = ...;
+ .user_data = ...,
}
});
...
@@ -824,6 +823,28 @@
was called, so you don't need to worry about thread-safety.
+ LOG FUNCTION OVERRIDE
+ =====================
+ You can override the log function at initialization time like this:
+
+ void my_log(const char* message, void* user_data) {
+ printf("sfetch says: \s\n", message);
+ }
+
+ ...
+ sfetch_setup(&(sfetch_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.
+
+
FUTURE PLANS / V2.0 IDEA DUMP
=============================
- An optional polling API (as alternative to callback API)
@@ -903,6 +924,16 @@ typedef struct sfetch_allocator_t {
void* user_data;
} sfetch_allocator_t;
+/*
+ sfetch_logger_t
+
+ Used in sfetch_desc_t to provide custom log callbacks to sokol_fetch.h.
+ Default behavior is SFETCH_LOG_DEFAULT(message).
+*/
+typedef struct sfetch_logger_t {
+ void (*log_cb)(const char* message, void* user_data);
+ void* user_data;
+} sfetch_logger_t;
/* configuration values for sfetch_setup() */
typedef struct sfetch_desc_t {
@@ -910,6 +941,7 @@ typedef struct sfetch_desc_t {
uint32_t num_channels; /* number of channels to fetch requests in parallel (default: 1) */
uint32_t num_lanes; /* max number of requests active on the same channel (default: 1) */
sfetch_allocator_t allocator; /* optional memory allocation overrides (default: malloc/free) */
+ sfetch_logger_t logger; /* optional log function overrides (default: SFETCH_LOG_DEFAULT(message)) */
} sfetch_desc_t;
/* a request handle to identify an active fetch request, returned by sfetch_send() */
@@ -1035,13 +1067,33 @@ inline sfetch_handle_t sfetch_send(const sfetch_request_t& request) { return sfe
#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 sfetch_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 SFETCH_LOG(s) { SOKOL_ASSERT(s); _sfetch_log(s); }
+ #ifndef SFETCH_LOG_DEFAULT
+ #if defined(__ANDROID__)
+ #include <android/log.h>
+ #define SFETCH_LOG_DEFAULT(s) __android_log_write(ANDROID_LOG_INFO, "SOKOL_FETCH", s)
+ #else
+ #include <stdio.h>
+ #define SFETCH_LOG_DEFAULT(s) puts(s)
+ #endif
#endif
+#else
+ #define SFETCH_LOG(s)
+#endif
+#ifndef SFETCH_LOG_DEFAULT
+ #define SFETCH_LOG_DEFAULT(s)
#endif
#ifndef _SOKOL_PRIVATE
@@ -1288,6 +1340,17 @@ _SOKOL_PRIVATE void _sfetch_free(void* ptr) {
}
}
+#if !SOKOL_NO_LOG
+_SOKOL_PRIVATE void _sfetch_log(const char* msg) {
+ if (_sfetch->desc.logger.log_cb) {
+ _sfetch->desc.logger.log_cb(msg, _sfetch->desc.logger.user_data);
+ }
+ else {
+ SFETCH_LOG_DEFAULT(msg);
+ }
+}
+#endif
+
_SOKOL_PRIVATE _sfetch_t* _sfetch_ctx(void) {
return _sfetch;
}
@@ -1716,7 +1779,7 @@ _SOKOL_PRIVATE bool _sfetch_win32_utf8_to_wide(const char* src, wchar_t* dst, in
_SOKOL_PRIVATE _sfetch_file_handle_t _sfetch_file_open(const _sfetch_path_t* path) {
wchar_t w_path[SFETCH_MAX_PATH];
if (!_sfetch_win32_utf8_to_wide(path->buf, w_path, sizeof(w_path))) {
- SOKOL_LOG("_sfetch_file_open: error converting UTF-8 path to wide string");
+ SFETCH_LOG("_sfetch_file_open: error converting UTF-8 path to wide string");
return 0;
}
_sfetch_file_handle_t h = CreateFileW(
@@ -2234,7 +2297,7 @@ _SOKOL_PRIVATE bool _sfetch_channel_send(_sfetch_channel_t* chn, uint32_t slot_i
return true;
}
else {
- SOKOL_LOG("sfetch_send: user_sent queue is full)");
+ SFETCH_LOG("sfetch_send: user_sent queue is full)");
return false;
}
}
@@ -2379,35 +2442,35 @@ _SOKOL_PRIVATE void _sfetch_channel_dowork(_sfetch_channel_t* chn, _sfetch_pool_
_SOKOL_PRIVATE bool _sfetch_validate_request(_sfetch_t* ctx, const sfetch_request_t* req) {
#if defined(SOKOL_DEBUG)
if (req->channel >= ctx->desc.num_channels) {
- SOKOL_LOG("_sfetch_validate_request: request.channel too big!");
+ SFETCH_LOG("_sfetch_validate_request: request.channel too big!");
return false;
}
if (!req->path) {
- SOKOL_LOG("_sfetch_validate_request: request.path is null!");
+ SFETCH_LOG("_sfetch_validate_request: request.path is null!");
return false;
}
if (strlen(req->path) >= (SFETCH_MAX_PATH-1)) {
- SOKOL_LOG("_sfetch_validate_request: request.path is too long (must be < SFETCH_MAX_PATH-1)");
+ SFETCH_LOG("_sfetch_validate_request: request.path is too long (must be < SFETCH_MAX_PATH-1)");
return false;
}
if (!req->callback) {
- SOKOL_LOG("_sfetch_validate_request: request.callback missing");
+ SFETCH_LOG("_sfetch_validate_request: request.callback missing");
return false;
}
if (req->chunk_size > req->buffer_size) {
- SOKOL_LOG("_sfetch_validate_request: request.chunk_size is greater request.buffer_size)");
+ SFETCH_LOG("_sfetch_validate_request: request.chunk_size is greater request.buffer_size)");
return false;
}
if (req->user_data_ptr && (req->user_data_size == 0)) {
- SOKOL_LOG("_sfetch_validate_request: request.user_data_ptr is set, but request.user_data_size is null");
+ SFETCH_LOG("_sfetch_validate_request: request.user_data_ptr is set, but request.user_data_size is null");
return false;
}
if (!req->user_data_ptr && (req->user_data_size > 0)) {
- SOKOL_LOG("_sfetch_validate_request: request.user_data_ptr is null, but request.user_data_size is not");
+ SFETCH_LOG("_sfetch_validate_request: request.user_data_ptr is null, but request.user_data_size is not");
return false;
}
if (req->user_data_size > SFETCH_MAX_USERDATA_UINT64 * sizeof(uint64_t)) {
- SOKOL_LOG("_sfetch_validate_request: request.user_data_size is too big (see SFETCH_MAX_USERDATA_UINT64");
+ SFETCH_LOG("_sfetch_validate_request: request.user_data_size is too big (see SFETCH_MAX_USERDATA_UINT64");
return false;
}
#else
@@ -2443,7 +2506,7 @@ SOKOL_API_IMPL void sfetch_setup(const sfetch_desc_t* desc_) {
/* replace zero-init items with default values */
if (ctx->desc.num_channels > SFETCH_MAX_CHANNELS) {
ctx->desc.num_channels = SFETCH_MAX_CHANNELS;
- SOKOL_LOG("sfetch_setup: clamping num_channels to SFETCH_MAX_CHANNELS");
+ SFETCH_LOG("sfetch_setup: clamping num_channels to SFETCH_MAX_CHANNELS");
}
/* setup the global request item pool */
@@ -2515,7 +2578,7 @@ SOKOL_API_IMPL sfetch_handle_t sfetch_send(const sfetch_request_t* request) {
uint32_t slot_id = _sfetch_pool_item_alloc(&ctx->pool, request);
if (0 == slot_id) {
- SOKOL_LOG("sfetch_send: request pool exhausted (too many active requests)");
+ SFETCH_LOG("sfetch_send: request pool exhausted (too many active requests)");
return invalid_handle;
}
if (!_sfetch_channel_send(&ctx->chn[request->channel], slot_id)) {