Skip to content

Commit

Permalink
Add event_enable_debug_logging() to control use of debug logs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nmathewson committed Dec 16, 2010
1 parent b8b8aa5 commit e30a82f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
16 changes: 16 additions & 0 deletions include/event2/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 24 additions & 3 deletions log-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@

#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;
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit e30a82f

Please sign in to comment.