]> git.itanic.dy.fi Git - rrdd/blob - debug.c
debug: Rework debugging
[rrdd] / debug.c
1 #include <time.h>
2 #include <unistd.h>
3 #include <stdarg.h>
4 #include "process.h"
5
6 #include "debug.h"
7
8 const char red_color[] = {
9         0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x31, 0x3b, 0x34, 0x30, 0x6d, 0x0,
10 };
11
12 const char green_color[] = {
13         0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x32, 0x3b, 0x34, 0x30, 0x6d, 0x0,
14 };
15
16 const char normal_color[] = {
17         0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x37, 0x3b, 0x34, 0x30, 0x6d, 0x0,
18 };
19
20 int trace_level = 1;
21
22 void print_trace(const char *file, int line, int color, int l,
23                 const char *fmt, ...)
24 {
25         va_list args;
26         time_t t = time(0);
27         char indent[16] = { "               " };
28         char time[32];
29         char trace[1024];
30
31         if (l > trace_level)
32                 return;
33
34         va_start(args, fmt);
35         vsnprintf(trace, sizeof(trace), fmt, args);
36         va_end(args);
37
38         strftime(time, sizeof(time), "%d.%m.%Y %T ", localtime(&t));
39         indent[get_parent_count()] = 0;
40
41         fprintf(stderr, "%s%s %s[%5d.%d] %s:%d %s %s",
42                 green_color, time, indent,
43                 getpid(), get_parent_count(),
44                 file, line, normal_color, trace);
45         fflush(stderr);
46 }