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.
This commit is contained in:
parent
b3bbeab6e6
commit
4b3fd94b1c
7 changed files with 39 additions and 4 deletions
|
@ -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"]
|
||||
)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
4
have.h
4
have.h
|
@ -71,6 +71,10 @@
|
|||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_TIME_H
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
|
28
src/logger.c
28
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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -501,6 +501,8 @@ int main_loop(void) {
|
|||
struct stat s;
|
||||
|
||||
sighup = false;
|
||||
|
||||
reopenlogger();
|
||||
|
||||
/* Reread our own configuration file */
|
||||
|
||||
|
|
Loading…
Reference in a new issue