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