]> granicus.if.org Git - libevent/commitdiff
Add event_enable_debug_logging() to control use of debug logs
authorNick Mathewson <nickm@torproject.org>
Thu, 16 Dec 2010 18:58:56 +0000 (13:58 -0500)
committerNick Mathewson <nickm@torproject.org>
Thu, 16 Dec 2010 18:58:56 +0000 (13:58 -0500)
Previously, debug logs were turned on if you built with -DUSE_DEBUG
and off otherwise.  This make builds with -DUSE_DEBUG hideously slow
and other builds unable to get debug logs.

This is based off a patch by Ralph Castain from October.  It tries a
little harder to avoid needless function calls, it doesn't require
stdbool, and makes the controlling parameter a mask rather than a
boolean so that we can later support enabling only the debugging
messages for the parts of Libevent you're trying to debug.

include/event2/event.h
log-internal.h
log.c

index 4b05b32538cfb589a1d60b3a9c4f9eb25c263b04..f71d0844c67b2921bffcfe9b67399c2cbeaa820b 100644 (file)
@@ -313,6 +313,22 @@ void event_set_log_callback(event_log_cb cb);
 typedef void (*event_fatal_cb)(int err);
 void event_set_fatal_callback(event_fatal_cb cb);
 
+#define EVENT_DBG_ALL 0xffffffffu
+
+/**
+ Turn on debugging logs and have them sent to the default log handler.
+
+ This is a global setting; you must call this before any calls that create an
+ event-base.
+
+ Debug logs are verbose.
+
+ @param which Controls which debug messages are turned on.  This option is
+   unused for now; for forward compatibility, you must pass in the constant
+   "EVENT_DBG_ALL"
+ */
+void event_enable_debug_logging(ev_uint32_t which);
+
 /**
   Associate a different event base with an event.
 
index c1905aea34fec5e9991beac399ddd3c3f97b0dac..e3c6d564a31ddb9a048a7afe660e9552331913e5 100644 (file)
 
 #define _EVENT_ERR_ABORT ((int)0xdeaddead)
 
+#define USE_GLOBAL_FOR_DEBUG_LOGGING
+
+#if !defined(_EVENT_DISABLE_DEBUG_MODE) || defined(USE_DEBUG)
+#define EVENT_DEBUG_LOGGING_ENABLED
+#endif
+
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#ifdef USE_GLOBAL_FOR_DEBUG_LOGGING
+extern ev_uint32_t _event_debug_logging_mask;
+#define _event_debug_get_logging_mask() (_event_debug_logging_mask)
+#else
+ev_uint32_t _event_debug_get_logging_mask(void);
+#endif
+#else
+#define _event_debug_get_logging_mask() (0)
+#endif
+
 void event_err(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3) EV_NORETURN;
 void event_warn(const char *fmt, ...) EV_CHECK_FMT(1,2);
 void event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...) EV_CHECK_FMT(3,4) EV_NORETURN;
@@ -48,10 +65,14 @@ void event_warnx(const char *fmt, ...) EV_CHECK_FMT(1,2);
 void event_msgx(const char *fmt, ...) EV_CHECK_FMT(1,2);
 void _event_debugx(const char *fmt, ...) EV_CHECK_FMT(1,2);
 
-#ifdef USE_DEBUG
-#define event_debug(x) _event_debugx x
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#define event_debug(x) do {                    \
+       if (_event_debug_get_logging_mask()) {  \
+               _event_debugx x;                \
+       }                                       \
+       } while (0)
 #else
-#define event_debug(x) do {;} while (0)
+#define event_debug(x) ((void)0)
 #endif
 
 #undef EV_CHECK_FMT
diff --git a/log.c b/log.c
index f0cd4320d139dd913d3e549029f0d26c6113762b..79eaae711f38c3962521c87a04feafd3b1ac99df 100644 (file)
--- a/log.c
+++ b/log.c
@@ -63,6 +63,25 @@ static void event_exit(int errcode) EV_NORETURN;
 
 static event_fatal_cb fatal_fn = NULL;
 
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#ifdef USE_DEBUG
+#define DEFAULT_MASK EVENT_DBG_ALL
+#else
+#define DEFAULT_MASK 0
+#endif
+
+#ifdef USE_GLOBAL_FOR_DEBUG_LOGGING
+ev_uint32_t _event_debug_logging_mask = DEFAULT_MASK;
+#else
+static ev_uint32_t _event_debug_logging_mask = DEFAULT_MASK;
+ev_uint32_t
+_event_debug_get_logging_mask(void)
+{
+       return _event_debug_logging_mask;
+}
+#endif
+#endif /* EVENT_DEBUG_LOGGING_ENABLED */
+
 void
 event_set_fatal_callback(event_fatal_cb cb)
 {