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