2 * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3 * Copyright (C) 2010 NORDUnet A/S
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
26 static char *debug_ident = NULL;
27 static uint8_t debug_level = DBG_INFO;
28 static char *debug_filepath = NULL;
29 static FILE *debug_file = NULL;
30 static int debug_syslogfacility = 0;
31 #if defined(WANT_FTICKS)
32 static int fticks_syslogfacility = 0;
34 static uint8_t debug_timestamp = 0;
35 static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
36 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
37 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
38 static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
39 LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
40 LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
42 void debug_init(char *ident) {
44 setvbuf(debug_file, NULL, _IONBF, 0);
48 void debug_set_level(uint8_t level) {
51 debug_level = DBG_ERR;
54 debug_level = DBG_WARN;
57 debug_level = DBG_NOTICE;
60 debug_level = DBG_INFO;
63 debug_level = DBG_DBG;
68 void debug_timestamp_on() {
72 uint8_t debug_get_level() {
76 #if defined(WANT_FTICKS)
77 int debug_set_ftickssyslogfacility(char *dest) {
79 if (!strncasecmp(dest, "x-syslog://", 11)) {
85 for (i = 0; facstrings[i]; i++)
86 if (!strcasecmp(dest, facstrings[i]))
89 debug(DBG_ERR, "Unknown syslog facility %s for F-Ticks, assuming default", dest);
90 fticks_syslogfacility = 0;
92 fticks_syslogfacility = facvals[i];
94 fticks_syslogfacility = 0;
96 if (fticks_syslogfacility && !debug_syslogfacility) {
97 openlog(debug_ident, LOG_PID, fticks_syslogfacility);
103 int debug_set_destination(char *dest) {
107 if (!strncasecmp(dest, "file:///", 8)) {
108 debug_filepath = stringcopy(dest + 7, 0);
109 debug_file = fopen(debug_filepath, "a");
112 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
113 debug_filepath, strerror(errno));
115 setvbuf(debug_file, NULL, _IONBF, 0);
118 if (!strncasecmp(dest, "x-syslog://", 11)) {
123 for (i = 0; facstrings[i]; i++)
124 if (!strcasecmp(dest, facstrings[i]))
127 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
128 debug_syslogfacility = facvals[i];
130 debug_syslogfacility = LOG_DAEMON;
131 openlog(debug_ident, LOG_PID, debug_syslogfacility);
134 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
138 void debug_reopen_log() {
141 /* not a file, noop, return success */
142 if (!debug_filepath) {
143 debug(DBG_ERR, "skipping reopen");
147 if (debug_file != stderr)
150 debug_file = fopen(debug_filepath, "a");
152 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
155 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
156 debug_filepath, strerror(errno));
158 setvbuf(debug_file, NULL, _IONBF, 0);
161 void debug_logit(uint8_t level, const char *format, va_list ap) {
166 if (debug_syslogfacility) {
169 priority = LOG_DEBUG;
175 priority = LOG_NOTICE;
178 priority = LOG_WARNING;
184 priority = LOG_DEBUG;
186 vsyslog(priority, format, ap);
188 if (debug_timestamp && (timebuf = malloc(256))) {
189 gettimeofday(&now, NULL);
190 ctime_r(&now.tv_sec, timebuf);
191 timebuf[strlen(timebuf) - 1] = '\0';
192 fprintf(debug_file, "%s: ", timebuf + 4);
195 vfprintf(debug_file, format, ap);
196 fprintf(debug_file, "\n");
200 void debug(uint8_t level, char *format, ...) {
202 if (level < debug_level)
204 va_start(ap, format);
205 debug_logit(level, format, ap);
209 void debugx(int status, uint8_t level, char *format, ...) {
210 if (level >= debug_level) {
212 va_start(ap, format);
213 debug_logit(level, format, ap);
219 void debugerrno(int err, uint8_t level, char *format, ...) {
220 if (level >= debug_level) {
222 size_t len = strlen(format);
223 char *tmp = malloc(len + 1024 + 2);
228 if (strerror_r(err, tmp + len, 1024))
230 va_start(ap, format);
231 debug_logit(level, tmp, ap);
236 void debugerrnox(int err, uint8_t level, char *format, ...) {
237 if (level >= debug_level) {
239 va_start(ap, format);
240 debugerrno(err, level, format, ap);
246 #if defined(WANT_FTICKS)
247 void fticks_debug(const char *format, ...) {
250 va_start(ap, format);
251 if (!debug_syslogfacility && !fticks_syslogfacility)
252 debug_logit(0xff, format, ap);
254 if (fticks_syslogfacility) {
255 priority = LOG_DEBUG|fticks_syslogfacility;
257 priority = LOG_DEBUG;
259 vsyslog(priority, format, ap);
264 /* Local Variables: */
265 /* c-file-style: "stroustrup" */