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