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