2 logger.c -- logging code
3 Copyright (C) 2004-2022 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"
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 static bool colorize_stderr = false;
42 bool logcontrol = false; // controlled by REQ_LOG <level>
44 bool umbilical_colorize = false;
46 #define SGR(s) ("\x1b[" s "m")
48 typedef enum color_t {
49 RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, GRAY,
50 RESET, // not really a color
53 typedef struct priority_disp_t {
58 static const priority_disp_t priorities[] = {
59 [LOG_EMERG] = {"EMERGENCY", MAGENTA},
60 [LOG_ALERT] = {"ALERT", MAGENTA},
61 [LOG_CRIT] = {"CRITICAL", MAGENTA},
62 [LOG_ERR] = {"ERROR", RED},
63 [LOG_WARNING] = {"WARNING", YELLOW},
64 [LOG_NOTICE] = {"NOTICE", CYAN},
65 [LOG_INFO] = {"INFO", GREEN},
66 [LOG_DEBUG] = {"DEBUG", BLUE},
69 static const char *ansi_codes[] = {
71 [GREEN] = SGR("32;1"),
72 [YELLOW] = SGR("33;1"),
74 [MAGENTA] = SGR("35;1"),
76 [WHITE] = SGR("37;1"),
81 static priority_disp_t get_priority(int priority) {
82 static const priority_disp_t unknown = {"UNKNOWN", WHITE};
84 if(priority >= LOG_EMERG && priority <= LOG_DEBUG) {
85 return priorities[priority];
91 // Formats current time to the second.
92 // Reuses result so repeated calls within the same second are more efficient.
93 static const char *current_time_str(void) {
94 static char timestr[sizeof("2000-12-31 12:34:56")] = "";
95 static time_t last_time = 0;
98 gettimeofday(&now, NULL);
101 time_t now_sec = now.tv_sec;
103 if(!*timestr || now_sec != last_time) {
105 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
111 // Format log entry with time, log level, and (possibly) colors, remembering if it was colorized.
112 // Returns true if buffer has been changed.
113 static bool format_pretty(char *buf, size_t buflen, int prio, const char *message, bool colorize, bool *colorized) {
114 // If we already wrote to buffer, and its colorization matches, we're done here
115 if(*buf && colorize == *colorized) {
119 // Otherwise, remember current colorization for future comparisons
120 *colorized = colorize;
122 priority_disp_t priority = get_priority(prio);
123 const char *color = "", *reset = "", *timecol = "";
126 color = ansi_codes[priority.color];
127 reset = ansi_codes[RESET];
128 timecol = ansi_codes[GRAY];
131 const char *timestr = current_time_str();
132 snprintf(buf, buflen, "%s%s %s%-7s%s %s", timecol, timestr, color, priority.name, reset, message);
136 static bool should_log(debug_t level) {
137 return (level <= debug_level && logmode != LOGMODE_NULL) || logcontrol;
140 static void real_logger(debug_t level, int priority, const char *message) {
141 char pretty[1024] = "";
142 bool pretty_colorized = false;
143 static bool suppress = false;
149 if(level <= debug_level) {
152 format_pretty(pretty, sizeof(pretty), priority, message, colorize_stderr, &pretty_colorized);
153 fprintf(stderr, "%s\n", pretty);
158 const char *timestr = current_time_str();
159 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
167 const char *messages[] = {message};
168 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
173 syslog(priority, "%s", message);
183 if(umbilical && do_detach) {
184 format_pretty(pretty, sizeof(pretty), priority, message, umbilical_colorize, &pretty_colorized);
186 if(write(umbilical, pretty, strlen(pretty)) == -1 || write(umbilical, "\n", 1) == -1) {
187 // Other end broken, nothing we can do about it.
196 size_t msglen = strlen(pretty);
198 for list_each(connection_t, c, &connection_list) {
205 if(level > (c->log_level != DEBUG_UNSET ? c->log_level : debug_level)) {
209 if(format_pretty(pretty, sizeof(pretty), priority, message, c->status.log_color, &pretty_colorized)) {
210 msglen = strlen(pretty);
213 if(send_request(c, "%d %d %lu", CONTROL, REQ_LOG, (unsigned long)msglen)) {
214 send_meta(c, pretty, msglen);
222 void logger(debug_t level, int priority, const char *format, ...) {
224 char message[1024] = "";
226 if(!should_log(level)) {
230 va_start(ap, format);
231 int len = vsnprintf(message, sizeof(message), format, ap);
232 message[sizeof(message) - 1] = 0;
235 if(len > 0 && (size_t)len < sizeof(message) - 1 && message[len - 1] == '\n') {
236 message[len - 1] = 0;
239 real_logger(level, priority, message);
242 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) ATTR_FORMAT(printf, 3, 0);
243 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
246 size_t msglen = sizeof(message);
248 if(!should_log(DEBUG_TRAFFIC)) {
252 int len = vsnprintf(message, msglen, format, ap);
253 message[sizeof(message) - 1] = 0;
255 if(len > 0 && (size_t)len < sizeof(message) - 1) {
256 if(message[len - 1] == '\n') {
260 // WARNING: s->handle can point to a connection_t or a node_t,
261 // but both types have the name and hostname fields at the same offsets.
262 connection_t *c = s->handle;
265 snprintf(message + len, sizeof(message) - len, " from %s (%s)", c->name, c->hostname);
269 real_logger(DEBUG_TRAFFIC, LOG_ERR, message);
272 void openlogger(const char *ident, logmode_t mode) {
279 colorize_stderr = use_ansi_escapes(stderr);
284 logfile = fopen(logfilename, "a");
287 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
288 logmode = LOGMODE_NULL;
295 loghandle = RegisterEventSource(NULL, logident);
298 fprintf(stderr, "Could not open log handle!\n");
299 logmode = LOGMODE_NULL;
305 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
315 if(logmode != LOGMODE_NULL) {
316 sptps_log = sptps_logger;
318 sptps_log = sptps_log_quiet;
322 void reopenlogger(void) {
323 if(logmode != LOGMODE_FILE) {
328 FILE *newfile = fopen(logfilename, "a");
331 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
340 void closelogger(void) {
348 DeregisterEventSource(loghandle);