2 logger.c -- logging code
3 Copyright (C) 2004-2017 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 int 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.
52 if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL)) {
56 if(level <= debug_level) {
59 fprintf(stderr, "%s\n", message);
65 gettimeofday(&now, NULL);
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);
77 const char *messages[] = {message};
78 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
83 syslog(priority, "%s", message);
92 if(umbilical && do_detach) {
93 write(umbilical, message, strlen(message));
94 write(umbilical, "\n", 1);
102 for list_each(connection_t, c, connection_list) {
109 if(level > (c->outcompression >= 0 ? c->outcompression : debug_level)) {
113 int len = strlen(message);
115 if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len)) {
116 send_meta(c, message, len);
124 void logger(int level, int priority, const char *format, ...) {
126 char message[1024] = "";
128 va_start(ap, format);
129 int len = vsnprintf(message, sizeof(message), format, ap);
130 message[sizeof(message) - 1] = 0;
133 if(len > 0 && (size_t)len < sizeof(message) - 1 && message[len - 1] == '\n') {
134 message[len - 1] = 0;
137 real_logger(level, priority, message);
140 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
143 size_t msglen = sizeof(message);
145 int len = vsnprintf(message, msglen, format, ap);
146 message[sizeof(message) - 1] = 0;
148 if(len > 0 && (size_t)len < sizeof(message) - 1) {
149 if(message[len - 1] == '\n') {
153 // WARNING: s->handle can point to a connection_t or a node_t,
154 // but both types have the name and hostname fields at the same offsets.
155 connection_t *c = s->handle;
158 snprintf(message + len, sizeof(message) - len, " from %s (%s)", c->name, c->hostname);
162 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
165 void openlogger(const char *ident, logmode_t mode) {
176 logfile = fopen(logfilename, "a");
179 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
180 logmode = LOGMODE_NULL;
187 loghandle = RegisterEventSource(NULL, logident);
190 fprintf(stderr, "Could not open log handle!\n");
191 logmode = LOGMODE_NULL;
197 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
206 if(logmode != LOGMODE_NULL) {
207 sptps_log = sptps_logger;
209 sptps_log = sptps_log_quiet;
213 void reopenlogger() {
214 if(logmode != LOGMODE_FILE) {
219 FILE *newfile = fopen(logfilename, "a");
222 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
231 void closelogger(void) {
239 DeregisterEventSource(loghandle);