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"
32 debug_t debug_level = DEBUG_NOTHING;
33 static logmode_t logmode = LOGMODE_STDERR;
35 static FILE *logfile = NULL;
37 static HANDLE loghandle = NULL;
39 static const char *logident = NULL;
40 bool logcontrol = false;
43 static void real_logger(int level, int priority, const char *message) {
44 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 gettimeofday(&now, NULL);
63 time_t now_sec = now.tv_sec;
64 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
65 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
71 const char *messages[] = {message};
72 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
76 syslog(priority, "%s", message);
84 if(umbilical && do_detach) {
85 write(umbilical, message, strlen(message));
86 write(umbilical, "\n", 1);
93 for list_each(connection_t, c, connection_list) {
97 if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
99 int len = strlen(message);
100 if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
101 send_meta(c, message, len);
107 void logger(int level, int priority, const char *format, ...) {
109 char message[1024] = "";
111 va_start(ap, format);
112 int len = vsnprintf(message, sizeof message, format, ap);
113 message[sizeof message - 1] = 0;
116 if(len > 0 && len < sizeof message - 1 && message[len - 1] == '\n')
117 message[len - 1] = 0;
119 real_logger(level, priority, message);
122 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
124 size_t msglen = sizeof message;
126 int len = vsnprintf(message, msglen, format, ap);
127 message[sizeof message - 1] = 0;
128 if(len > 0 && len < sizeof message - 1) {
129 if(message[len - 1] == '\n')
132 // WARNING: s->handle can point to a connection_t or a node_t,
133 // but both types have the name and hostname fields at the same offsets.
134 connection_t *c = s->handle;
136 snprintf(message + len, sizeof message - len, " from %s (%s)", c->name, c->hostname);
139 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
142 void openlogger(const char *ident, logmode_t mode) {
152 logfile = fopen(logfilename, "a");
154 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
155 logmode = LOGMODE_NULL;
160 loghandle = RegisterEventSource(NULL, logident);
162 fprintf(stderr, "Could not open log handle!");
163 logmode = LOGMODE_NULL;
168 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
176 if(logmode != LOGMODE_NULL)
177 sptps_log = sptps_logger;
179 sptps_log = sptps_log_quiet;
182 void reopenlogger() {
183 if(logmode != LOGMODE_FILE)
187 FILE *newfile = fopen(logfilename, "a");
189 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
197 void closelogger(void) {
204 DeregisterEventSource(loghandle);