Improved --logfile option.
[oweals/tinc.git] / src / logger.c
index 02f3f0f8c14aac3ff35dd441babf9f3ea543efed..f886ba4c0d7d38dd434a4cf61bef68bd11f6d512 100644 (file)
@@ -1,7 +1,7 @@
 /*
     logger.c -- logging code
-    Copyright (C) 2003 Guus Sliepen <guus@sliepen.eu.org>
-                  2003 Ivo Timmermans <ivo@o2w.nl>
+    Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
+                  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
     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\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);
 
@@ -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);
                        }