Use bools and enums where appropriate.
[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.5 2003/07/22 20:55:19 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_NULL:
41                         break;
42                 case LOGMODE_STDERR:
43                         logpid = getpid();
44                         break;
45                 case LOGMODE_FILE:
46                         logpid = getpid();
47                         logfile = fopen(logfilename, "a");
48                         if(!logfile)
49                                 logmode = LOGMODE_NULL;
50                         break;
51                 case LOGMODE_SYSLOG:
52                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
53                         break;
54         }
55 }
56
57 void logger(int priority, const char *format, ...) {
58         va_list ap;
59
60         va_start(ap, format);
61
62         switch(logmode) {
63                 case LOGMODE_NULL:
64                         break;
65                 case LOGMODE_STDERR:
66                         vfprintf(stderr, format, ap);
67                         fprintf(stderr, "\n");
68                         break;
69                 case LOGMODE_FILE:
70                         fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
71                         vfprintf(logfile, format, ap);
72                         fprintf(logfile, "\n");
73                         break;
74                 case LOGMODE_SYSLOG:
75 #ifdef HAVE_VSYSLOG
76                         vsyslog(priority, format, ap);
77 #else
78                         {
79                                 char message[4096];
80                                 vsnprintf(message, sizeof(message), format, ap);
81                                 syslog(priority, "%s", message);
82                         }
83 #endif
84                         break;
85         }
86
87         va_end(ap);
88 }
89
90 void closelogger(void) {
91         switch(logmode) {
92                 case LOGMODE_NULL:
93                 case LOGMODE_STDERR:
94                         break;
95                 case LOGMODE_FILE:
96                         fclose(logfile);
97                         break;
98                 case LOGMODE_SYSLOG:
99                         closelog();
100                         break;
101         }
102 }