X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Flogger.c;h=6765cc5abeba7994b1283734714577eeb44f3bf1;hp=02f3f0f8c14aac3ff35dd441babf9f3ea543efed;hb=85a841258c1a19282b48c6b8663128568c16d9ab;hpb=46cfe6199449a86eb58abaeac45b4021ffa7e178 diff --git a/src/logger.c b/src/logger.c index 02f3f0f8..6765cc5a 100644 --- a/src/logger.c +++ b/src/logger.c @@ -1,7 +1,7 @@ /* logger.c -- logging code - Copyright (C) 2003 Guus Sliepen - 2003 Ivo Timmermans + Copyright (C) 2004-2006 Guus Sliepen + 2004-2005 Ivo Timmermans This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,11 +13,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id: logger.c,v 1.1.2.11 2003/08/17 12:04:35 guus Exp $ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "system.h" @@ -46,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 @@ -66,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", 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); @@ -78,7 +96,9 @@ void logger(int priority, const char *format, ...) { fflush(stderr); break; case LOGMODE_FILE: - fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, 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); @@ -87,7 +107,7 @@ void logger(int priority, const char *format, ...) { #ifdef HAVE_MINGW { char message[4096]; - char *messages[] = {message}; + const char *messages[] = {message}; vsnprintf(message, sizeof(message), format, ap); ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL); }