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[] = {
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 != FTICKS_LOG) {
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 == FTICKS_LOG) {
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 == FTICKS_LOG)
111 fticks_syslogfacility = facvals[i];
113 debug_syslogfacility = facvals[i];
115 if (log_type == FTICKS_LOG)
116 fticks_syslogfacility = 0;
118 debug_syslogfacility = LOG_DAEMON;
120 if (log_type == FTICKS_LOG) {
121 if (fticks_syslogfacility && !debug_syslogfacility) {
122 openlog(debug_ident, LOG_PID, fticks_syslogfacility);
125 openlog(debug_ident, LOG_PID, debug_syslogfacility);
129 debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
133 void debug_reopen_log() {
136 /* not a file, noop, return success */
137 if (!debug_filepath) {
138 debug(DBG_ERR, "skipping reopen");
142 if (debug_file != stderr)
145 debug_file = fopen(debug_filepath, "a");
147 debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
150 debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
151 debug_filepath, strerror(errno));
153 setvbuf(debug_file, NULL, _IONBF, 0);
156 void debug_logit(uint8_t level, const char *format, va_list ap) {
161 if (debug_syslogfacility) {
164 priority = LOG_DEBUG;
170 priority = LOG_NOTICE;
173 priority = LOG_WARNING;
179 priority = LOG_DEBUG;
181 vsyslog(priority, format, ap);
183 if (debug_timestamp && (timebuf = malloc(256))) {
184 gettimeofday(&now, NULL);
185 ctime_r(&now.tv_sec, timebuf);
186 timebuf[strlen(timebuf) - 1] = '\0';
187 fprintf(debug_file, "%s: ", timebuf + 4);
190 vfprintf(debug_file, format, ap);
191 fprintf(debug_file, "\n");
195 void debug(uint8_t level, char *format, ...) {
197 if (level < debug_level)
199 va_start(ap, format);
200 debug_logit(level, format, ap);
204 void debugx(int status, uint8_t level, char *format, ...) {
205 if (level >= debug_level) {
207 va_start(ap, format);
208 debug_logit(level, format, ap);
214 void debugerrno(int err, uint8_t level, char *format, ...) {
215 if (level >= debug_level) {
217 size_t len = strlen(format);
218 char *tmp = malloc(len + 1024 + 2);
223 if (strerror_r(err, tmp + len, 1024))
225 va_start(ap, format);
226 debug_logit(level, tmp, ap);
231 void debugerrnox(int err, uint8_t level, char *format, ...) {
232 if (level >= debug_level) {
234 va_start(ap, format);
235 debugerrno(err, level, format, ap);
241 #if defined(WANT_FTICKS)
242 void fticks_debug(const char *format, ...) {
245 va_start(ap, format);
246 if (!debug_syslogfacility && !fticks_syslogfacility)
247 debug_logit(0xff, format, ap);
249 priority = LOG_DEBUG | fticks_syslogfacility;
250 vsyslog(priority, format, ap);
255 /* Local Variables: */
256 /* c-file-style: "stroustrup" */