1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2 * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3 * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
22 static char *debug_ident = NULL;
23 static uint8_t debug_level = DBG_INFO;
24 static char *debug_filepath = NULL;
25 static FILE *debug_file = NULL;
26 static int debug_syslogfacility = 0;
27 #if defined(WANT_FTICKS)
28 static int fticks_syslogfacility = 0;
30 static uint8_t debug_timestamp = 0;
32 void debug_init(char *ident) {
34 setvbuf(debug_file, NULL, _IONBF, 0);
38 void debug_set_level(uint8_t level) {
41 debug_level = DBG_ERR;
44 debug_level = DBG_WARN;
47 debug_level = DBG_NOTICE;
50 debug_level = DBG_INFO;
53 debug_level = DBG_DBG;
58 void debug_timestamp_on() {
62 uint8_t debug_get_level() {
66 int debug_set_destination(char *dest, int log_type) {
67 static const char *facstrings[] = {
68 "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
69 "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
70 "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
71 static const int facvals[] = {
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 };
78 if (!strncasecmp(dest, "file:///", 8)) {
79 if (log_type != LOG_TYPE_FTICKS) {
80 debug_filepath = stringcopy(dest + 7, 0);
81 debug_file = fopen(debug_filepath, "a");
84 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
85 debug_filepath, strerror(errno));
87 setvbuf(debug_file, NULL, _IONBF, 0);
89 debug(DBG_WARN, "FTicksSyslogFacility starting with file:/// not "
90 "permitted, assuming default F-Ticks destination");
94 if (!strncasecmp(dest, "x-syslog://", 11) || log_type == LOG_TYPE_FTICKS) {
95 if (!strncasecmp(dest, "x-syslog://", 11)) {
101 for (i = 0; facstrings[i]; i++)
102 if (!strcasecmp(dest, facstrings[i]))
105 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
106 if (log_type != LOG_TYPE_FTICKS)
107 debug_syslogfacility = facvals[i];
108 #if defined(WANT_FTICKS)
109 else if (log_type == LOG_TYPE_FTICKS)
110 fticks_syslogfacility = facvals[i];
113 if (log_type != LOG_TYPE_FTICKS)
114 debug_syslogfacility = LOG_DAEMON;
115 #if defined(WANT_FTICKS)
116 else if (log_type == LOG_TYPE_FTICKS)
117 fticks_syslogfacility = 0;
120 openlog(debug_ident, LOG_PID, debug_syslogfacility);
123 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
127 void debug_reopen_log() {
130 /* not a file, noop, return success */
131 if (!debug_filepath) {
132 debug(DBG_ERR, "skipping reopen");
136 if (debug_file != stderr)
139 debug_file = fopen(debug_filepath, "a");
141 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
144 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
145 debug_filepath, strerror(errno));
147 setvbuf(debug_file, NULL, _IONBF, 0);
150 void debug_logit(uint8_t level, const char *format, va_list ap) {
155 if (debug_syslogfacility) {
158 priority = LOG_DEBUG;
164 priority = LOG_NOTICE;
167 priority = LOG_WARNING;
173 priority = LOG_DEBUG;
175 vsyslog(priority, format, ap);
177 if (debug_timestamp && (timebuf = malloc(256))) {
178 gettimeofday(&now, NULL);
179 ctime_r(&now.tv_sec, timebuf);
180 timebuf[strlen(timebuf) - 1] = '\0';
181 fprintf(debug_file, "%s: ", timebuf + 4);
184 vfprintf(debug_file, format, ap);
185 fprintf(debug_file, "\n");
189 void debug(uint8_t level, char *format, ...) {
191 if (level < debug_level)
193 va_start(ap, format);
194 debug_logit(level, format, ap);
198 void debugx(int status, uint8_t level, char *format, ...) {
199 if (level >= debug_level) {
201 va_start(ap, format);
202 debug_logit(level, format, ap);
208 void debugerrno(int err, uint8_t level, char *format, ...) {
209 if (level >= debug_level) {
211 size_t len = strlen(format);
212 char *tmp = malloc(len + 1024 + 2);
217 if (strerror_r(err, tmp + len, 1024))
219 va_start(ap, format);
220 debug_logit(level, tmp, ap);
225 void debugerrnox(int err, uint8_t level, char *format, ...) {
226 if (level >= debug_level) {
228 va_start(ap, format);
229 debugerrno(err, level, format, ap);
235 #if defined(WANT_FTICKS)
236 void fticks_debug(const char *format, ...) {
239 va_start(ap, format);
240 if (!debug_syslogfacility && !fticks_syslogfacility)
241 debug_logit(0xff, format, ap);
243 priority = LOG_DEBUG | fticks_syslogfacility;
244 vsyslog(priority, format, ap);
249 /* Local Variables: */
250 /* c-file-style: "stroustrup" */