added logging to file and syslog (need to add support for specifying facility)
[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     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     extern int errno;
42     
43     if (!strncasecmp(dest, "file:///", 8)) {
44         debug_file = fopen(dest + 7, "a");
45         if (!debug_file)
46             debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
47                    dest + 7, strerror(errno));
48         setvbuf(debug_file, NULL, _IONBF, 0);
49         return 1;
50     }
51     if (!strcasecmp(dest, "x-syslog://")) {
52         debug_syslogfacility = LOG_DAEMON;
53         openlog(debug_ident, LOG_PID, debug_syslogfacility);
54         return 1;
55     }
56     return 0;
57 }
58
59 void debug_logit(uint8_t level, const char *format, va_list ap) {
60     struct timeval now;
61     char *timebuf;
62     int priority;
63     
64     if (debug_syslogfacility) {
65         switch (level) {
66         case DBG_INFO:
67             priority = LOG_INFO;
68             break;
69         case DBG_WARN:
70             priority = LOG_WARNING;
71             break;
72         case DBG_ERR:
73             priority = LOG_ERR;
74             break;
75         default:
76             priority = LOG_DEBUG;
77         }
78         vsyslog(priority, format, ap);
79     } else {
80         timebuf = malloc(256);
81         if (timebuf) {
82             gettimeofday(&now, NULL);
83             ctime_r(&now.tv_sec, timebuf);
84             timebuf[strlen(timebuf) - 1] = '\0';
85             fprintf(debug_file, "%s: ", timebuf);
86             free(timebuf);
87         }
88         vfprintf(debug_file, format, ap);
89         fprintf(debug_file, "\n");
90     }
91 }
92
93 void debug(uint8_t level, char *format, ...) {
94     va_list ap;
95     if (level < debug_level)
96         return;
97     va_start(ap, format);
98     debug_logit(level, format, ap);
99     va_end(ap);
100 }
101
102 void debugx(int status, uint8_t level, char *format, ...) {
103     if (level >= debug_level) {
104         va_list ap;
105         va_start(ap, format);
106         debug_logit(level, format, ap);
107         va_end(ap);
108     }
109     exit(status);
110 }