Big header file cleanup: everything that has to do with standard system
[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.4 2003/07/17 15:06:26 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "logger.h"
27
28 int debug_level = DEBUG_NOTHING;
29 static int 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, int 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                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
51                         break;
52         }
53 }
54
55 void logger(int priority, const char *format, ...) {
56         va_list ap;
57
58         va_start(ap, format);
59
60         switch(logmode) {
61                 case LOGMODE_STDERR:
62                         vfprintf(stderr, format, ap);
63                         fprintf(stderr, "\n");
64                         break;
65                 case LOGMODE_FILE:
66                         fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
67                         vfprintf(logfile, format, ap);
68                         fprintf(logfile, "\n");
69                         break;
70                 case LOGMODE_SYSLOG:
71 #ifdef HAVE_VSYSLOG
72                         vsyslog(priority, format, ap);
73 #else
74                         {
75                                 char message[4096];
76                                 vsnprintf(message, sizeof(message), format, ap);
77                                 syslog(priority, "%s", message);
78                         }
79 #endif
80                         break;
81         }
82
83         va_end(ap);
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 }