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