formatting and skipping openlog call for FTOCKS_LOG
[radsecproxy.git] / debug.c
1 /*
2  * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3  * Copyright (C) 2010 NORDUnet A/S
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  */
9
10 #ifndef SYS_SOLARIS9
11 #include <stdint.h>
12 #endif
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <strings.h>
18 #include <time.h>
19 #include <sys/time.h>
20 #include <syslog.h>
21 #include <errno.h>
22 #include <assert.h>
23 #include "debug.h"
24 #include "util.h"
25
26 static char *debug_ident = NULL;
27 static uint8_t debug_level = DBG_INFO;
28 static char *debug_filepath = NULL;
29 static FILE *debug_file = NULL;
30 static int debug_syslogfacility = 0;
31 #if defined(WANT_FTICKS)
32 static int fticks_syslogfacility = 0;
33 #endif
34 static uint8_t debug_timestamp = 0;
35
36 void debug_init(char *ident) {
37     debug_file = stderr;
38     setvbuf(debug_file, NULL, _IONBF, 0);
39     debug_ident = ident;
40 }
41
42 void debug_set_level(uint8_t level) {
43     switch (level) {
44     case 1:
45         debug_level = DBG_ERR;
46         return;
47     case 2:
48         debug_level = DBG_WARN;
49         return;
50     case 3:
51         debug_level = DBG_NOTICE;
52         return;
53     case 4:
54         debug_level = DBG_INFO;
55         return;
56     case 5:
57         debug_level = DBG_DBG;
58         return;
59     }
60 }
61
62 void debug_timestamp_on() {
63     debug_timestamp = 1;
64 }
65
66 uint8_t debug_get_level() {
67     return debug_level;
68 }
69
70 int debug_set_destination(char *dest, int log_type) {
71     static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
72         "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
73         "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
74     static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
75         LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
76         LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
77     extern int errno;
78     int i;
79
80     if (!strncasecmp(dest, "file:///", 8)) {
81         if (log_type!=FTICKS_LOG) {
82             debug_filepath = stringcopy(dest + 7, 0);
83             debug_file = fopen(debug_filepath, "a");
84             if (!debug_file) {
85                 debug_file = stderr;
86                 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
87                    debug_filepath, strerror(errno));
88             }
89             setvbuf(debug_file, NULL, _IONBF, 0);
90         } else {
91             debug(DBG_WARN, "FTicksSyslogFacility starting with file:/// not permitted, assuming default F-Ticks destination");
92         }
93         return 1;
94     }
95     if (!strncasecmp(dest, "x-syslog://", 11) || (log_type==FTICKS_LOG)) {
96         if (!strncasecmp(dest, "x-syslog://", 11)) {
97                 dest += 11;
98                 if (*dest == '/')
99                   dest++;
100         }
101         if (*dest) {
102             for (i = 0; facstrings[i]; i++)
103                 if (!strcasecmp(dest, facstrings[i]))
104                     break;
105             if (!facstrings[i])
106                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
107 #if defined(WANT_FTICKS)
108             if (log_type==FTICKS_LOG)
109                 fticks_syslogfacility = facvals[i];
110 #endif
111             if (log_type!=FTICKS_LOG)
112                 debug_syslogfacility = facvals[i];
113         } else {
114 #if defined(WANT_FTICKS)
115                 if (log_type==FTICKS_LOG)
116                    fticks_syslogfacility = 0;
117 #endif
118                 if (log_type!=FTICKS_LOG)
119                    debug_syslogfacility = LOG_DAEMON;
120         }
121         openlog(debug_ident, LOG_PID, debug_syslogfacility);
122         return 1;
123     }
124     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
125     exit(1);
126 }
127
128 void debug_reopen_log() {
129     extern int errno;
130
131     /* not a file, noop, return success */
132     if (!debug_filepath) {
133         debug(DBG_ERR, "skipping reopen");
134         return;
135     }
136
137     if (debug_file != stderr)
138         fclose(debug_file);
139
140     debug_file = fopen(debug_filepath, "a");
141     if (debug_file)
142         debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
143     else {
144         debug_file = stderr;
145         debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
146               debug_filepath, strerror(errno));
147     }
148     setvbuf(debug_file, NULL, _IONBF, 0);
149 }
150
151 void debug_logit(uint8_t level, const char *format, va_list ap) {
152     struct timeval now;
153     char *timebuf;
154     int priority;
155
156     if (debug_syslogfacility) {
157         switch (level) {
158         case DBG_DBG:
159             priority = LOG_DEBUG;
160             break;
161         case DBG_INFO:
162             priority = LOG_INFO;
163             break;
164         case DBG_NOTICE:
165             priority = LOG_NOTICE;
166             break;
167         case DBG_WARN:
168             priority = LOG_WARNING;
169             break;
170         case DBG_ERR:
171             priority = LOG_ERR;
172             break;
173         default:
174             priority = LOG_DEBUG;
175         }
176         vsyslog(priority, format, ap);
177     } else {
178         if (debug_timestamp && (timebuf = malloc(256))) {
179             gettimeofday(&now, NULL);
180             ctime_r(&now.tv_sec, timebuf);
181             timebuf[strlen(timebuf) - 1] = '\0';
182             fprintf(debug_file, "%s: ", timebuf + 4);
183             free(timebuf);
184         }
185         vfprintf(debug_file, format, ap);
186         fprintf(debug_file, "\n");
187     }
188 }
189
190 void debug(uint8_t level, char *format, ...) {
191     va_list ap;
192     if (level < debug_level)
193         return;
194     va_start(ap, format);
195     debug_logit(level, format, ap);
196     va_end(ap);
197 }
198
199 void debugx(int status, uint8_t level, char *format, ...) {
200     if (level >= debug_level) {
201         va_list ap;
202         va_start(ap, format);
203         debug_logit(level, format, ap);
204         va_end(ap);
205     }
206     exit(status);
207 }
208
209 void debugerrno(int err, uint8_t level, char *format, ...) {
210     if (level >= debug_level) {
211         va_list ap;
212         size_t len = strlen(format);
213         char *tmp = malloc(len + 1024 + 2);
214         assert(tmp);
215         strcpy(tmp, format);
216         tmp[len++] = ':';
217         tmp[len++] = ' ';
218         if (strerror_r(err, tmp + len, 1024))
219             tmp = format;
220         va_start(ap, format);
221         debug_logit(level, tmp, ap);
222         va_end(ap);
223     }
224 }
225
226 void debugerrnox(int err, uint8_t level, char *format, ...) {
227     if (level >= debug_level) {
228         va_list ap;
229         va_start(ap, format);
230         debugerrno(err, level, format, ap);
231         va_end(ap);
232     }
233     exit(err);
234 }
235
236 #if defined(WANT_FTICKS)
237 void fticks_debug(const char *format, ...) {
238     int priority;
239     va_list ap;
240     va_start(ap, format);
241     if (!debug_syslogfacility && !fticks_syslogfacility)
242         debug_logit(0xff, format, ap);
243     else {
244         priority = LOG_DEBUG|fticks_syslogfacility;
245         vsyslog(priority, format, ap);
246         va_end(ap);
247     }
248 }
249 #endif
250 /* Local Variables: */
251 /* c-file-style: "stroustrup" */
252 /* End: */