2 logger.c -- logging code
3 Copyright (C) 2004-2006 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.
26 debug_t debug_level = DEBUG_NOTHING;
27 static logmode_t logmode = LOGMODE_STDERR;
29 extern char *logfilename;
30 static FILE *logfile = NULL;
32 static HANDLE loghandle = NULL;
34 static const char *logident = NULL;
36 void openlogger(const char *ident, logmode_t mode) {
46 logfile = fopen(logfilename, "a");
48 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
49 logmode = LOGMODE_NULL;
54 loghandle = RegisterEventSource(NULL, logident);
56 fprintf(stderr, "Could not open log handle!");
57 logmode = LOGMODE_NULL;
62 openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
72 if(logmode != LOGMODE_FILE)
76 FILE *newfile = fopen(logfilename, "a");
78 logger(LOG_ERR, "Unable to reopen log file %s: %s\n", logfilename, strerror(errno));
85 void logger(int priority, const char *format, ...) {
87 char timestr[32] = "";
94 vfprintf(stderr, format, ap);
95 fprintf(stderr, "\n");
100 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now));
101 fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid);
102 vfprintf(logfile, format, ap);
103 fprintf(logfile, "\n");
110 const char *messages[] = {message};
111 vsnprintf(message, sizeof(message), format, ap);
112 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
117 vsyslog(priority, format, ap);
121 vsnprintf(message, sizeof(message), format, ap);
122 syslog(priority, "%s", message);
135 void closelogger(void) {
142 DeregisterEventSource(loghandle);