From ae8928d2ffbfa3b9dba70bc2f70379ddc20947dc Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Sun, 20 Jul 2014 14:52:26 +0100 Subject: [PATCH] Improve server logging. Organization: Straylight/Edgeware From: Mark Wooding * 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 | 22 ++++++++++++++++++---- client/tripectl.c | 19 +++++++++++++++---- contrib/tripe-upstart.in | 8 +++++++- init/tripe-init.in | 8 +++++++- init/tripe.conf | 9 ++++++++- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/client/tripectl.1.in b/client/tripectl.1.in index 4c6777e1..33e56cc9 100644 --- a/client/tripectl.1.in +++ b/client/tripectl.1.in @@ -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. diff --git a/client/tripectl.c b/client/tripectl.c index 95f95f44..f14c137e 100644 --- a/client/tripectl.c +++ b/client/tripectl.c @@ -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; diff --git a/contrib/tripe-upstart.in b/contrib/tripe-upstart.in index 42d15de0..8d558f01 100644 --- a/contrib/tripe-upstart.in +++ b/contrib/tripe-upstart.in @@ -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} \ diff --git a/init/tripe-init.in b/init/tripe-init.in index 7a21c4ad..c146bdf9 100755 --- a/init/tripe-init.in +++ b/init/tripe-init.in @@ -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} \ diff --git a/init/tripe.conf b/init/tripe.conf index 2b4a5a3e..743c17a6 100644 --- a/init/tripe.conf +++ b/init/tripe.conf @@ -68,10 +68,17 @@ #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). -- [mdw]