2 logger.c -- logging code
3 Copyright (C) 2004-2012 Guus Sliepen <guus@tinc-vpn.org>
4 2004-2005 Ivo Timmermans
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.
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.
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.
26 #include "connection.h"
27 #include "control_common.h"
30 debug_t debug_level = DEBUG_NOTHING;
31 static logmode_t logmode = LOGMODE_STDERR;
33 extern char *logfilename;
34 static FILE *logfile = NULL;
36 static HANDLE loghandle = NULL;
38 static const char *logident = NULL;
39 bool logcontrol = false;
42 static void real_logger(int level, int priority, const char *message) {
43 char timestr[32] = "";
45 static bool suppress = false;
47 // Bail out early if there is nothing to do.
51 if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
54 if(level <= debug_level) {
57 fprintf(stderr, "%s\n", message);
62 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now));
63 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
69 const char *messages[] = {message};
70 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
74 syslog(priority, "%s", message);
86 for list_each(connection_t, c, connection_list) {
90 if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
92 int len = strlen(message);
93 if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
94 send_meta(c, message, len);
100 void logger(int level, int priority, const char *format, ...) {
102 char message[1024] = "";
104 va_start(ap, format);
105 int len = vsnprintf(message, sizeof message, format, ap);
108 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
109 message[len - 1] = 0;
111 real_logger(level, priority, message);
114 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
115 char message[1024] = "";
116 int len = vsnprintf(message, sizeof message, format, ap);
117 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
118 message[len - 1] = 0;
120 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
123 void openlogger(const char *ident, logmode_t mode) {
133 logfile = fopen(logfilename, "a");
135 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
136 logmode = LOGMODE_NULL;
141 loghandle = RegisterEventSource(NULL, logident);
143 fprintf(stderr, "Could not open log handle!");
144 logmode = LOGMODE_NULL;
149 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
157 if(logmode != LOGMODE_NULL)
158 sptps_log = sptps_logger;
160 sptps_log = sptps_log_quiet;
163 void reopenlogger() {
164 if(logmode != LOGMODE_FILE)
168 FILE *newfile = fopen(logfilename, "a");
170 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
178 void closelogger(void) {
185 DeregisterEventSource(loghandle);