]> git.itanic.dy.fi Git - rrdd/commitdiff
Make all debug output go through wrappers
authorTimo Kokkonen <kaapeli@ee.oulu.fi>
Sun, 6 Apr 2008 08:29:36 +0000 (11:29 +0300)
committerTimo Kokkonen <kaapeli@ee.oulu.fi>
Sun, 6 Apr 2008 08:29:36 +0000 (11:29 +0300)
Makefile
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]
main.c
parser.c
process.c

index dcaf6571820d465ad6de81e6c2a136d9388603b4..daadb937b39874e1614939903ffe223eaf72386d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,8 @@ CC=gcc
 LD=ld
 CFLAGS=-Wall -O2 -g
 
-RRDD_OBJS= main.o process.o draw_graphs.o parser.o scheduler.o string.o
+RRDD_OBJS= main.o process.o draw_graphs.o parser.o scheduler.o string.o \
+               debug.o
 
 rrdd: $(RRDD_OBJS)
        @echo -e "\tLD\t" rrdd
diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..67d126d
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,13 @@
+#include "debug.h"
+
+const char red_color[] = {
+       0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x31, 0x3b, 0x34, 0x30, 0x6d, 0x0,
+};
+
+const char green_color[] = {
+       0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x32, 0x3b, 0x34, 0x30, 0x6d, 0x0,
+};
+
+const char normal_color[] = {
+       0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x37, 0x3b, 0x34, 0x30, 0x6d, 0x0,
+};
diff --git a/debug.h b/debug.h
new file mode 100644 (file)
index 0000000..eff0993
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,18 @@
+#ifndef __DEBUG_H
+#define __DEBUG_H
+
+#include <unistd.h>
+
+extern const char red_color[];
+extern const char green_color[];
+extern const char normal_color[];
+
+#define pr_err(fmt, arg...) \
+       fprintf(stderr, "%s[%d] %s:%d Error %s" fmt, red_color, getpid(), \
+               __FILE__, __LINE__, normal_color, ##arg)
+
+#define pr_info(fmt, arg...) \
+       printf("%s[%d] %s:%d %s" fmt, green_color, getpid(), \
+              __FILE__, __LINE__, normal_color, ##arg)
+
+#endif
diff --git a/main.c b/main.c
index 91c10cfaa1fc751fcabf89d862e45150e8c23492..fae47eddbe23c24da5d3cf4e3132ffbe01c8a5e5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -5,6 +5,7 @@
 #include "draw_graphs.h"
 #include "parser.h"
 #include "scheduler.h"
+#include "debug.h"
 
 #include "testdata.h"
 
@@ -15,7 +16,7 @@ int main(int argc, char *argv[])
        int sleeptime;
 
        while (1) {
-               printf("%d: loop start\n", getpid());
+               pr_info("loop start\n");
                /*
                 * Update all databases parallel in one shot
                 */
@@ -30,7 +31,7 @@ int main(int argc, char *argv[])
                        continue;
 
                sleeptime = get_next_update((struct rrd_database **)&all_rrds);
-               printf("Time to sleep %d seconds\n", sleeptime);
+               pr_info("Time to sleep %d seconds\n", sleeptime);
                sleep(sleeptime);
 
        }
index 02f7afd021ce9a70368f82d2fb3850549e49a00c..9476494f9d120493c39d52ae97065a6ac181a419 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -6,6 +6,7 @@
 #include "parser.h"
 #include "process.h"
 #include "string.h"
+#include "debug.h"
 
 #define STATFILE "/proc/stat"
 
@@ -17,12 +18,12 @@ int cpu_parser(char *data)
        int user, nice, sys, idle, wait, irq, softirq;
 
        if (file == NULL) {
-               fprintf(stderr, "Failed to open file %s\n", STATFILE);
+               pr_err("Failed to open file %s\n", STATFILE);
                return -1;
        }
 
        if (!fgets(buf, 1024, file)) {
-               fprintf(stderr, "Failed to read file %s\n", STATFILE);
+               pr_err("Failed to read file %s\n", STATFILE);
                fclose(file);
                return -1;
        }
@@ -52,7 +53,7 @@ int mem_parser(char *data)
        FILE *file = fopen(MEMFILE, "r");
 
        if (file == NULL) {
-               fprintf(stderr, "Failed to open file %s\n", MEMFILE);
+               pr_err("Failed to open file %s\n", MEMFILE);
                return -1;
        }
 
@@ -117,7 +118,7 @@ int digitemp_parser(char *data)
 
        pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
        if (pid < 0) {
-               fprintf(stderr, "Failed to parse digitemp");
+               pr_err("Failed to parse digitemp");
                sprintf(data, "U:U");
                return -1;
        }
index 0e5c1c2ce24a7c3d01604b6b3bcf2c3f601c5c63..950a267d0060d61fdd1d03b4ebd70d2fa2ba9177 100644 (file)
--- a/process.c
+++ b/process.c
@@ -1,4 +1,5 @@
 #include "process.h"
+#include "debug.h"
 
 int child_count = 0;
 
@@ -8,12 +9,12 @@ int do_fork(void)
        child = fork();
        if (child < 0) {
                error = errno;
-               fprintf(stderr, "fork() failed: %s\n", strerror(error));
+               pr_err("fork() failed: %s\n", strerror(error));
                return -1;
        }
 
        if (child) {
-               printf("%d: Forked child %d\n", getpid(), child);
+               pr_info("Forked child %d\n", child);
                child_count++;
                return child;
        }
@@ -31,7 +32,7 @@ int run(const char *cmd, char *const argv[])
 
        execvp(cmd, argv);
        error = errno;
-       printf("Failed to execv command %s: %s\n", cmd, strerror(error));
+       pr_err("Failed to execv command %s: %s\n", cmd, strerror(error));
        exit(1);
        return 0;
 }
@@ -46,12 +47,11 @@ int harvest_zombies(int pid)
        pid = waitpid(pid, &status, 0);
        if (pid < 0) {
                error = errno;
-               fprintf(stderr, "%d: Error on wait(): %s\n", getpid(),
-                       strerror(error));
+               pr_err("Error on wait(): %s\n", strerror(error));
        }
        else
-               printf("pid %d: exit %d. %d still running\n", pid, status,
-                      child_count--);
+               pr_info("pid %d: exit code %d. Children left: %d\n", pid,
+                       status, --child_count);
 
        return 1;
 }
