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