2 * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3 * Copyright (C) 2010,2011 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;
36 void debug_init(char *ident) {
38 setvbuf(debug_file, NULL, _IONBF, 0);
42 void debug_set_level(uint8_t level) {
45 debug_level = DBG_ERR;
48 debug_level = DBG_WARN;
51 debug_level = DBG_NOTICE;
54 debug_level = DBG_INFO;
57 debug_level = DBG_DBG;
62 void debug_timestamp_on() {
66 uint8_t debug_get_level() {
70 int debug_set_destination(char *dest, int log_type) {
71 static const char *facstrings[] = {
72 "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
73 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
74 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
75 static const int facvals[] = {
76 LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
77 LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
78 LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
82 if (!strncasecmp(dest, "file:///", 8)) {
83 if (log_type != LOG_TYPE_FTICKS) {
84 debug_filepath = stringcopy(dest + 7, 0);
85 debug_file = fopen(debug_filepath, "a");
88 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
89 debug_filepath, strerror(errno));
91 setvbuf(debug_file, NULL, _IONBF, 0);
93 debug(DBG_WARN, "FTicksSyslogFacility starting with file:/// not "
94 "permitted, assuming default F-Ticks destination");
98 if (!strncasecmp(dest, "x-syslog://", 11) || log_type == LOG_TYPE_FTICKS) {
99 if (!strncasecmp(dest, "x-syslog://", 11)) {
105 for (i = 0; facstrings[i]; i++)
106 if (!strcasecmp(dest, facstrings[i]))
109 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
110 if (log_type != LOG_TYPE_FTICKS)
111 debug_syslogfacility = facvals[i];
112 #if defined(WANT_FTICKS)
113 else if (log_type == LOG_TYPE_FTICKS)
114 fticks_syslogfacility = facvals[i];
117 if (log_type != LOG_TYPE_FTICKS)
118 debug_syslogfacility = LOG_DAEMON;
119 #if defined(WANT_FTICKS)
120 else if (log_type == LOG_TYPE_FTICKS)
121 fticks_syslogfacility = 0;
124 openlog(debug_ident, LOG_PID, debug_syslogfacility);
127 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
131 void debug_reopen_log() {
134 /* not a file, noop, return success */
135 if (!debug_filepath) {
136 debug(DBG_ERR, "skipping reopen");
140 if (debug_file != stderr)
143 debug_file = fopen(debug_filepath, "a");
145 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
148 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
149 debug_filepath, strerror(errno));
151 setvbuf(debug_file, NULL, _IONBF, 0);
154 void debug_logit(uint8_t level, const char *format, va_list ap) {
159 if (debug_syslogfacility) {
162 priority = LOG_DEBUG;
168 priority = LOG_NOTICE;
171 priority = LOG_WARNING;
177 priority = LOG_DEBUG;
179 vsyslog(priority, format, ap);
181 if (debug_timestamp && (timebuf = malloc(256))) {
182 gettimeofday(&now, NULL);
183 ctime_r(&now.tv_sec, timebuf);
184 timebuf[strlen(timebuf) - 1] = '\0';
185 fprintf(debug_file, "%s: ", timebuf + 4);
188 vfprintf(debug_file, format, ap);
189 fprintf(debug_file, "\n");
193 void debug(uint8_t level, char *format, ...) {
195 if (level < debug_level)
197 va_start(ap, format);
198 debug_logit(level, format, ap);
202 void debugx(int status, uint8_t level, char *format, ...) {
203 if (level >= debug_level) {
205 va_start(ap, format);
206 debug_logit(level, format, ap);
212 void debugerrno(int err, uint8_t level, char *format, ...) {
213 if (level >= debug_level) {
215 size_t len = strlen(format);
216 char *tmp = malloc(len + 1024 + 2);
221 if (strerror_r(err, tmp + len, 1024))
223 va_start(ap, format);
224 debug_logit(level, tmp, ap);
229 void debugerrnox(int err, uint8_t level, char *format, ...) {
230 if (level >= debug_level) {
232 va_start(ap, format);
233 debugerrno(err, level, format, ap);
239 #if defined(WANT_FTICKS)
240 void fticks_debug(const char *format, ...) {
243 va_start(ap, format);
244 if (!debug_syslogfacility && !fticks_syslogfacility)
245 debug_logit(0xff, format, ap);
247 priority = LOG_DEBUG | fticks_syslogfacility;
248 vsyslog(priority, format, ap);
253 /* Local Variables: */
254 /* c-file-style: "stroustrup" */