]> git.itanic.dy.fi Git - rrdd/commitdiff
debug: Rework debugging
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 24 Jun 2012 19:59:23 +0000 (22:59 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 24 Jun 2012 19:59:23 +0000 (22:59 +0300)
Instead of having a huge debug macro that gets expanded every time a
debug print is needed, separate the debugging facilities into
debu.c. This will reduce excess code duplication a lot.

A concept of debugging level is also introduced. This makes it
possible to add debug prints with different priority level and have
some of the prints to be silenced when user is not interested about
them.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
debug.c
debug.h

diff --git a/debug.c b/debug.c
index 67d126d106acfbd67f2e919712299e1c8702322f..c139080a18fa743d9563334ec7435b0f8d7cfc41 100644 (file)
--- a/debug.c
+++ b/debug.c
@@ -1,3 +1,8 @@
+#include <time.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include "process.h"
+
 #include "debug.h"
 
 const char red_color[] = {
@@ -11,3 +16,31 @@ const char green_color[] = {
 const char normal_color[] = {
        0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x37, 0x3b, 0x34, 0x30, 0x6d, 0x0,
 };
+
+int trace_level = 1;
+
+void print_trace(const char *file, int line, int color, int l,
+               const char *fmt, ...)
+{
+        va_list args;
+       time_t t = time(0);
+       char indent[16] = { "               " };
+       char time[32];
+       char trace[1024];
+
+       if (l > trace_level)
+               return;
+
+        va_start(args, fmt);
+       vsnprintf(trace, sizeof(trace), fmt, args);
+        va_end(args);
+
+       strftime(time, sizeof(time), "%d.%m.%Y %T ", localtime(&t));
+       indent[get_parent_count()] = 0;
+
+       fprintf(stderr, "%s%s %s[%5d.%d] %s:%d %s %s",
+               green_color, time, indent,
+               getpid(), get_parent_count(),
+               file, line, normal_color, trace);
+       fflush(stderr);
+}
diff --git a/debug.h b/debug.h
index 3507c90d2ec8f77de800e84f11a041d09bcb661b..54e81a2cc0fb715fa534191e2085cc8cb81b0413 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -1,47 +1,15 @@
 #ifndef __DEBUG_H
 #define __DEBUG_H
 
-#include <time.h>
-#include <unistd.h>
-#include "process.h"
-
 extern const char red_color[];
 extern const char green_color[];
 extern const char normal_color[];
 
-#define pr_info(fmt, arg...)                                           \
-       do {                                                            \
-               time_t t = time(0);                                     \
-               char indent[16] = { "               " };                \
-               char time[32];                                          \
-                                                                       \
-               strftime(time, sizeof(time), "%d.%m.%Y %T ",            \
-                       localtime(&t));                                 \
-               indent[get_parent_count()] = 0;                         \
-                                                                       \
-               fprintf(stderr, "%s%s %s[%5d.%d] %s:%d %s"              \
-                       fmt, green_color, time, indent,                 \
-                       getpid(), get_parent_count(),                   \
-                       __FILE__, __LINE__, normal_color, ##arg);       \
-               fflush(stderr);                                         \
-       } while (0)
-
+void  __attribute__ ((__format__ (__printf__, 5, 6)))
+print_trace(const char *file, int line, int color, int l,
+               const char *fmt, ...);
 
-#define pr_err(fmt, arg...)                                            \
-       do {                                                            \
-               time_t t = time(0);                                     \
-               char indent[16] = { "               " };                \
-               char time[32];                                          \
-                                                                       \
-               strftime(time, sizeof(time), "%d.%m.%Y %T ",            \
-                       localtime(&t));                                 \
-               indent[get_parent_count()] = 0;                         \
-                                                                       \
-               fprintf(stderr, "%s%s %s[%5d.%d] %s:%d Error %s"        \
-                       fmt, red_color, time, indent,                   \
-                       getpid(), get_parent_count(),                   \
-                       __FILE__, __LINE__, normal_color, ##arg);       \
-               fflush(stderr);                                         \
-       } while (0)
+#define pr_err(arg...)  print_trace(__FILE__, __LINE__, 1, 0, arg)
+#define pr_info(arg...) print_trace(__FILE__, __LINE__, 2, 1, arg)
 
 #endif