2 * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3 * Copyright (C) 2010 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[] = { "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
72 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
73 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
74 static const int facvals[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
75 LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
76 LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
80 if (!strncasecmp(dest, "file:///", 8)) {
81 if (log_type!=FTICKS_LOG) {
82 debug_filepath = stringcopy(dest + 7, 0);
83 debug_file = fopen(debug_filepath, "a");
86 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
87 debug_filepath, strerror(errno));
89 setvbuf(debug_file, NULL, _IONBF, 0);
91 debug(DBG_WARN, "FTicksSyslogFacility starting with file:/// not permitted, assuming default F-Ticks destination");
95 if (!strncasecmp(dest, "x-syslog://", 11) || (log_type==FTICKS_LOG)) {
96 if (!strncasecmp(dest, "x-syslog://", 11)) {
102 for (i = 0; facstrings[i]; i++)
103 if (!strcasecmp(dest, facstrings[i]))
106 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
107 if (log_type==FTICKS_LOG)
108 fticks_syslogfacility = facvals[i];
110 debug_syslogfacility = facvals[i];
112 if (log_type==FTICKS_LOG)
113 fticks_syslogfacility = 0;
115 debug_syslogfacility = LOG_DAEMON;
117 if (log_type==FTICKS_LOG) {
118 if (fticks_syslogfacility && !debug_syslogfacility) {
119 openlog(debug_ident, LOG_PID, fticks_syslogfacility);
122 openlog(debug_ident, LOG_PID, debug_syslogfacility);
125 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
129 void debug_reopen_log() {
132 /* not a file, noop, return success */
133 if (!debug_filepath) {
134 debug(DBG_ERR, "skipping reopen");
138 if (debug_file != stderr)
141 debug_file = fopen(debug_filepath, "a");
143 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
146 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
147 debug_filepath, strerror(errno));
149 setvbuf(debug_file, NULL, _IONBF, 0);
152 void debug_logit(uint8_t level, const char *format, va_list ap) {
157 if (debug_syslogfacility) {
160 priority = LOG_DEBUG;
166 priority = LOG_NOTICE;
169 priority = LOG_WARNING;
175 priority = LOG_DEBUG;
177 vsyslog(priority, format, ap);
179 if (debug_timestamp && (timebuf = malloc(256))) {
180 gettimeofday(&now, NULL);
181 ctime_r(&now.tv_sec, timebuf);
182 timebuf[strlen(timebuf) - 1] = '\0';
183 fprintf(debug_file, "%s: ", timebuf + 4);
186 vfprintf(debug_file, format, ap);
187 fprintf(debug_file, "\n");
191 void debug(uint8_t level, char *format, ...) {
193 if (level < debug_level)
195 va_start(ap, format);
196 debug_logit(level, format, ap);
200 void debugx(int status, uint8_t level, char *format, ...) {
201 if (level >= debug_level) {
203 va_start(ap, format);
204 debug_logit(level, format, ap);
210 void debugerrno(int err, uint8_t level, char *format, ...) {
211 if (level >= debug_level) {
213 size_t len = strlen(format);
214 char *tmp = malloc(len + 1024 + 2);
219 if (strerror_r(err, tmp + len, 1024))
221 va_start(ap, format);
222 debug_logit(level, tmp, ap);
227 void debugerrnox(int err, uint8_t level, char *format, ...) {
228 if (level >= debug_level) {
230 va_start(ap, format);
231 debugerrno(err, level, format, ap);
237 #if defined(WANT_FTICKS)
238 void fticks_debug(const char *format, ...) {
241 va_start(ap, format);
242 if (!debug_syslogfacility && !fticks_syslogfacility)
243 debug_logit(0xff, format, ap);
245 priority = LOG_DEBUG|fticks_syslogfacility;
246 vsyslog(priority, format, ap);
251 /* Local Variables: */
252 /* c-file-style: "stroustrup" */