@@ -68,13 +68,13 @@ int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
 
        if (pipe(rfd)) {
                error = errno;
-               fprintf(stderr, "pipe() failed: %s\n", strerror(error));
+               pr_err("pipe() failed: %s\n", strerror(error));
                return -1;
        }
 
        if (pipe(wfd)) {
                error = errno;
-               fprintf(stderr, "pipe() failed: %s\n", strerror(error));
+               pr_err("pipe() failed: %s\n", strerror(error));
                return -1;
        }
 
@@ -96,7 +96,7 @@ int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
        /* Now we have redirected both stdin and stdout to parent process */
        execvp(cmd, argv);
        error = errno;
-       printf("Failed to execv command %s: %s\n", cmd, strerror(error));
+       pr_err("Failed to execv command %s: %s\n", cmd, strerror(error));
        exit(1);
        return 0;
 }
@@ -118,9 +118,8 @@ int run_piped_stream(const char *cmd, char *const argv[],
                *readf = fdopen(rfd, "r");
                if (*readf == NULL) {
                        error = errno;
-                       fprintf(stderr,
-                               "Error opening file stream for fd %d: %s\n",
-                               rfd, strerror(error));
+                       pr_err("Error opening file stream for fd %d: %s\n",
+                              rfd, strerror(error));
                        return -1;
                }
        }
@@ -129,9 +128,8 @@ int run_piped_stream(const char *cmd, char *const argv[],
                *writef = fdopen(wfd, "w");
                if (*writef == NULL) {
                        error = errno;
-                       fprintf(stderr,
-                               "Error opening file stream for fd %d: %s\n",
-                               wfd, strerror(error));
+                       pr_err("Error opening file stream for fd %d: %s\n",
+                              wfd, strerror(error));
                        return -1;
                }
        }