fixed syslog url syntax, updated example config and manpage
[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             debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
67                    dest + 7, strerror(errno));
68         setvbuf(debug_file, NULL, _IONBF, 0);
69         return 1;
70     }
71     if (!strncasecmp(dest, "x-syslog://", 11)) {
72         dest += 11;
73         if (*dest == '/')
74             dest++;
75         if (*dest) {
76             for (i = 0; facstrings[i]; i++)
77                 if (!strcasecmp(dest, facstrings[i]))
78                     break;
79             if (!facstrings[i])
80                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
81             debug_syslogfacility = facvals[i];
82         } else
83             debug_syslogfacility = LOG_DAEMON;
84         openlog(debug_ident, LOG_PID, debug_syslogfacility);
85         return 1;
86     }
87     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
88     exit(1);
89 }
90
91 void debug_logit(uint8_t level, const char *format, va_list ap) {
92     struct timeval now;
93     char *timebuf;
94     int priority;
95     
96     if (debug_syslogfacility) {
97         switch (level) {
98         case DBG_DBG:
99             priority = LOG_DEBUG;
100             break;
101         case DBG_INFO:
102             priority = LOG_INFO;
103             break;
104         case DBG_WARN:
105             priority = LOG_WARNING;
106             break;
107         case DBG_ERR:
108             priority = LOG_ERR;
109             break;
110         default:
111             priority = LOG_DEBUG;
112         }
113         vsyslog(priority, format, ap);
114     } else {
115         timebuf = malloc(256);
116         if (timebuf) {
117             gettimeofday(&now, NULL);
118             ctime_r(&now.tv_sec, timebuf);
119             timebuf[strlen(timebuf) - 1] = '\0';
120             fprintf(debug_file, "%s: ", timebuf);
121             free(timebuf);
122         }
123         vfprintf(debug_file, format, ap);
124         fprintf(debug_file, "\n");
125     }
126 }
127
128 void debug(uint8_t level, char *format, ...) {
129     va_list ap;
130     if (level < debug_level)
131         return;
132     va_start(ap, format);
133     debug_logit(level, format, ap);
134     va_end(ap);
135 }
136
137 void debugx(int status, uint8_t level, char *format, ...) {
138     if (level >= debug_level) {
139         va_list ap;
140         va_start(ap, format);
141         debug_logit(level, format, ap);
142         va_end(ap);
143     }
144     exit(status);
145 }