Fix compiler warnings.
[oweals/tinc.git] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2017 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 "meta.h"
25 #include "names.h"
26 #include "logger.h"
27 #include "connection.h"
28 #include "control_common.h"
29 #include "process.h"
30 #include "sptps.h"
31
32 int debug_level = DEBUG_NOTHING;
33 static logmode_t logmode = LOGMODE_STDERR;
34 static pid_t logpid;
35 static FILE *logfile = NULL;
36 #ifdef HAVE_MINGW
37 static HANDLE loghandle = NULL;
38 #endif
39 static const char *logident = NULL;
40 bool logcontrol = false;
41 int umbilical = 0;
42
43 static void real_logger(int level, int priority, const char *message) {
44         char timestr[32] = "";
45         static bool suppress = false;
46
47         // Bail out early if there is nothing to do.
48         if(suppress) {
49                 return;
50         }
51
52         if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL)) {
53                 return;
54         }
55
56         if(level <= debug_level) {
57                 switch(logmode) {
58                 case LOGMODE_STDERR:
59                         fprintf(stderr, "%s\n", message);
60                         fflush(stderr);
61                         break;
62
63                 case LOGMODE_FILE:
64                         if(!now.tv_sec) {
65                                 gettimeofday(&now, NULL);
66                         }
67
68                         time_t now_sec = now.tv_sec;
69                         strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
70                         fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
71                         fflush(logfile);
72                         break;
73
74                 case LOGMODE_SYSLOG:
75 #ifdef HAVE_MINGW
76                         {
77                                 const char *messages[] = {message};
78                                 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
79                         }
80
81 #else
82 #ifdef HAVE_SYSLOG_H
83                         syslog(priority, "%s", message);
84 #endif
85 #endif
86                         break;
87
88                 case LOGMODE_NULL:
89                         break;
90                 }
91
92                 if(umbilical && do_detach) {
93                         write(umbilical, message, strlen(message));
94                         write(umbilical, "\n", 1);
95                 }
96         }
97
98         if(logcontrol) {
99                 suppress = true;
100                 logcontrol = false;
101
102                 for list_each(connection_t, c, connection_list) {
103                         if(!c->status.log) {
104                                 continue;
105                         }
106
107                         logcontrol = true;
108
109                         if(level > (c->outcompression >= 0 ? c->outcompression : debug_level)) {
110                                 continue;
111                         }
112
113                         int len = strlen(message);
114
115                         if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len)) {
116                                 send_meta(c, message, len);
117                         }
118                 }
119
120                 suppress = false;
121         }
122 }
123
124 void logger(int level, int priority, const char *format, ...) {
125         va_list ap;
126         char message[1024] = "";
127
128         va_start(ap, format);
129         int len = vsnprintf(message, sizeof(message), format, ap);
130         message[sizeof(message) - 1] = 0;
131         va_end(ap);
132
133         if(len > 0 && len < sizeof(message) - 1 && message[len - 1] == '\n') {
134                 message[len - 1] = 0;
135         }
136
137         real_logger(level, priority, message);
138 }
139
140 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
141         char message[1024];
142         size_t msglen = sizeof(message);
143
144         int len = vsnprintf(message, msglen, format, ap);
145         message[sizeof(message) - 1] = 0;
146
147         if(len > 0 && len < sizeof(message) - 1) {
148                 if(message[len - 1] == '\n') {
149                         message[--len] = 0;
150                 }
151
152                 // WARNING: s->handle can point to a connection_t or a node_t,
153                 // but both types have the name and hostname fields at the same offsets.
154                 connection_t *c = s->handle;
155
156                 if(c) {
157                         snprintf(message + len, sizeof(message) - len, " from %s (%s)", c->name, c->hostname);
158                 }
159         }
160
161         real_logger(DEBUG_ALWAYS, LOG_ERR, message);
162 }
163
164 void openlogger(const char *ident, logmode_t mode) {
165         logident = ident;
166         logmode = mode;
167
168         switch(mode) {
169         case LOGMODE_STDERR:
170                 logpid = getpid();
171                 break;
172
173         case LOGMODE_FILE:
174                 logpid = getpid();
175                 logfile = fopen(logfilename, "a");
176
177                 if(!logfile) {
178                         fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
179                         logmode = LOGMODE_NULL;
180                 }
181
182                 break;
183
184         case LOGMODE_SYSLOG:
185 #ifdef HAVE_MINGW
186                 loghandle = RegisterEventSource(NULL, logident);
187
188                 if(!loghandle) {
189                         fprintf(stderr, "Could not open log handle!\n");
190                         logmode = LOGMODE_NULL;
191                 }
192
193                 break;
194 #else
195 #ifdef HAVE_SYSLOG_H
196                 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
197                 break;
198 #endif
199 #endif
200
201         case LOGMODE_NULL:
202                 break;
203         }
204
205         if(logmode != LOGMODE_NULL) {
206                 sptps_log = sptps_logger;
207         } else {
208                 sptps_log = sptps_log_quiet;
209         }
210 }
211
212 void reopenlogger() {
213         if(logmode != LOGMODE_FILE) {
214                 return;
215         }
216
217         fflush(logfile);
218         FILE *newfile = fopen(logfilename, "a");
219
220         if(!newfile) {
221                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
222                 return;
223         }
224
225         fclose(logfile);
226         logfile = newfile;
227 }
228
229
230 void closelogger(void) {
231         switch(logmode) {
232         case LOGMODE_FILE:
233                 fclose(logfile);
234                 break;
235
236         case LOGMODE_SYSLOG:
237 #ifdef HAVE_MINGW
238                 DeregisterEventSource(loghandle);
239                 break;
240 #else
241 #ifdef HAVE_SYSLOG_H
242                 closelog();
243                 break;
244 #endif
245 #endif
246
247         case LOGMODE_NULL:
248         case LOGMODE_STDERR:
249                 break;
250         }
251 }