From b1b30f068cd8bee41cf18726b183d3dc3dc6c5aa Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 27 Jul 2012 08:53:45 +0200 Subject: [PATCH] Add an option to disable the logging. With the new option, the library can be compiled with the entire logging infrastructure disabled. The public api remains unchanged, but the internal logging functions will have no effect anymore. In practice the overhead of the logging functions should be quite small, and disabling the logging at runtime might be more convenient. Especially because troubleshooting will become much harder without any logging. --- configure.ac | 9 +++++++++ src/context-private.h | 12 ++++++++++++ src/context.c | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/configure.ac b/configure.ac index d08f2d7..2b54da2 100644 --- a/configure.ac +++ b/configure.ac @@ -28,6 +28,15 @@ LT_PREREQ([2.2.0]) LT_INIT([win32-dll]) LT_PROG_RC +# Logging support. +AC_ARG_ENABLE([logging], + [AS_HELP_STRING([--enable-logging=@<:@yes/no@:>@], + [Enable logging @<:@default=yes@:>@])], + [], [enable_logging=yes]) +AS_IF([test "x$enable_logging" = "xyes"], [ + AC_DEFINE(ENABLE_LOGGING, [1], [Enable logging.]) +]) + # Checks for programs. AC_PROG_CC AC_PROG_CC_C99 diff --git a/src/context-private.h b/src/context-private.h index c32fa1b..3ccf78f 100644 --- a/src/context-private.h +++ b/src/context-private.h @@ -22,17 +22,29 @@ #ifndef DC_CONTEXT_PRIVATE_H #define DC_CONTEXT_PRIVATE_H +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +#ifdef ENABLE_LOGGING #define SYSERROR(context, errcode) dc_context_syserror (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, errcode) #define ERROR(context, ...) dc_context_log (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) #define WARNING(context, ...) dc_context_log (context, DC_LOGLEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) #define INFO(context, ...) dc_context_log (context, DC_LOGLEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) #define DEBUG(context, ...) dc_context_log (context, DC_LOGLEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) +#else +#define SYSERROR(context, errcode) +#define ERROR(context, ...) +#define WARNING(context, ...) +#define INFO(context, ...) +#define DEBUG(context, ...) +#endif dc_status_t dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...); diff --git a/src/context.c b/src/context.c index 45f6800..1b0b58f 100644 --- a/src/context.c +++ b/src/context.c @@ -34,9 +34,12 @@ struct dc_context_t { dc_loglevel_t loglevel; dc_logfunc_t logfunc; void *userdata; +#ifdef ENABLE_LOGGING char msg[4096]; +#endif }; +#ifdef ENABLE_LOGGING static void logfunc (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) { @@ -44,6 +47,7 @@ logfunc (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsign fprintf (stderr, "%s: %s [in %s:%d (%s)]\n", loglevels[loglevel], msg, file, line, function); } +#endif dc_status_t dc_context_new (dc_context_t **out) @@ -57,11 +61,18 @@ dc_context_new (dc_context_t **out) if (context == NULL) return DC_STATUS_NOMEMORY; +#ifdef ENABLE_LOGGING context->loglevel = DC_LOGLEVEL_WARNING; context->logfunc = logfunc; +#else + context->loglevel = DC_LOGLEVEL_NONE; + context->logfunc = NULL; +#endif context->userdata = NULL; +#ifdef ENABLE_LOGGING memset (context->msg, 0, sizeof (context->msg)); +#endif *out = context; @@ -82,7 +93,9 @@ dc_context_set_loglevel (dc_context_t *context, dc_loglevel_t loglevel) if (context == NULL) return DC_STATUS_INVALIDARGS; +#ifdef ENABLE_LOGGING context->loglevel = loglevel; +#endif return DC_STATUS_SUCCESS; } @@ -93,8 +106,10 @@ dc_context_set_logfunc (dc_context_t *context, dc_logfunc_t logfunc, void *userd if (context == NULL) return DC_STATUS_INVALIDARGS; +#ifdef ENABLE_LOGGING context->logfunc = logfunc; context->userdata = userdata; +#endif return DC_STATUS_SUCCESS; } @@ -102,11 +117,14 @@ dc_context_set_logfunc (dc_context_t *context, dc_logfunc_t logfunc, void *userd dc_status_t dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...) { +#ifdef ENABLE_LOGGING va_list ap; +#endif if (context == NULL) return DC_STATUS_INVALIDARGS; +#ifdef ENABLE_LOGGING if (loglevel > context->loglevel) return DC_STATUS_SUCCESS; @@ -118,6 +136,7 @@ dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, va_end (ap); context->logfunc (context, loglevel, file, line, function, context->msg, context->userdata); +#endif return DC_STATUS_SUCCESS; }