Make evdns use the regular logging system by default

Once, for reasons that made sense at the time, we had evdns.c use its
own logging subsystem with two levels, "warn" and "debug".  This leads
to problems, since setting a log handler for Libevent wouldn't actually
trap these messages, since they weren't on by default, and since some of
the warns should really be msgs.

This patch changes the default behavior of evdns.c to log to
event_(debugx,warnx,msgx) by default, and adds a new (internal-use-only)
log level of EVDNS_LOG_MSG.  Programs that set a evdns logging
function will see no change.  Programs that don't will now see evdns
warnings reported like other warnings.
This commit is contained in:
Nick Mathewson 2010-03-10 16:25:16 -05:00
parent 13e4f3bd89
commit b2f2be6edc
2 changed files with 30 additions and 8 deletions

34
evdns.c
View File

@ -105,6 +105,7 @@
#define EVDNS_LOG_DEBUG 0 #define EVDNS_LOG_DEBUG 0
#define EVDNS_LOG_WARN 1 #define EVDNS_LOG_WARN 1
#define EVDNS_LOG_MSG 2
#ifndef HOST_NAME_MAX #ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255 #define HOST_NAME_MAX 255
@ -435,12 +436,23 @@ debug_ntop(const struct sockaddr *sa)
return "<unknown>"; return "<unknown>";
} }
static void
default_evdns_log_fn(int warning, const char *buf)
{
if (warning == EVDNS_LOG_WARN)
event_warnx("[evdns] %s", buf);
else if (warning == EVDNS_LOG_MSG)
event_msgx("[evdns] %s", buf);
else
event_debug(("[evdns] %s", buf));
}
static evdns_debug_log_fn_type evdns_log_fn = NULL; static evdns_debug_log_fn_type evdns_log_fn = NULL;
void void
evdns_set_log_fn(evdns_debug_log_fn_type fn) evdns_set_log_fn(evdns_debug_log_fn_type fn)
{ {
evdns_log_fn = fn; evdns_log_fn = fn;
} }
#ifdef __GNUC__ #ifdef __GNUC__
@ -459,8 +471,15 @@ _evdns_log(int warn, const char *fmt, ...)
return; return;
va_start(args,fmt); va_start(args,fmt);
evutil_vsnprintf(buf, sizeof(buf), fmt, args); evutil_vsnprintf(buf, sizeof(buf), fmt, args);
evdns_log_fn(warn, buf);
va_end(args); va_end(args);
if (evdns_log_fn) {
if (warn == EVDNS_LOG_MSG)
warn = EVDNS_LOG_WARN;
evdns_log_fn(warn, buf);
} else {
default_evdns_log_fn(warn, buf);
}
} }
#define log _evdns_log #define log _evdns_log
@ -555,12 +574,12 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
/* then don't do anything */ /* then don't do anything */
if (!ns->state) return; if (!ns->state) return;
log(EVDNS_LOG_WARN, "Nameserver %s has failed: %s", log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
debug_ntop((struct sockaddr*)&ns->address), msg); debug_ntop((struct sockaddr*)&ns->address), msg);
base->global_good_nameservers--; base->global_good_nameservers--;
EVUTIL_ASSERT(base->global_good_nameservers >= 0); EVUTIL_ASSERT(base->global_good_nameservers >= 0);
if (base->global_good_nameservers == 0) { if (base->global_good_nameservers == 0) {
log(EVDNS_LOG_WARN, "All nameservers have failed"); log(EVDNS_LOG_MSG, "All nameservers have failed");
} }
ns->state = 0; ns->state = 0;
@ -602,7 +621,7 @@ nameserver_up(struct nameserver *const ns)
{ {
ASSERT_LOCKED(ns->base); ASSERT_LOCKED(ns->base);
if (ns->state) return; if (ns->state) return;
log(EVDNS_LOG_WARN, "Nameserver %s is back up", log(EVDNS_LOG_MSG, "Nameserver %s is back up",
debug_ntop((struct sockaddr *)&ns->address)); debug_ntop((struct sockaddr *)&ns->address));
evtimer_del(&ns->timeout_event); evtimer_del(&ns->timeout_event);
if (ns->probe_request) { if (ns->probe_request) {
@ -1288,8 +1307,9 @@ server_port_read(struct evdns_server_port *s) {
int err = evutil_socket_geterror(s->socket); int err = evutil_socket_geterror(s->socket);
if (EVUTIL_ERR_RW_RETRIABLE(err)) if (EVUTIL_ERR_RW_RETRIABLE(err))
return; return;
log(EVDNS_LOG_WARN, "Error %s (%d) while reading request.", log(EVDNS_LOG_WARN,
evutil_socket_error_to_string(err), err); "Error %s (%d) while reading request.",
evutil_socket_error_to_string(err), err);
return; return;
} }
request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen); request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);

View File

@ -476,7 +476,9 @@ typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
/** /**
Set the callback function to handle log messages. Set the callback function to handle DNS log messages. If this
callback is not set, evdns log messages are handled with the regular
Libevent logging system.
@param fn the callback to be invoked when a log message is generated @param fn the callback to be invoked when a log message is generated
*/ */