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
--- /dev/null
+#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,
+};
--- /dev/null
+#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
#include "draw_graphs.h"
#include "parser.h"
#include "scheduler.h"
+#include "debug.h"
#include "testdata.h"
int sleeptime;
while (1) {
- printf("%d: loop start\n", getpid());
+ pr_info("loop start\n");
/*
* Update all databases parallel in one shot
*/
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);
}
#include "parser.h"
#include "process.h"
#include "string.h"
+#include "debug.h"
#define STATFILE "/proc/stat"
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;
}
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;
}
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;
}
#include "process.h"
+#include "debug.h"
int child_count = 0;
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;
}
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;
}
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;
}
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;
}
/* 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;
}
*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;
}
}
*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;
}
}