More missing IPv6 definitions and autoconf checks to make sure it compiles
[tinc] / 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.2 2003/07/06 23:16:28 guus Exp $
21 */
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <syslog.h>
26 #include <unistd.h>
27
28 #include "conf.h"
29 #include "logger.h"
30
31 #include "system.h"
32
33 volatile int debug_level = DEBUG_NOTHING;
34 static int logmode = LOGMODE_STDERR;
35 static pid_t logpid;
36 extern char *logfilename;
37 static FILE *logfile = NULL;
38 static const char *logident = NULL;
39
40 void openlogger(const char *ident, int mode) {
41         logident = ident;
42         logmode = mode;
43         
44         switch(mode) {
45                 case LOGMODE_STDERR:
46                         logpid = getpid();
47                         break;
48                 case LOGMODE_FILE:
49                         logpid = getpid();
50                         logfile = fopen(logfilename, "a");
51                         if(!logfile)
52                                 logmode = LOGMODE_NULL;
53                         break;
54                 case LOGMODE_SYSLOG:
55                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
56                         break;
57         }
58 }
59
60 void vlogger(int priority, const char *format, va_list ap) {
61         switch(logmode) {
62                 case LOGMODE_STDERR:
63                         vfprintf(stderr, format, ap);
64                         fprintf(stderr, "\n");
65                         break;
66                 case LOGMODE_FILE:
67                         fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
68                         vfprintf(logfile, format, ap);
69                         fprintf(logfile, "\n");
70                         break;
71                 case LOGMODE_SYSLOG:
72 #ifdef HAVE_VSYSLOG
73                         vsyslog(priority, format, ap);
74 #else
75                         {
76                                 char message[4096];
77                                 vsnprintf(message, sizeof(message), format, ap);
78                                 syslog(priority, "%s", message);
79                         }
80 #endif
81                         break;
82         }
83 }
84
85 void closelogger(void) {
86         switch(logmode) {
87                 case LOGMODE_FILE:
88                         fclose(logfile);
89                         break;
90                 case LOGMODE_SYSLOG:
91                         closelog();
92                         break;
93         }
94 }