From 3ab946ffad95405971834f5c50784d8e9dfabd6a Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sat, 11 Nov 2023 17:20:20 -0500 Subject: Make log globals thread-local, add array reference Signed-off-by: Andrew Opalach --- src/array.c | 1 + src/log.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/array.c b/src/array.c index c843ff6..e95b1a9 100644 --- a/src/array.c +++ b/src/array.c @@ -2,6 +2,7 @@ #include "../include/al/array_sort.h" // https://github.com/lifthrasiir/angolmois/blob/master/angolmois.c#L79 +// https://gist.github.com/lifthrasiir/4422136 #define array(type) \ struct { \ diff --git a/src/log.c b/src/log.c index ca9266d..9a738df 100644 --- a/src/log.c +++ b/src/log.c @@ -4,8 +4,7 @@ #define AL_LOG_MESSAGE_SIZE 511 #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) " -static void *al_log_userdata = NULL; - +#if !AL_LOG_SKIP static s32 al_print_default(void *userdata, char *s) { (void)userdata; @@ -14,15 +13,15 @@ static s32 al_print_default(void *userdata, char *s) return ret; } -static s32 (*_al_print)(void *, char *) = al_print_default; +__al_thread s32 (*_al_print)(void *, char *) = al_print_default; +__al_thread void *_al_log_userdata = NULL; -void al_set_print(void *userdata, s32 (*print_func)(void *, char *)) +void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { - al_log_userdata = userdata; _al_print = print_func; + _al_log_userdata = userdata; } -#if !AL_LOG_SKIP static char *get_buffer(const char *level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args) { @@ -33,6 +32,8 @@ static char *get_buffer(const char *level, const char *section, const char *fmt, buffer[strcspn(buffer, "\r\n")] = '\0'; return buffer; } +#else +void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { (void)print_func; (void)userdata; } #endif s32 _al_log_nop(const char *section, const char *fmt, ...) @@ -62,7 +63,7 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 #if AL_LOG_SKIP return ret; #else - return _al_print(al_log_userdata, buffer); + return _al_print(_al_log_userdata, buffer); #endif #endif } @@ -84,7 +85,7 @@ s32 _al_logv(const char *level, const char *section, const char *name, const s32 #if AL_LOG_SKIP return ret; #else - return _al_print(al_log_userdata, buffer); + return _al_print(_al_log_userdata, buffer); #endif #endif } -- cgit v1.2.3-101-g0448