Native Windows support.
[oweals/tinc.git] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2003 Guus Sliepen <guus@sliepen.eu.org>
4                   2003 Ivo Timmermans <ivo@o2w.nl>
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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: logger.c,v 1.1.2.8 2003/07/29 22:59:00 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "logger.h"
27
28 debug_t debug_level = DEBUG_NOTHING;
29 static logmode_t logmode = LOGMODE_STDERR;
30 static pid_t logpid;
31 extern char *logfilename;
32 static FILE *logfile = NULL;
33 static const char *logident = NULL;
34
35 void openlogger(const char *ident, logmode_t mode) {
36         logident = ident;
37         logmode = mode;
38         
39         switch(mode) {
40                 case LOGMODE_STDERR:
41                         logpid = getpid();
42                         break;
43                 case LOGMODE_FILE:
44                         logpid = getpid();
45                         logfile = fopen(logfilename, "a");
46                         if(!logfile)
47                                 logmode = LOGMODE_NULL;
48                         break;
49                 case LOGMODE_SYSLOG:
50 #ifdef HAVE_SYSLOG_H
51                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
52                         break;
53 #endif
54                 case LOGMODE_NULL:
55                         break;
56         }
57 }
58
59 void logger(int priority, const char *format, ...) {
60         va_list ap;
61
62         va_start(ap, format);
63
64         switch(logmode) {
65                 case LOGMODE_STDERR:
66                         vfprintf(stderr, format, ap);
67                         fprintf(stderr, "\n");
68                         fflush(stderr);
69                         break;
70                 case LOGMODE_FILE:
71                         fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
72                         vfprintf(logfile, format, ap);
73                         fprintf(logfile, "\n");
74                         break;
75                 case LOGMODE_SYSLOG:
76 #ifdef HAVE_SYSLOG_H
77 #ifdef HAVE_VSYSLOG
78                         vsyslog(priority, format, ap);
79 #else
80                         {
81                                 char message[4096];
82                                 vsnprintf(message, sizeof(message), format, ap);
83                                 syslog(priority, "%s", message);
84                         }
85 #endif
86                         break;
87 #endif
88                 case LOGMODE_NULL:
89                         break;
90         }
91
92         va_end(ap);
93 }
94
95 void closelogger(void) {
96         switch(logmode) {
97                 case LOGMODE_FILE:
98                         fclose(logfile);
99                         break;
100                 case LOGMODE_SYSLOG:
101 #ifdef HAVE_SYSLOG_H
102                         closelog();
103                         break;
104 #endif
105                 case LOGMODE_NULL:
106                 case LOGMODE_STDERR:
107                         break;
108                         break;
109         }
110 }