aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2020-09-09 18:13:40 +0100
committerTomasz Kramkowski <tk@the-tk.com>2020-09-09 18:13:40 +0100
commit22ea2f04442442f9728825ad7888ca9c5250407e (patch)
tree84d91bb50599b538b3ead72337cbc39327e4a6b8
parent2e1ff80dab0cee0f197dccc9c5dd2f3b9afdb168 (diff)
downloadpack-22ea2f04442442f9728825ad7888ca9c5250407e.tar.gz
pack-22ea2f04442442f9728825ad7888ca9c5250407e.tar.xz
pack-22ea2f04442442f9728825ad7888ca9c5250407e.zip
Add tracing functions for debugging
-rw-r--r--Makefile2
-rw-r--r--pack.h7
-rw-r--r--trace.c41
-rw-r--r--trace.h7
4 files changed, 56 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 5d2ed5d..1d88ef2 100644
--- a/Makefile
+++ b/Makefile
@@ -1 +1 @@
-unpack_test: unpack_test.o unpack.o common.o
+unpack_test: unpack_test.o unpack.o common.o trace.o
diff --git a/pack.h b/pack.h
index 7a8fc69..ec8cf63 100644
--- a/pack.h
+++ b/pack.h
@@ -9,6 +9,13 @@ enum pack_status {
PACK_TOOSMALL,
};
+extern enum pack_trace {
+ PACK_TRACE_OFF,
+ PACK_TRACE_CALL,
+ PACK_TRACE_DEBUG,
+ PACK_TRACE_ALL,
+} pack_trace;
+
enum pack_status unpack(void *buf, size_t size, const char *fmt, ...);
const char *pack_strerror(enum pack_status status);
diff --git a/trace.c b/trace.c
new file mode 100644
index 0000000..456d277
--- /dev/null
+++ b/trace.c
@@ -0,0 +1,41 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "pack.h"
+#include "trace.h"
+
+enum pack_trace pack_trace = PACK_TRACE_OFF;
+
+void trace(enum pack_trace level, const char *fmt, va_list va)
+{
+ char *type;
+
+ if (level > pack_trace) return;
+
+ switch (level) {
+ case PACK_TRACE_CALL: type = "CALL"; break;
+ case PACK_TRACE_DEBUG: type = "DEBUG"; break;
+ }
+
+ fprintf(stderr, "PACK_TRACE_%s: ", type);
+ vfprintf(stderr, fmt, va);
+ fputc('\n', stderr);
+}
+
+void tr_call(const char *fmt, ...)
+{
+ va_list va;
+
+ va_start(va, fmt);
+ trace(PACK_TRACE_CALL, fmt, va);
+ va_end(va);
+}
+
+void tr_debug(const char *fmt, ...)
+{
+ va_list va;
+
+ va_start(va, fmt);
+ trace(PACK_TRACE_DEBUG, fmt, va);
+ va_end(va);
+}
diff --git a/trace.h b/trace.h
new file mode 100644
index 0000000..cf631cc
--- /dev/null
+++ b/trace.h
@@ -0,0 +1,7 @@
+#ifndef PACK_TRACE_H
+#define PACK_TRACE_H
+
+void tr_call(const char *fmt, ...);
+void tr_debug(const char *fmt, ...);
+
+#endif // !PACK_TRACE_H