From 6c20909213a182d68457ef0d90245fbfaf2a65f5 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 6 Oct 2013 17:57:09 +0300 Subject: [PATCH] Add more advanced debug macro 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 --- Makefile | 2 +- debug.c | 25 +++++++++++++++++++++++++ debug.h | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 debug.c create mode 100644 debug.h diff --git a/Makefile b/Makefile index e02d333..9ccc202 100644 --- 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 index 0000000..dc89f6d --- /dev/null +++ b/debug.c @@ -0,0 +1,25 @@ +#include +#include + +#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 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 -- 2.45.0