]> git.meshlink.io Git - meshlink/blob - src/logger.c
Include <limits.h> for PATH_MAX.
[meshlink] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2013 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 "sptps.h"
30
31 debug_t debug_level = DEBUG_NOTHING;
32 static logmode_t logmode = LOGMODE_STDERR;
33 static pid_t logpid;
34 static FILE *logfile = NULL;
35 #ifdef HAVE_MINGW
36 static HANDLE loghandle = NULL;
37 #endif
38 static const char *logident = NULL;
39 bool logcontrol = false;
40
41
42 static void real_logger(int level, int priority, const char *message) {
43         char timestr[32] = "";
44         static bool suppress = false;
45
46         // Bail out early if there is nothing to do.
47         if(suppress)
48                 return;
49
50         if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
51                 return;
52
53         if(level <= debug_level) {
54                 switch(logmode) {
55                         case LOGMODE_STDERR:
56                                 fprintf(stderr, "%s\n", message);
57                                 fflush(stderr);
58                                 break;
59                         case LOGMODE_FILE:
60                                 if(!now.tv_sec)
61                                         gettimeofday(&now, NULL);
62                                 time_t now_sec = now.tv_sec;
63                                 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
64                                 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
65                                 fflush(logfile);
66                                 break;
67                         case LOGMODE_SYSLOG:
68 #ifdef HAVE_MINGW
69                                 {
70                                         const char *messages[] = {message};
71                                         ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
72                                 }
73 #else
74 #ifdef HAVE_SYSLOG_H
75                                 syslog(priority, "%s", message);
76 #endif
77 #endif
78                                 break;
79                         case LOGMODE_NULL:
80                                 break;
81                 }
82         }
83
84         if(logcontrol) {
85                 suppress = true;
86                 logcontrol = false;
87                 for list_each(connection_t, c, connection_list) {
88                         if(!c->status.log)
89                                 continue;
90                         logcontrol = true;
91                         if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
92                                 continue;
93                         int len = strlen(message);
94                         if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
95                                 send_meta(c, message, len);
96                 }
97                 suppress = false;
98         }
99 }
100
101 void logger(int level, int priority, const char *format, ...) {
102         va_list ap;
103         char message[1024] = "";
104
105         va_start(ap, format);
106         int len = vsnprintf(message, sizeof message, format, ap);
107         va_end(ap);
108
109         if(len > 0 && len < sizeof message && message[len - 1] == '\n')
110                 message[len - 1] = 0;
111
112         real_logger(level, priority, message);
113 }
114
115 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
116         char message[1024] = "";
117         int len = vsnprintf(message, sizeof message, format, ap);
118         if(len > 0 && len < sizeof message && message[len - 1] == '\n')
119                 message[len - 1] = 0;
120
121         real_logger(DEBUG_ALWAYS, LOG_ERR, message);
122 }
123
124 void openlogger(const char *ident, logmode_t mode) {
125         logident = ident;
126         logmode = mode;
127
128         switch(mode) {
129                 case LOGMODE_STDERR:
130                         logpid = getpid();
131                         break;
132                 case LOGMODE_FILE:
133                         logpid = getpid();
134                         logfile = fopen(logfilename, "a");
135                         if(!logfile) {
136                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
137                                 logmode = LOGMODE_NULL;
138                         }
139                         break;
140                 case LOGMODE_SYSLOG:
141 #ifdef HAVE_MINGW
142                         loghandle = RegisterEventSource(NULL, logident);
143                         if(!loghandle) {
144                                 fprintf(stderr, "Could not open log handle!");
145                                 logmode = LOGMODE_NULL;
146                         }
147                         break;
148 #else
149 #ifdef HAVE_SYSLOG_H
150                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
151                         break;
152 #endif
153 #endif
154                 case LOGMODE_NULL:
155                         break;
156         }
157
158         if(logmode != LOGMODE_NULL)
159                 sptps_log = sptps_logger;
160         else
161                 sptps_log = sptps_log_quiet;
162 }
163
164 void reopenlogger() {
165         if(logmode != LOGMODE_FILE)
166                 return;
167
168         fflush(logfile);
169         FILE *newfile = fopen(logfilename, "a");
170         if(!newfile) {
171                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
172                 return;
173         }
174         fclose(logfile);
175         logfile = newfile;
176 }
177
178
179 void closelogger(void) {
180         switch(logmode) {
181                 case LOGMODE_FILE:
182                         fclose(logfile);
183                         break;
184                 case LOGMODE_SYSLOG:
185 #ifdef HAVE_MINGW
186                         DeregisterEventSource(loghandle);
187                         break;
188 #else
189 #ifdef HAVE_SYSLOG_H
190                         closelog();
191                         break;
192 #endif
193 #endif
194                 case LOGMODE_NULL:
195                 case LOGMODE_STDERR:
196                         break;
197                         break;
198         }
199 }