Remove the warnings when IP_DONTFRAGMENT/IPV6-DONTFRAG is not supported.
[tinc] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
4                   2004-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25
26 debug_t debug_level = DEBUG_NOTHING;
27 static logmode_t logmode = LOGMODE_STDERR;
28 static pid_t logpid;
29 extern char *logfilename;
30 static FILE *logfile = NULL;
31 #ifdef HAVE_MINGW
32 static HANDLE loghandle = NULL;
33 #endif
34 static const char *logident = NULL;
35
36 void openlogger(const char *ident, logmode_t mode) {
37         logident = ident;
38         logmode = mode;
39         
40         switch(mode) {
41                 case LOGMODE_STDERR:
42                         logpid = getpid();
43                         break;
44                 case LOGMODE_FILE:
45                         logpid = getpid();
46                         logfile = fopen(logfilename, "a");
47                         if(!logfile) {
48                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
49                                 logmode = LOGMODE_NULL;
50                         }
51                         break;
52                 case LOGMODE_SYSLOG:
53 #ifdef HAVE_MINGW
54                         loghandle = RegisterEventSource(NULL, logident);
55                         if(!loghandle) {
56                                 fprintf(stderr, "Could not open log handle!");
57                                 logmode = LOGMODE_NULL;
58                         }
59                         break;
60 #else
61 #ifdef HAVE_SYSLOG_H
62                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
63                         break;
64 #endif
65 #endif
66                 case LOGMODE_NULL:
67                         break;
68         }
69 }
70
71 void reopenlogger() {
72         if(logmode != LOGMODE_FILE)
73                 return;
74
75         fflush(logfile);
76         FILE *newfile = fopen(logfilename, "a");
77         if(!newfile) {
78                 logger(LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
79                 return;
80         }
81         fclose(logfile);
82         logfile = newfile;
83 }
84
85 void logger(int priority, const char *format, ...) {
86         va_list ap;
87         char timestr[32] = "";
88         time_t now;
89
90         va_start(ap, format);
91
92         switch(logmode) {
93                 case LOGMODE_STDERR:
94                         vfprintf(stderr, format, ap);
95                         fprintf(stderr, "\n");
96                         fflush(stderr);
97                         break;
98                 case LOGMODE_FILE:
99                         now = time(NULL);
100                         strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now));
101                         fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid);
102                         vfprintf(logfile, format, ap);
103                         fprintf(logfile, "\n");
104                         fflush(logfile);
105                         break;
106                 case LOGMODE_SYSLOG:
107 #ifdef HAVE_MINGW
108                         {
109                                 char message[4096];
110                                 const char *messages[] = {message};
111                                 vsnprintf(message, sizeof(message), format, ap);
112                                 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
113                         }
114 #else
115 #ifdef HAVE_SYSLOG_H
116 #ifdef HAVE_VSYSLOG
117                         vsyslog(priority, format, ap);
118 #else
119                         {
120                                 char message[4096];
121                                 vsnprintf(message, sizeof(message), format, ap);
122                                 syslog(priority, "%s", message);
123                         }
124 #endif
125                         break;
126 #endif
127 #endif
128                 case LOGMODE_NULL:
129                         break;
130         }
131
132         va_end(ap);
133 }
134
135 void closelogger(void) {
136         switch(logmode) {
137                 case LOGMODE_FILE:
138                         fclose(logfile);
139                         break;
140                 case LOGMODE_SYSLOG:
141 #ifdef HAVE_MINGW
142                         DeregisterEventSource(loghandle);
143                         break;
144 #else
145 #ifdef HAVE_SYSLOG_H
146                         closelog();
147                         break;
148 #endif
149 #endif
150                 case LOGMODE_NULL:
151                 case LOGMODE_STDERR:
152                         break;
153                         break;
154         }
155 }