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