]> git.itanic.dy.fi Git - rrdd/blobdiff - debug.c
onewire_parser.c: Fix compiler warnings about string lengths
[rrdd] / debug.c
diff --git a/debug.c b/debug.c
index 6c7042eda7af35cb91e2c0dd3c321afca9c08c7b..201f7ef2d1a233cdaa70e877bee2fdaf19448224 100644 (file)
--- a/debug.c
+++ b/debug.c
@@ -1,8 +1,12 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <time.h>
 #include <unistd.h>
 #include <stdarg.h>
-#include "process.h"
+#include <pthread.h>
 
+#include "process.h"
 #include "debug.h"
 
 const char red_color[] = {
@@ -17,7 +21,8 @@ const char normal_color[] = {
        0x1b, 0x5b, 0x30, 0x3b, 0x33, 0x37, 0x3b, 0x34, 0x30, 0x6d, 0x0,
 };
 
-int trace_level = 1;
+int trace_level = TRACE_INFO;
+static int logfile_fd = STDERR_FILENO;
 
 static const char *assign_color(int color)
 {
@@ -27,7 +32,7 @@ static const char *assign_color(int color)
        case 2:
                return green_color;
        default:
-               return normal_color;
+       return normal_color;
        }
 }
 
@@ -38,7 +43,10 @@ void print_trace(const char *file, int line, int color, int l,
        time_t t = time(0);
        char time[32];
        char trace[1024];
+       char all_trace[1538];
+       char thread_name[16];
        const char *color_str = assign_color(color);
+       int ret;
 
        if (l > trace_level)
                return;
@@ -49,9 +57,38 @@ void print_trace(const char *file, int line, int color, int l,
 
        strftime(time, sizeof(time), "%d.%m.%Y %T", localtime(&t));
 
-       fprintf(stderr, "%s%s [%5d.%d] %s:%d %s %s",
+       pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
+
+       ret = snprintf(all_trace, sizeof(all_trace),
+               "%s%s [%s] %s:%d %s %s",
                color_str, time,
-               getpid(), get_parent_count(),
+               thread_name,
                file, line, normal_color, trace);
-       fflush(stderr);
+
+       ret = write(logfile_fd, all_trace, ret);
+       /*
+        * If write failed, well, not much point in trying to print a
+        * trace about it.. So we just ignore the return value
+        */
+}
+
+int open_log_file(const char *logfile)
+{
+       int fd;
+
+       pr_info("Switching logging to file: %s\n", logfile);
+
+       fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
+       if (fd < 0) {
+               pr_err("Failed to open logfile %s: %m\n", logfile);
+               return fd;
+       }
+
+       if (logfile_fd != STDERR_FILENO)
+               close(logfile_fd);
+       logfile_fd = fd;
+
+       pr_info("Opened file %s for logging\n", logfile);
+
+       return 0;
 }