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