Merge branch 'master' into 1.1
[tinc] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
4                   2004-2005 Ivo Timmermans
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25
26 debug_t debug_level = DEBUG_NOTHING;
27 static logmode_t logmode = LOGMODE_STDERR;
28 static pid_t logpid;
29 extern char *logfilename;
30 static FILE *logfile = NULL;
31 #ifdef HAVE_MINGW
32 static HANDLE loghandle = NULL;
33 #endif
34 static const char *logident = NULL;
35
36 void openlogger(const char *ident, logmode_t mode) {
37         logident = ident;
38         logmode = mode;
39         
40         switch(mode) {
41                 case LOGMODE_STDERR:
42                         logpid = getpid();
43                         break;
44                 case LOGMODE_FILE:
45                         logpid = getpid();
46                         logfile = fopen(logfilename, "a");
47                         if(!logfile)
48                                 logmode = LOGMODE_NULL;
49                         break;
50                 case LOGMODE_SYSLOG:
51 #ifdef HAVE_MINGW
52                         loghandle = RegisterEventSource(NULL, logident);
53                         if(!loghandle)
54                                 logmode = LOGMODE_NULL;
55                         break;
56 #else
57 #ifdef HAVE_SYSLOG_H
58                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
59                         break;
60 #endif
61 #endif
62                 case LOGMODE_NULL:
63                         break;
64         }
65 }
66
67 void logger(int priority, const char *format, ...) {
68         va_list ap;
69
70         va_start(ap, format);
71
72         switch(logmode) {
73                 case LOGMODE_STDERR:
74                         vfprintf(stderr, format, ap);
75                         fprintf(stderr, "\n");
76                         fflush(stderr);
77                         break;
78                 case LOGMODE_FILE:
79                         fprintf(logfile, "%ld %s[%ld]: ", time(NULL), logident, (long)logpid);
80                         vfprintf(logfile, format, ap);
81                         fprintf(logfile, "\n");
82                         fflush(logfile);
83                         break;
84                 case LOGMODE_SYSLOG:
85 #ifdef HAVE_MINGW
86                         {
87                                 char message[4096];
88                                 const char *messages[] = {message};
89                                 vsnprintf(message, sizeof message, format, ap);
90                                 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
91                         }
92 #else
93 #ifdef HAVE_SYSLOG_H
94 #ifdef HAVE_VSYSLOG
95                         vsyslog(priority, format, ap);
96 #else
97                         {
98                                 char message[4096];
99                                 vsnprintf(message, sizeof message, format, ap);
100                                 syslog(priority, "%s", message);
101                         }
102 #endif
103                         break;
104 #endif
105 #endif
106                 case LOGMODE_NULL:
107                         break;
108         }
109
110         va_end(ap);
111 }
112
113 void closelogger(void) {
114         switch(logmode) {
115                 case LOGMODE_FILE:
116                         fclose(logfile);
117                         break;
118                 case LOGMODE_SYSLOG:
119 #ifdef HAVE_MINGW
120                         DeregisterEventSource(loghandle);
121                         break;
122 #else
123 #ifdef HAVE_SYSLOG_H
124                         closelog();
125                         break;
126 #endif
127 #endif
128                 case LOGMODE_NULL:
129                 case LOGMODE_STDERR:
130                         break;
131                         break;
132         }
133 }