From 4b3fd94b1cc79c24c4092b6b10ed4627a2648d26 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 6 Jun 2011 16:26:11 +0200 Subject: [PATCH 1/1] Improved --logfile option. Instead of UNIX time, the log messages now start with the time in RFC3339 format, which human-readable and still easy for the computer to parse and sort. The HUP signal will also cause the log file to be closed and reopened, which is useful when log rotation is used. If there is an error while opening the log file, this is logged to stderr. --- configure.in | 2 +- doc/tinc.texi | 2 ++ doc/tincd.8.in | 4 ++++ have.h | 4 ++++ src/logger.c | 28 +++++++++++++++++++++++++--- src/logger.h | 1 + src/net.c | 2 ++ 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/configure.in b/configure.in index 45db547..1421b16 100644 --- a/configure.in +++ b/configure.in @@ -101,7 +101,7 @@ dnl Checks for header files. dnl We do this in multiple stages, because unlike Linux all the other operating systems really suck and don't include their own dependencies. AC_HEADER_STDC -AC_CHECK_HEADERS([stdbool.h syslog.h sys/file.h sys/ioctl.h sys/mman.h sys/param.h sys/resource.h sys/socket.h sys/time.h sys/uio.h sys/wait.h netdb.h arpa/inet.h dirent.h]) +AC_CHECK_HEADERS([stdbool.h syslog.h sys/file.h sys/ioctl.h sys/mman.h sys/param.h sys/resource.h sys/socket.h sys/time.h time.h sys/uio.h sys/wait.h netdb.h arpa/inet.h dirent.h]) AC_CHECK_HEADERS([net/if.h net/if_types.h linux/if_tun.h net/if_tun.h net/tun/if_tun.h net/if_tap.h net/tap/if_tap.h net/ethernet.h net/if_arp.h netinet/in_systm.h netinet/in.h netinet/in6.h], [], [], [#include "have.h"] ) diff --git a/doc/tinc.texi b/doc/tinc.texi index 6bbc2e2..52a0ecc 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -1638,6 +1638,8 @@ it defaults to the maximum time of 15 minutes. Partially rereads configuration files. Connections to hosts whose host config file are removed are closed. New outgoing connections specified in @file{tinc.conf} will be made. +If the --logfile option is used, this will also close and reopen the log file, +useful when log rotation is used. @item INT Temporarily increases debug level to 5. diff --git a/doc/tincd.8.in b/doc/tincd.8.in index a8ef2fb..5ea08c0 100644 --- a/doc/tincd.8.in +++ b/doc/tincd.8.in @@ -130,6 +130,10 @@ Connections to hosts whose host config file are removed are closed. New outgoing connections specified in .Pa tinc.conf will be made. +If the +.Fl -logfile +option is used, this will also close and reopen the log file, +useful when log rotation is used. .It INT Temporarily increases debug level to 5. Send this signal again to revert to the original level. diff --git a/have.h b/have.h index 073fbaa..72af069 100644 --- a/have.h +++ b/have.h @@ -71,6 +71,10 @@ #include #endif +#ifdef HAVE_TIME_H +#include +#endif + #ifdef HAVE_SYS_TYPES_H #include #endif diff --git a/src/logger.c b/src/logger.c index bc20438..f886ba4 100644 --- a/src/logger.c +++ b/src/logger.c @@ -44,14 +44,18 @@ void openlogger(const char *ident, logmode_t mode) { case LOGMODE_FILE: logpid = getpid(); logfile = fopen(logfilename, "a"); - if(!logfile) + if(!logfile) { + fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno)); logmode = LOGMODE_NULL; + } break; case LOGMODE_SYSLOG: #ifdef HAVE_MINGW loghandle = RegisterEventSource(NULL, logident); - if(!loghandle) + if(!loghandle) { + fprintf(stderr, "Could not open log handle!"); logmode = LOGMODE_NULL; + } break; #else #ifdef HAVE_SYSLOG_H @@ -64,8 +68,24 @@ void openlogger(const char *ident, logmode_t mode) { } } +void reopenlogger() { + if(logmode != LOGMODE_FILE) + return; + + fflush(logfile); + FILE *newfile = fopen(logfilename, "a"); + if(!newfile) { + logger(LOG_ERR, "Unable to reopen log file %s: %s\n", logfilename, strerror(errno)); + return; + } + fclose(logfile); + logfile = newfile; +} + void logger(int priority, const char *format, ...) { va_list ap; + char timestr[32] = ""; + time_t now; va_start(ap, format); @@ -76,7 +96,9 @@ void logger(int priority, const char *format, ...) { fflush(stderr); break; case LOGMODE_FILE: - fprintf(logfile, "%ld %s[%ld]: ", time(NULL), logident, (long)logpid); + now = time(NULL); + strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now)); + fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid); vfprintf(logfile, format, ap); fprintf(logfile, "\n"); fflush(logfile); diff --git a/src/logger.h b/src/logger.h index 9c20ead..ff2cb34 100644 --- a/src/logger.h +++ b/src/logger.h @@ -47,6 +47,7 @@ enum { extern debug_t debug_level; extern void openlogger(const char *, logmode_t); +extern void reopenlogger(void); extern void logger(int, const char *, ...) __attribute__ ((__format__(printf, 2, 3))); extern void closelogger(void); diff --git a/src/net.c b/src/net.c index c2f7022..c5193c5 100644 --- a/src/net.c +++ b/src/net.c @@ -501,6 +501,8 @@ int main_loop(void) { struct stat s; sighup = false; + + reopenlogger(); /* Reread our own configuration file */ -- 2.25.1