]> git.meshlink.io Git - meshlink/blob - src/logger.c
6ea688466562c01678a50a315ffc01ec73c47516
[meshlink] / 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.2 2003/08/24 20:38:24 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 #ifdef HAVE_MINGW
34 static HANDLE loghandle = NULL;
35 #endif
36 static const char *logident = NULL;
37
38 void openlogger(const char *ident, logmode_t mode) {
39         logident = ident;
40         logmode = mode;
41         
42         switch(mode) {
43                 case LOGMODE_STDERR:
44                         logpid = getpid();
45                         break;
46                 case LOGMODE_FILE:
47                         logpid = getpid();
48                         logfile = fopen(logfilename, "a");
49                         if(!logfile)
50                                 logmode = LOGMODE_NULL;
51                         break;
52                 case LOGMODE_SYSLOG:
53 #ifdef HAVE_MINGW
54                         loghandle = RegisterEventSource(NULL, logident);
55                         if(!loghandle)
56                                 logmode = LOGMODE_NULL;
57                         break;
58 #else
59 #ifdef HAVE_SYSLOG_H
60                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
61                         break;
62 #endif
63 #endif
64                 case LOGMODE_NULL:
65                         break;
66         }
67 }
68
69 void logger(int priority, const char *format, ...) {
70         va_list ap;
71
72         va_start(ap, format);
73
74         switch(logmode) {
75                 case LOGMODE_STDERR:
76                         vfprintf(stderr, format, ap);
77                         fprintf(stderr, "\n");
78                         fflush(stderr);
79                         break;
80                 case LOGMODE_FILE:
81                         fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
82                         vfprintf(logfile, format, ap);
83                         fprintf(logfile, "\n");
84                         fflush(logfile);
85                         break;
86                 case LOGMODE_SYSLOG:
87 #ifdef HAVE_MINGW
88                         {
89                                 char message[4096];
90                                 char *messages[] = {message};
91                                 vsnprintf(message, sizeof(message), format, ap);
92                                 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
93                         }
94 #else
95 #ifdef HAVE_SYSLOG_H
96 #ifdef HAVE_VSYSLOG
97                         vsyslog(priority, format, ap);
98 #else
99                         {
100                                 char message[4096];
101                                 vsnprintf(message, sizeof(message), format, ap);
102                                 syslog(priority, "%s", message);
103                         }
104 #endif
105                         break;
106 #endif
107 #endif
108                 case LOGMODE_NULL:
109                         break;
110         }
111
112         va_end(ap);
113 }
114
115 void closelogger(void) {
116         switch(logmode) {
117                 case LOGMODE_FILE:
118                         fclose(logfile);
119                         break;
120                 case LOGMODE_SYSLOG:
121 #ifdef HAVE_MINGW
122                         DeregisterEventSource(loghandle);
123                         break;
124 #else
125 #ifdef HAVE_SYSLOG_H
126                         closelog();
127                         break;
128 #endif
129 #endif
130                 case LOGMODE_NULL:
131                 case LOGMODE_STDERR:
132                         break;
133                         break;
134         }
135 }