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) {
43 if (!strncasecmp(dest, "file:///", 8)) {
44 debug_file = fopen(dest + 7, "a");
46 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
47 dest + 7, strerror(errno));
48 setvbuf(debug_file, NULL, _IONBF, 0);
51 if (!strcasecmp(dest, "x-syslog://")) {
52 debug_syslogfacility = LOG_DAEMON;
53 openlog(debug_ident, LOG_PID, debug_syslogfacility);
59 void debug_logit(uint8_t level, const char *format, va_list ap) {
64 if (debug_syslogfacility) {
70 priority = LOG_WARNING;
78 vsyslog(priority, format, ap);
80 timebuf = malloc(256);
82 gettimeofday(&now, NULL);
83 ctime_r(&now.tv_sec, timebuf);
84 timebuf[strlen(timebuf) - 1] = '\0';
85 fprintf(debug_file, "%s: ", timebuf);
88 vfprintf(debug_file, format, ap);
89 fprintf(debug_file, "\n");
93 void debug(uint8_t level, char *format, ...) {
95 if (level < debug_level)
98 debug_logit(level, format, ap);
102 void debugx(int status, uint8_t level, char *format, ...) {
103 if (level >= debug_level) {
105 va_start(ap, format);
106 debug_logit(level, format, ap);