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