chiark / gitweb /
Improve server logging.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 20 Jul 2014 13:52:26 +0000 (14:52 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 20 Jul 2014 13:57:05 +0000 (14:57 +0100)
  * The client can now log to stdout or stderr easily.  Annoyingly,
    `-f/dev/stderr' doesn't work if running privilege-separated, so
    provide some handy magic tokens.

  * The init scripts now have configuration for writing to syslog.

client/tripectl.1.in
client/tripectl.c
contrib/tripe-upstart.in
init/tripe-init.in
init/tripe.conf

index 4c6777e18bf5db104e081506056dedf4ea58952b..33e56cc9ba5add643c8af6d98a8250aa40ed5be0 100644 (file)
@@ -44,7 +44,7 @@ tripectl \- simple client for TrIPE
 .RI [ args ]...]
 .br
 .B tripectl
-.RB [ \-Dl ]
+.RB [ \-Dlt ]
 .RB [ \-f
 .IR file ]
 .RB [ \-\fIoptions ]
@@ -223,12 +223,26 @@ trace messages are logged with severity
 .BI "\-f, \-\-logfile=" file
 Write warnings and trace messages to
 .IR file .
-On receipt of a
+The
+.I file
+may be
+.RB ` \- '
+to request output to stdout, or
+.RB ` ! '
+to request output to stderr.  If a proper filename is given (rather than
+one of these special tokens), then on receipt of a
 .B SIGHUP
 signal,
 .B tripectl
-will close its log file and reopen a new one with the same name.  This
-is useful when you rotate logs.
+will close its log file and reopen a new one with the same name; this is
+useful when you rotate logs.
+.TP
+.B "\-t, \-\-no-timestamp"
+When logging to a file (with
+.BR \-f ),
+don't prefix log items with a timestamp.  This is useful when the log
+output is being captured by some process which will add its own
+timestamps anyway.
 .TP
 .B "\-w, \-\-warnings"
 Write warnings to standard error even when running noninteractively.
index 95f95f44ba5ba07c5e95375200a4322056764f43..f14c137e44283d707f8fb420e9f5d66f299ed2a2 100644 (file)
@@ -94,6 +94,7 @@ static const char *bgtag = 0;
 #define f_warn 128u
 #define f_uclose 256u
 #define f_losing 512u
+#define f_nostamp 1024u
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -110,8 +111,9 @@ static void writelog(const char *cat, const char *msg)
   char buf[256];
   time_t t = time(0);
   struct tm *tm = localtime(&t);
-  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
-  fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
+  if (f & f_nostamp) buf[0] = 0;
+  else strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", tm);
+  fprintf(logfp, "%s%s: %s\n", buf, cat, msg);
 }
 
 static void checkbg(char **p)
@@ -274,7 +276,11 @@ static void logfile(const char *name)
 {
   FILE *fp;
 
-  if ((fp = fopen(name, "a")) != 0) {
+  if (strcmp(name, "-") == 0)
+    logfp = stdout;
+  else if (strcmp(name, "!") == 0)
+    logfp = stderr;
+  else if ((fp = fopen(name, "a")) != 0) {
     if (logfp)
       fclose(logfp);
     logfp =  fp;
@@ -354,6 +360,7 @@ Options in full:\n\
 \n\
 -l, --syslog           Log messages to system log.\n\
 -f, --logfile=FILE     Log messages to FILE.\n\
+-t, --no-timestamp     When logging to a file, don't emit timestamps.\n\
 -w, --warnings         Show warnings when running commands.\n\
 ", fp);
 }
@@ -406,12 +413,13 @@ int main(int argc, char *argv[])
       { "spawn-args",  OPTF_ARGREQ,    0,      'S' },
       { "syslog",      0,              0,      'l' },
       { "logfile",     OPTF_ARGREQ,    0,      'f' },
+      { "no-timestamp",        0,              0,      't' },
       { "warnings",    0,              0,      'w' },
       { "pidfile",     OPTF_ARGREQ,    0,      'P' },
       { 0,             0,              0,      0 }
     };
 
-    i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts, 0, 0, 0);
+    i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:t", opts, 0, 0, 0);
     if (i < 0)
       break;
     switch (i) {
@@ -461,6 +469,9 @@ int main(int argc, char *argv[])
        logname = optarg;
        f |= f_noinput;
        break;
+      case 't':
+       f |= f_nostamp;
+       break;
       case 'P':
        pidfile = optarg;
        break;
index 42d15de01cbfe9a5851ae120d39df6f9f4a9ebc4..8d558f0175db44747aed8b979c1384abed07d51c 100644 (file)
@@ -143,9 +143,15 @@ script
        export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
 
        ## Start the server, passing lots of arguments.
+       logopt=
+       case ${syslogp-nil} in nil) ;; *) logopt="$logopt -l" ;; esac
+       case ${logfile+t},${syslogp-nil} in
+         t,*) logopt="$logopt -f$logfile" ;;
+         ,nil) logopt="$logopt -f@logfile@" ;;
+       esac
        $tripectl -s -p$tripe \
          -P$pidfile \
-         -f${logfile-@logfile@} \
+         $logopt \
          ${keytag+-S-t$keytag} \
          ${addr+-S-b$addr} ${port+-S-p$port} \
          ${user+-U$user} ${group+-G$group} \
index 7a21c4ad605df72b6d9c0854f500be88d4250397..c146bdf9646ea57a8e458c0ddc35f262ad3c8421 100755 (executable)
@@ -133,9 +133,15 @@ case "$1" in
     fi
 
     ## Start the server, passing lots of arguments.
+    logopt=
+    case ${syslogp-nil} in nil) ;; *) logopt="$logopt -l" ;; esac
+    case ${logfile+t},${syslogp-nil} in
+      t,*) logopt="$logopt -f$logfile" ;;
+      ,nil) logopt="$logopt -f@logfile@" ;;
+    esac
     $tripectl -D -s -p$tripe \
       -P$pidfile \
-      -f${logfile-@logfile@} \
+      $logopt \
       ${keytag+-S-t$keytag} \
       ${addr+-S-b$addr} ${port+-S-p$port} \
       ${user+-U$user} ${group+-G$group} \
index 2b4a5a3e8abedc21c152a47a7e827714e1ff8ae9..743c17a61e0e060c38efa19b8beebb0f987e583e 100644 (file)
 #miscopts=
 
 ## Logfile to write to.  The default is determined by the `--with-logfile'
-## configure option, which defaults to `./tripe.log' (relative to $TRIPEDIR).
+## configure option, which defaults to `./tripe.log' (relative to
+## $TRIPEDIR).  This may be set to `-' to write the log to stdout, or `!' to
+## write to stderr.
 ##
 #logfile=/var/log/tripe
 
+## Whether to write a log to syslog.  If this is something other than `nil',
+## then logs are written to syslog.  If `syslogp' is non-nil and `logfile'
+## above is unset then no logs are written to files.
+#syslogp=nil
+
 ## Where to put tripectl's pidfile when it starts up.  The default is
 ## determined by the `--with-pidfile' configure option, which defaults to
 ## `./tripectl.pid' (relative to $TRIPEDIR).