2 * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
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.
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;
26 void debug_init(char *ident) {
28 setvbuf(debug_file, NULL, _IONBF, 0);
32 void debug_set_level(uint8_t level) {
36 uint8_t debug_get_level() {
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 };
50 if (!strncasecmp(dest, "file:///", 8)) {
51 debug_file = fopen(dest + 7, "a");
53 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
54 dest + 7, strerror(errno));
55 setvbuf(debug_file, NULL, _IONBF, 0);
58 if (!strncasecmp(dest, "x-syslog://", 11)) {
61 for (i = 0; facstrings[i]; i++)
62 if (!strcasecmp(dest, facstrings[i]))
65 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
66 debug_syslogfacility = facvals[i];
68 debug_syslogfacility = LOG_DAEMON;
69 openlog(debug_ident, LOG_PID, debug_syslogfacility);
72 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
76 void debug_logit(uint8_t level, const char *format, va_list ap) {
81 if (debug_syslogfacility) {
87 priority = LOG_WARNING;
95 vsyslog(priority, format, ap);
97 timebuf = malloc(256);
99 gettimeofday(&now, NULL);
100 ctime_r(&now.tv_sec, timebuf);
101 timebuf[strlen(timebuf) - 1] = '\0';
102 fprintf(debug_file, "%s: ", timebuf);
105 vfprintf(debug_file, format, ap);
106 fprintf(debug_file, "\n");
110 void debug(uint8_t level, char *format, ...) {
112 if (level < debug_level)
114 va_start(ap, format);
115 debug_logit(level, format, ap);
119 void debugx(int status, uint8_t level, char *format, ...) {
120 if (level >= debug_level) {
122 va_start(ap, format);
123 debug_logit(level, format, ap);