2 logger.c -- logging code
3 Copyright (C) 2004-2015 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.
27 #include "connection.h"
28 #include "control_common.h"
31 debug_t debug_level = DEBUG_NOTHING;
32 static logmode_t logmode = LOGMODE_STDERR;
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] = "";
44 static bool suppress = false;
46 // Bail out early if there is nothing to do.
50 if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
53 if(level <= debug_level) {
56 fprintf(stderr, "%s\n", message);
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);
70 const char *messages[] = {message};
71 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
75 syslog(priority, "%s", message);
84 write(umbilical, message, strlen(message));
85 write(umbilical, "\n", 1);
92 for list_each(connection_t, c, connection_list) {
96 if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
98 int len = strlen(message);
99 if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
100 send_meta(c, message, len);
106 void logger(int level, int priority, const char *format, ...) {
108 char message[1024] = "";
110 va_start(ap, format);
111 int len = vsnprintf(message, sizeof message, format, ap);
114 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
115 message[len - 1] = 0;
117 real_logger(level, priority, message);
120 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
121 char message[1024] = "";
122 size_t msglen = sizeof message;
124 int len = vsnprintf(message, msglen, format, ap);
125 if(len > 0 && len < sizeof message) {
126 if(message[len - 1] == '\n')
129 connection_t *c = s->handle;
131 snprintf(message + len, sizeof message - len, " from %s (%s)", c->name, c->hostname);
134 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
137 void openlogger(const char *ident, logmode_t mode) {
147 logfile = fopen(logfilename, "a");
149 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
150 logmode = LOGMODE_NULL;
155 loghandle = RegisterEventSource(NULL, logident);
157 fprintf(stderr, "Could not open log handle!");
158 logmode = LOGMODE_NULL;
163 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
171 if(logmode != LOGMODE_NULL)
172 sptps_log = sptps_logger;
174 sptps_log = sptps_log_quiet;
177 void reopenlogger() {
178 if(logmode != LOGMODE_FILE)
182 FILE *newfile = fopen(logfilename, "a");
184 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
192 void closelogger(void) {
199 DeregisterEventSource(loghandle);