vss: Use the stripped header also at stream start.
[paraslash.git] / daemon.c
index 011ece6d3d7689b34e793fb4b00f813afc324fc2..b7a0a3267b68df5b020159f90c56337073463244 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1,16 +1,20 @@
 /*
- * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file daemon.c Some helpers for programs that detach from the console. */
-#include "para.h"
-#include "daemon.h"
+
+#include <regex.h>
 #include <pwd.h>
 #include <sys/types.h> /* getgrnam() */
 #include <grp.h>
+#include <sys/time.h>
+#include <stdbool.h>
 
+#include "para.h"
+#include "daemon.h"
 #include "string.h"
 #include "color.h"
 
@@ -52,10 +56,8 @@ void daemon_set_default_log_colors(void)
                [LL_CRIT] = "magenta bold",
                [LL_EMERG] = "red bold",
        };
-       for (i = 0; i < NUM_LOGLEVELS; i++) {
-               int ret = color_parse(default_log_colors[i], me->log_colors[i]);
-               assert(ret >= 0);
-       }
+       for (i = 0; i < NUM_LOGLEVELS; i++)
+               color_parse_or_die(default_log_colors[i], me->log_colors[i]);
 }
 
 /**
@@ -67,7 +69,7 @@ void daemon_set_default_log_colors(void)
  *
  * \return 1 On success, -1 on errors.
  */
-int daemon_set_log_color(char const *arg)
+void daemon_set_log_color_or_die(char const *arg)
 {
        char *p = strchr(arg, ':');
        int ret, ll;
@@ -79,14 +81,11 @@ int daemon_set_log_color(char const *arg)
                goto err;
        ll = ret;
        p++;
-       ret = color_parse(p, me->log_colors[ll]);
-       if (ret < 0)
-               goto err;
-       return 1;
+       color_parse_or_die(p, me->log_colors[ll]);
+       return;
 err:
-       PARA_ERROR_LOG("%s: color syntax error\n", arg);
-       return -1;
-
+       PARA_EMERG_LOG("%s: color syntax error\n", arg);
+       exit(EXIT_FAILURE);
 }
 
 /**
@@ -139,7 +138,7 @@ void daemon_clear_flag(unsigned flag)
        me->flags &= ~flag;
 }
 
-static unsigned daemon_test_flag(unsigned flag)
+static bool daemon_test_flag(unsigned flag)
 {
        return me->flags & flag;
 }
@@ -330,8 +329,9 @@ __printf_2_3 void para_log(int ll, const char* fmt,...)
        va_list argp;
        FILE *fp;
        struct tm *tm;
-       time_t t1;
        char *color;
+       bool log_time = daemon_test_flag(DF_LOG_TIME), log_timing =
+               daemon_test_flag(DF_LOG_TIMING);
 
        ll = PARA_MIN(ll, NUM_LOGLEVELS - 1);
        ll = PARA_MAX(ll, LL_DEBUG);
@@ -342,12 +342,18 @@ __printf_2_3 void para_log(int ll, const char* fmt,...)
        color = daemon_test_flag(DF_COLOR_LOG)? me->log_colors[ll] : NULL;
        if (color)
                fprintf(fp, "%s", color);
-       if (daemon_test_flag(DF_LOG_TIME)) { /* print date and time */
-               char str[100];
-               time(&t1);
-               tm = localtime(&t1);
-               strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
-               fprintf(fp, "%s ", str);
+       if (log_time || log_timing) {
+               struct timeval tv;
+               gettimeofday(&tv, NULL);
+               if (daemon_test_flag(DF_LOG_TIME)) { /* print date and time */
+                       time_t t1 = tv.tv_sec;
+                       char str[100];
+                       tm = localtime(&t1);
+                       strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
+                       fprintf(fp, "%s%s", str, log_timing? ":" : " ");
+               }
+               if (log_timing) /* print milliseconds */
+                       fprintf(fp, "%04lu ", (long unsigned)tv.tv_usec / 1000);
        }
        if (daemon_test_flag(DF_LOG_HOSTNAME)) {
                if (!me->hostname)