]> git.itanic.dy.fi Git - log-plotter/commitdiff
Add more advanced debug macro
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 6 Oct 2013 14:57:09 +0000 (17:57 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 6 Oct 2013 15:24:24 +0000 (18:24 +0300)
Make it possible to have multiple debug levels. This makes it possible
to remove unneeded (debug) output when user is not interested in
seeing the output.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
Makefile
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]

index e02d333be42a162c531835aacd3296b72fa7792e..9ccc202bf760f9f802e4c6bd47fdc9c2703a1d33 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC=gcc
 LD=ld
 CFLAGS=-Wall -O2 -g -fPIC
 
-LOG-PLOTTER_OBJS = baud.o main.o options.o
+LOG-PLOTTER_OBJS = baud.o main.o options.o debug.o
 
 ALL_OBJS = $(LOG-PLOTTER_OBJS)
 ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..dc89f6d
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "debug.h"
+
+int trace_level = TRACE_INFO;
+
+void print_trace(const char *file, int line, int l,
+               const char *fmt, ...)
+{
+       FILE *out_file;
+        va_list args;
+
+       if (l > trace_level)
+               return;
+
+       if (l == TRACE_ERR)
+               out_file = stderr;
+       else
+               out_file = stdout;
+
+        va_start(args, fmt);
+       vfprintf(out_file, fmt, args);
+        va_end(args);
+}
diff --git a/debug.h b/debug.h
new file mode 100644 (file)
index 0000000..1b327e0
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,23 @@
+#ifndef __DEBUG_H
+#define __DEBUG_H
+
+extern int trace_level;
+
+enum {
+       TRACE_ERR = 1,
+       TRACE_WARN,
+       TRACE_INFO,
+       TRACE_DEBUG,
+       TRACE_MAX,
+};
+
+void  __attribute__ ((__format__ (__printf__, 4, 5)))
+print_trace(const char *file, int line, int l,
+               const char *fmt, ...);
+
+#define pr_err(arg...)   print_trace(__FILE__, __LINE__, TRACE_ERR, arg)
+#define pr_warn(arg...)  print_trace(__FILE__, __LINE__, TRACE_WARN, arg)
+#define pr_info(arg...)  print_trace(__FILE__, __LINE__, TRACE_INFO, arg)
+#define pr_debug(arg...) print_trace(__FILE__, __LINE__, TRACE_DEBUG, arg)
+
+#endif