Improved --logfile option.
authorGuus Sliepen <guus@tinc-vpn.org>
Mon, 6 Jun 2011 14:26:11 +0000 (16:26 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Mon, 6 Jun 2011 14:26:11 +0000 (16:26 +0200)
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
doc/tinc.texi
doc/tincd.8.in
have.h
src/logger.c
src/logger.h
src/net.c

index 45db547..1421b16 100644 (file)
@@ -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
 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"]
 )
 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"]
 )
index 6bbc2e2..52a0ecc 100644 (file)
@@ -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.
 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.
 
 @item INT
 Temporarily increases debug level to 5.
index a8ef2fb..5ea08c0 100644 (file)
@@ -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.
 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.
 .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 (file)
--- a/have.h
+++ b/have.h
 #include <sys/time.h>
 #endif
 
 #include <sys/time.h>
 #endif
 
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
+
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
index bc20438..f886ba4 100644 (file)
@@ -44,14 +44,18 @@ void openlogger(const char *ident, logmode_t mode) {
                case LOGMODE_FILE:
                        logpid = getpid();
                        logfile = fopen(logfilename, "a");
                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;
                                logmode = LOGMODE_NULL;
+                       }
                        break;
                case LOGMODE_SYSLOG:
 #ifdef HAVE_MINGW
                        loghandle = RegisterEventSource(NULL, logident);
                        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;
                                logmode = LOGMODE_NULL;
+                       }
                        break;
 #else
 #ifdef HAVE_SYSLOG_H
                        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;
 void logger(int priority, const char *format, ...) {
        va_list ap;
+       char timestr[32] = "";
+       time_t now;
 
        va_start(ap, format);
 
 
        va_start(ap, format);
 
@@ -76,7 +96,9 @@ void logger(int priority, const char *format, ...) {
                        fflush(stderr);
                        break;
                case LOGMODE_FILE:
                        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);
                        vfprintf(logfile, format, ap);
                        fprintf(logfile, "\n");
                        fflush(logfile);
index 9c20ead..ff2cb34 100644 (file)
@@ -47,6 +47,7 @@ enum {
 
 extern debug_t debug_level;
 extern void openlogger(const char *, logmode_t);
 
 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);
 
 extern void logger(int, const char *, ...) __attribute__ ((__format__(printf, 2, 3)));
 extern void closelogger(void);
 
index c2f7022..c5193c5 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -501,6 +501,8 @@ int main_loop(void) {
                        struct stat s;
                        
                        sighup = false;
                        struct stat s;
                        
                        sighup = false;
+
+                       reopenlogger();
                        
                        /* Reread our own configuration file */
 
                        
                        /* Reread our own configuration file */