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"
31 #include "compression.h"
33 debug_t debug_level = DEBUG_NOTHING;
34 static logmode_t logmode = LOGMODE_STDERR;
36 static FILE *logfile = NULL;
38 static HANDLE loghandle = NULL;
40 static const char *logident = NULL;
41 bool logcontrol = false; // controlled by REQ_LOG <level>
44 static bool should_log(debug_t level) {
45 return (level <= debug_level && logmode != LOGMODE_NULL) || logcontrol;
48 static void real_logger(debug_t level, int priority, const char *message) {
49 char timestr[32] = "";
50 static bool suppress = false;
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);
93 if(umbilical && do_detach) {
94 write(umbilical, message, strlen(message));
95 write(umbilical, "\n", 1);
103 for list_each(connection_t, c, &connection_list) {
110 if(level > (c->outcompression >= COMPRESS_NONE ? c->outcompression : debug_level)) {
114 size_t len = strlen(message);
116 if(send_request(c, "%d %d %zu", CONTROL, REQ_LOG, len)) {
117 send_meta(c, message, len);
125 void logger(debug_t level, int priority, const char *format, ...) {
127 char message[1024] = "";
129 if(!should_log(level)) {
133 va_start(ap, format);
134 int len = vsnprintf(message, sizeof(message), format, ap);
135 message[sizeof(message) - 1] = 0;
138 if(len > 0 && (size_t)len < sizeof(message) - 1 && message[len - 1] == '\n') {
139 message[len - 1] = 0;
142 real_logger(level, priority, message);
145 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
148 size_t msglen = sizeof(message);
150 if(!should_log(DEBUG_ALWAYS)) {
154 int len = vsnprintf(message, msglen, format, ap);
155 message[sizeof(message) - 1] = 0;
157 if(len > 0 && (size_t)len < sizeof(message) - 1) {
158 if(message[len - 1] == '\n') {
162 // WARNING: s->handle can point to a connection_t or a node_t,
163 // but both types have the name and hostname fields at the same offsets.
164 connection_t *c = s->handle;
167 snprintf(message + len, sizeof(message) - len, " from %s (%s)", c->name, c->hostname);
171 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
174 void openlogger(const char *ident, logmode_t mode) {
185 logfile = fopen(logfilename, "a");
188 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
189 logmode = LOGMODE_NULL;
196 loghandle = RegisterEventSource(NULL, logident);
199 fprintf(stderr, "Could not open log handle!\n");
200 logmode = LOGMODE_NULL;
206 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
216 if(logmode != LOGMODE_NULL) {
217 sptps_log = sptps_logger;
219 sptps_log = sptps_log_quiet;
223 void reopenlogger() {
224 if(logmode != LOGMODE_FILE) {
229 FILE *newfile = fopen(logfilename, "a");
232 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
241 void closelogger(void) {
249 DeregisterEventSource(loghandle);