Simplify logging, update copyrights and some minor cleanups.
[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.3 2003/07/12 17:41:45 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 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 logger(int priority, const char *format, ...) {
61         va_list ap;
62
63         va_start(ap, format);
64
65         switch(logmode) {
66                 case LOGMODE_STDERR:
67                         vfprintf(stderr, format, ap);
68                         fprintf(stderr, "\n");
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_VSYSLOG
77                         vsyslog(priority, format, ap);
78 #else
79                         {
80                                 char message[4096];
81                                 vsnprintf(message, sizeof(message), format, ap);
82                                 syslog(priority, "%s", message);
83                         }
84 #endif
85                         break;
86         }
87
88         va_end(ap);
89 }
90
91 void closelogger(void) {
92         switch(logmode) {
93                 case LOGMODE_FILE:
94                         fclose(logfile);
95                         break;
96                 case LOGMODE_SYSLOG:
97                         closelog();
98                         break;
99         }
100 }