Detach as default, options for foreground and debug level
[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_WARN;
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     }
44 }
45
46 uint8_t debug_get_level() {
47     return debug_level;
48 }
49
50 int debug_set_destination(char *dest) {
51     static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
52                                         "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
53                                         "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
54     static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
55                                    LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
56                                    LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
57     extern int errno;
58     int i;
59     
60     if (!strncasecmp(dest, "file:///", 8)) {
61         debug_file = fopen(dest + 7, "a");
62         if (!debug_file)
63             debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
64                    dest + 7, strerror(errno));
65         setvbuf(debug_file, NULL, _IONBF, 0);
66         return 1;
67     }
68     if (!strncasecmp(dest, "x-syslog://", 11)) {
69         dest += 11;
70         if (*dest) {
71             for (i = 0; facstrings[i]; i++)
72                 if (!strcasecmp(dest, facstrings[i]))
73                     break;
74             if (!facstrings[i])
75                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
76             debug_syslogfacility = facvals[i];
77         } else
78             debug_syslogfacility = LOG_DAEMON;
79         openlog(debug_ident, LOG_PID, debug_syslogfacility);
80         return 1;
81     }
82     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
83     exit(1);
84 }
85
86 void debug_logit(uint8_t level, const char *format, va_list ap) {
87     struct timeval now;
88     char *timebuf;
89     int priority;
90     
91     if (debug_syslogfacility) {
92         switch (level) {
93         case DBG_INFO:
94             priority = LOG_INFO;
95             break;
96         case DBG_WARN:
97             priority = LOG_WARNING;
98             break;
99         case DBG_ERR:
100             priority = LOG_ERR;
101             break;
102         default:
103             priority = LOG_DEBUG;
104         }
105         vsyslog(priority, format, ap);
106     } else {
107         timebuf = malloc(256);
108         if (timebuf) {
109             gettimeofday(&now, NULL);
110             ctime_r(&now.tv_sec, timebuf);
111             timebuf[strlen(timebuf) - 1] = '\0';
112             fprintf(debug_file, "%s: ", timebuf);
113             free(timebuf);
114         }
115         vfprintf(debug_file, format, ap);
116         fprintf(debug_file, "\n");
117     }
118 }
119
120 void debug(uint8_t level, char *format, ...) {
121     va_list ap;
122     if (level < debug_level)
123         return;
124     va_start(ap, format);
125     debug_logit(level, format, ap);
126     va_end(ap);
127 }
128
129 void debugx(int status, uint8_t level, char *format, ...) {
130     if (level >= debug_level) {
131         va_list ap;
132         va_start(ap, format);
133         debug_logit(level, format, ap);
134         va_end(ap);
135     }
136     exit(status);
137 }