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_INFO;
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) {
35 debug_level = DBG_ERR;
38 debug_level = DBG_WARN;
41 debug_level = DBG_INFO;
44 debug_level = DBG_DBG;
49 uint8_t debug_get_level() {
53 int debug_set_destination(char *dest) {
54 static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
55 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
56 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
57 static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
58 LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
59 LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
63 if (!strncasecmp(dest, "file:///", 8)) {
64 debug_file = fopen(dest + 7, "a");
66 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
67 dest + 7, strerror(errno));
68 setvbuf(debug_file, NULL, _IONBF, 0);
71 if (!strncasecmp(dest, "x-syslog://", 11)) {
74 for (i = 0; facstrings[i]; i++)
75 if (!strcasecmp(dest, facstrings[i]))
78 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
79 debug_syslogfacility = facvals[i];
81 debug_syslogfacility = LOG_DAEMON;
82 openlog(debug_ident, LOG_PID, debug_syslogfacility);
85 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
89 void debug_logit(uint8_t level, const char *format, va_list ap) {
94 if (debug_syslogfacility) {
103 priority = LOG_WARNING;
109 priority = LOG_DEBUG;
111 vsyslog(priority, format, ap);
113 timebuf = malloc(256);
115 gettimeofday(&now, NULL);
116 ctime_r(&now.tv_sec, timebuf);
117 timebuf[strlen(timebuf) - 1] = '\0';
118 fprintf(debug_file, "%s: ", timebuf);
121 vfprintf(debug_file, format, ap);
122 fprintf(debug_file, "\n");
126 void debug(uint8_t level, char *format, ...) {
128 if (level < debug_level)
130 va_start(ap, format);
131 debug_logit(level, format, ap);
135 void debugx(int status, uint8_t level, char *format, ...) {
136 if (level >= debug_level) {
138 va_start(ap, format);
139 debug_logit(level, format, ap);