2 logger.c -- logging code
3 Copyright (C) 2004-2013 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"
30 debug_t debug_level = DEBUG_NOTHING;
31 static logmode_t logmode = LOGMODE_STDERR;
33 static FILE *logfile = NULL;
35 static HANDLE loghandle = NULL;
37 static const char *logident = NULL;
39 static void real_logger(int level, int priority, const char *message) {
40 char timestr[32] = "";
41 static bool suppress = false;
43 // Bail out early if there is nothing to do.
47 if(level > debug_level || logmode == LOGMODE_NULL)
50 if(level <= debug_level) {
53 fprintf(stderr, "%s\n", message);
58 gettimeofday(&now, NULL);
59 time_t now_sec = now.tv_sec;
60 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
61 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
67 const char *messages[] = {message};
68 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
72 syslog(priority, "%s", message);
82 void logger(int level, int priority, const char *format, ...) {
84 char message[1024] = "";
87 int len = vsnprintf(message, sizeof message, format, ap);
90 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
93 real_logger(level, priority, message);
96 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
97 char message[1024] = "";
98 int len = vsnprintf(message, sizeof message, format, ap);
99 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
100 message[len - 1] = 0;
102 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
105 void openlogger(const char *ident, logmode_t mode) {
115 logfile = fopen(logfilename, "a");
117 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
118 logmode = LOGMODE_NULL;
123 loghandle = RegisterEventSource(NULL, logident);
125 fprintf(stderr, "Could not open log handle!");
126 logmode = LOGMODE_NULL;
131 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
139 if(logmode != LOGMODE_NULL)
140 sptps_log = sptps_logger;
142 sptps_log = sptps_log_quiet;
145 void reopenlogger() {
146 if(logmode != LOGMODE_FILE)
150 FILE *newfile = fopen(logfilename, "a");
152 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
160 void closelogger(void) {
167 DeregisterEventSource(loghandle);