summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-01-16 18:41:39 -0300
committerEuAndreh <eu@euandre.org>2025-01-16 18:41:39 -0300
commitc341b2bdb0259ea00c08a33a0e1d71d678039725 (patch)
treeacae3853f8a2bacf2de53783ca1d59faa19c602d /src
parentMakefile: keep symlinks in "install" target (diff)
downloadpindaiba-c341b2bdb0259ea00c08a33a0e1d71d678039725.tar.gz
pindaiba-c341b2bdb0259ea00c08a33a0e1d71d678039725.tar.xz
src/trace.c: Replace thread_local with pthreads
Diffstat (limited to 'src')
-rw-r--r--src/trace.c41
-rw-r--r--src/trace.h4
2 files changed, 29 insertions, 16 deletions
diff --git a/src/trace.c b/src/trace.c
index 335ee43..d21a792 100644
--- a/src/trace.c
+++ b/src/trace.c
@@ -2,25 +2,36 @@
#include <assert.h>
#include <errno.h>
+#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <threads.h>
#include "logerr.h"
#include "trace.h"
-static thread_local enum TraceLevel
-LEVEL = TraceLevel_INFO;
+static pthread_key_t
+LEVEL_KEY;
-static thread_local FILE *
-STREAM = NULL;
+static pthread_key_t
+STREAM_KEY;
+static pthread_once_t
+ONCE = PTHREAD_ONCE_INIT;
+
+static void
+init(void) {
+ assert(pthread_key_create(&LEVEL_KEY, NULL) == 0);
+ assert(pthread_key_create(&STREAM_KEY, NULL) == 0);
+ assert(pthread_setspecific(LEVEL_KEY, (void *)TraceLevel_INFO) == 0);
+ assert(pthread_setspecific(LEVEL_KEY, stderr) == 0);
+}
+
void
vftracef(
const char *const file,
@@ -31,7 +42,10 @@ vftracef(
const char *restrict format,
va_list args
) {
- if (level > LEVEL) {
+ assert(pthread_once(&ONCE, init) == 0);
+
+ const enum TraceLevel current_level = (enum TraceLevel)pthread_getspecific(LEVEL_KEY);
+ if (level > current_level) {
return;
}
@@ -73,23 +87,22 @@ tracef(
const char *restrict format,
...
) {
- if (STREAM == NULL) {
- STREAM = stderr;
- }
+ assert(pthread_once(&ONCE, init) == 0);
+
va_list args;
va_start(args, format);
- vftracef(file, function, lineno, STREAM, level, format, args);
+ vftracef(file, function, lineno, pthread_getspecific(STREAM_KEY), level, format, args);
va_end(args);
}
-void
+int
trace_set_stream(FILE *stream) {
- STREAM = stream;
+ return pthread_setspecific(STREAM_KEY, stream);
}
-void
+int
trace_set_level(const enum TraceLevel level) {
assert(level >= TraceLevel_NONE);
assert(level <= TraceLevel_DEBUG);
- LEVEL = level;
+ return pthread_setspecific(LEVEL_KEY, (void *)level);
}
diff --git a/src/trace.h b/src/trace.h
index b81e616..be3f6ee 100644
--- a/src/trace.h
+++ b/src/trace.h
@@ -48,8 +48,8 @@ tracef(
...
);
-void
+int
trace_set_stream(FILE *stream);
-void
+int
trace_set_level(const enum TraceLevel level);