summaryrefslogtreecommitdiff
path: root/src/trace.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace.c')
-rw-r--r--src/trace.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/trace.c b/src/trace.c
new file mode 100644
index 0000000..b37da2f
--- /dev/null
+++ b/src/trace.c
@@ -0,0 +1,73 @@
+#include "config.h"
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+
+#include "trace.h"
+
+
+
+thread_local enum TraceLevel
+TRACE_LEVEL = TraceLevel_INFO;
+
+thread_local FILE *
+TRACE_OUTPUT = NULL;
+
+
+
+void
+set_level(const enum TraceLevel level) {
+ assert(level >= TraceLevel_NONE);
+ assert(level <= TraceLevel_ERROR);
+ TRACE_LEVEL = level;
+}
+
+void
+set_output(FILE *stream) {
+ TRACE_OUTPUT = stream;
+}
+
+int
+vtrace(
+ const char *const file,
+ const char *const function,
+ const int lineno,
+ const enum TraceLevel level,
+ const char *restrict format,
+ ...
+) {
+ int rc = 0;
+
+ // FIXME: test
+ if (level < TRACE_LEVEL) {
+ return rc;
+ }
+
+ if (TRACE_OUTPUT == NULL) {
+ TRACE_OUTPUT = stderr;
+ }
+
+ // FIXME: weavify!
+ if (fprintf(TRACE_OUTPUT, "%s:%s:%d: ", file, function, lineno) < 0) {
+ perror(__FILE__ ":vtrace(): fprintf() < 0");
+ rc = -1;
+ }
+
+ va_list args;
+ va_start(args, format);
+ if (vfprintf(TRACE_OUTPUT, format, args) < 0) {
+ perror(__FILE__ ":vtrace(): vfprintf() < 0");
+ rc = -1;
+ }
+ va_end(args);
+
+ if (fprintf(TRACE_OUTPUT, "\n") < 0) {
+ perror(__FILE__ ":vtrace(): fprintf() < 0");
+ rc = -1;
+ }
+
+ return rc;
+}