segfault when logging that couldn't open log file fixed, now log to stderr
[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 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <time.h>
16 #include <sys/time.h>
17 #include <syslog.h>
18 #include <errno.h>
19 #include "debug.h"
20
21 static char *debug_ident = NULL;
22 static uint8_t debug_level = DBG_INFO;
23 static FILE *debug_file = NULL;
24 static int debug_syslogfacility = 0;
25
26 void debug_init(char *ident) {
27     debug_file = stderr;
28     setvbuf(debug_file, NULL, _IONBF, 0);
29     debug_ident = ident;
30 }
31
32 void debug_set_level(uint8_t level) {
33     switch (level) {
34     case 1:
35         debug_level = DBG_ERR;
36         return;
37     case 2:
38         debug_level = DBG_WARN;
39         return;
40     case 3:
41         debug_level = DBG_INFO;
42         return;
43     case 4:
44         debug_level = DBG_DBG;
45         return;
46     }
47 }
48
49 uint8_t debug_get_level() {
50     return debug_level;
51 }
52
53 int debug_set_destination(char *dest) {
54     static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
55                                         "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
56                                         "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
57     static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
58                                    LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
59                                    LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
60     extern int errno;
61     int i;
62     
63     if (!strncasecmp(dest, "file:///", 8)) {
64         debug_file = fopen(dest + 7, "a");
65         if (!debug_file) {
66             debug_file = stderr;
67             debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
68                    dest + 7, strerror(errno));
69         }
70         setvbuf(debug_file, NULL, _IONBF, 0);
71         return 1;
72     }
73     if (!strncasecmp(dest, "x-syslog://", 11)) {
74         dest += 11;
75         if (*dest == '/')
76             dest++;
77         if (*dest) {
78             for (i = 0; facstrings[i]; i++)
79                 if (!strcasecmp(dest, facstrings[i]))
80                     break;
81             if (!facstrings[i])
82                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
83             debug_syslogfacility = facvals[i];
84         } else
85             debug_syslogfacility = LOG_DAEMON;
86         openlog(debug_ident, LOG_PID, debug_syslogfacility);
87         return 1;
88     }
89     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
90     exit(1);
91 }
92
93 void debug_logit(uint8_t level, const char *format, va_list ap) {
94     struct timeval now;
95     char *timebuf;
96     int priority;
97     
98     if (debug_syslogfacility) {
99         switch (level) {
100         case DBG_DBG:
101             priority = LOG_DEBUG;
102             break;
103         case DBG_INFO:
104             priority = LOG_INFO;
105             break;
106         case DBG_WARN:
107             priority = LOG_WARNING;
108             break;
109         case DBG_ERR:
110             priority = LOG_ERR;
111             break;
112         default:
113             priority = LOG_DEBUG;
114         }
115         vsyslog(priority, format, ap);
116     } else {
117         timebuf = malloc(256);
118         if (timebuf) {
119             gettimeofday(&now, NULL);
120             ctime_r(&now.tv_sec, timebuf);
121             timebuf[strlen(timebuf) - 1] = '\0';
122             fprintf(debug_file, "%s: ", timebuf);
123             free(timebuf);
124         }
125         vfprintf(debug_file, format, ap);
126         fprintf(debug_file, "\n");
127     }
128 }
129
130 void debug(uint8_t level, char *format, ...) {
131     va_list ap;
132     if (level < debug_level)
133         return;
134     va_start(ap, format);
135     debug_logit(level, format, ap);
136     va_end(ap);
137 }
138
139 void debugx(int status, uint8_t level, char *format, ...) {
140     if (level >= debug_level) {
141         va_list ap;
142         va_start(ap, format);
143         debug_logit(level, format, ap);
144         va_end(ap);
145     }
146     exit(status);
147 }