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.
24 static char *debug_ident = NULL;
25 static uint8_t debug_level = DBG_INFO;
26 static char *debug_filepath = NULL;
27 static FILE *debug_file = NULL;
28 static int debug_syslogfacility = 0;
29 static uint8_t debug_timestamp = 0;
31 void debug_init(char *ident) {
33 setvbuf(debug_file, NULL, _IONBF, 0);
37 void debug_set_level(uint8_t level) {
40 debug_level = DBG_ERR;
43 debug_level = DBG_WARN;
46 debug_level = DBG_INFO;
49 debug_level = DBG_DBG;
54 void debug_timestamp_on() {
58 uint8_t debug_get_level() {
62 int debug_set_destination(char *dest) {
63 static const char *facstrings[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
64 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
65 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
66 static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
67 LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
68 LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
72 if (!strncasecmp(dest, "file:///", 8)) {
73 debug_filepath = stringcopy(dest + 7, 0);
74 debug_file = fopen(debug_filepath, "a");
77 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
78 debug_filepath, strerror(errno));
80 setvbuf(debug_file, NULL, _IONBF, 0);
83 if (!strncasecmp(dest, "x-syslog://", 11)) {
88 for (i = 0; facstrings[i]; i++)
89 if (!strcasecmp(dest, facstrings[i]))
92 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
93 debug_syslogfacility = facvals[i];
95 debug_syslogfacility = LOG_DAEMON;
96 openlog(debug_ident, LOG_PID, debug_syslogfacility);
99 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
103 void debug_reopen_log() {
106 /* not a file, noop, return success */
107 if (!debug_filepath) {
108 debug(DBG_ERR, "skipping reopen");
112 if (debug_file != stderr)
115 debug_file = fopen(debug_filepath, "a");
117 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
120 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
121 debug_filepath, strerror(errno));
123 setvbuf(debug_file, NULL, _IONBF, 0);
126 void debug_logit(uint8_t level, const char *format, va_list ap) {
131 if (debug_syslogfacility) {
134 priority = LOG_DEBUG;
140 priority = LOG_WARNING;
146 priority = LOG_DEBUG;
148 vsyslog(priority, format, ap);
150 if (debug_timestamp && (timebuf = malloc(256))) {
151 gettimeofday(&now, NULL);
152 ctime_r(&now.tv_sec, timebuf);
153 timebuf[strlen(timebuf) - 1] = '\0';
154 fprintf(debug_file, "%s: ", timebuf + 4);
157 vfprintf(debug_file, format, ap);
158 fprintf(debug_file, "\n");
162 void debug(uint8_t level, char *format, ...) {
164 if (level < debug_level)
166 va_start(ap, format);
167 debug_logit(level, format, ap);
171 void debugx(int status, uint8_t level, char *format, ...) {
172 if (level >= debug_level) {
174 va_start(ap, format);
175 debug_logit(level, format, ap);