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