2 logger.c -- logging code
3 Copyright (C) 2004-2013 Guus Sliepen <guus@meshlink.io>
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.
26 #include "connection.h"
29 debug_t debug_level = DEBUG_NOTHING;
30 static logmode_t logmode = LOGMODE_STDERR;
33 static HANDLE loghandle = NULL;
35 static const char *logident = NULL;
37 static void real_logger(int level, int priority, const char *message) {
38 char timestr[32] = "";
39 static bool suppress = false;
41 // Bail out early if there is nothing to do.
45 if(level > debug_level || logmode == LOGMODE_NULL)
48 if(level <= debug_level) {
51 fprintf(stderr, "%s\n", message);
57 const char *messages[] = {message};
58 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
62 syslog(priority, "%s", message);
72 void logger(int level, int priority, const char *format, ...) {
74 char message[1024] = "";
77 int len = vsnprintf(message, sizeof message, format, ap);
80 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
83 real_logger(level, priority, message);
86 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
87 char message[1024] = "";
88 int len = vsnprintf(message, sizeof message, format, ap);
89 if(len > 0 && len < sizeof message && message[len - 1] == '\n')
92 real_logger(DEBUG_ALWAYS, LOG_ERR, message);
95 void openlogger(const char *ident, logmode_t mode) {
105 loghandle = RegisterEventSource(NULL, logident);
107 fprintf(stderr, "Could not open log handle!");
108 logmode = LOGMODE_NULL;
113 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
121 if(logmode != LOGMODE_NULL)
122 sptps_log = sptps_logger;
124 sptps_log = sptps_log_quiet;
127 void closelogger(void) {
131 DeregisterEventSource(loghandle);