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.
This commit is contained in:
Jef Driesen 2012-07-27 08:53:45 +02:00
parent 866e636e8c
commit b1b30f068c
3 changed files with 40 additions and 0 deletions

View File

@ -28,6 +28,15 @@ LT_PREREQ([2.2.0])
LT_INIT([win32-dll]) LT_INIT([win32-dll])
LT_PROG_RC 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. # Checks for programs.
AC_PROG_CC AC_PROG_CC
AC_PROG_CC_C99 AC_PROG_CC_C99

View File

@ -22,17 +22,29 @@
#ifndef DC_CONTEXT_PRIVATE_H #ifndef DC_CONTEXT_PRIVATE_H
#define DC_CONTEXT_PRIVATE_H #define DC_CONTEXT_PRIVATE_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <libdivecomputer/context.h> #include <libdivecomputer/context.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
#ifdef ENABLE_LOGGING
#define SYSERROR(context, errcode) dc_context_syserror (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, errcode) #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 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 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 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__) #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_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, ...); dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...);

View File

@ -34,9 +34,12 @@ struct dc_context_t {
dc_loglevel_t loglevel; dc_loglevel_t loglevel;
dc_logfunc_t logfunc; dc_logfunc_t logfunc;
void *userdata; void *userdata;
#ifdef ENABLE_LOGGING
char msg[4096]; char msg[4096];
#endif
}; };
#ifdef ENABLE_LOGGING
static void 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) 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); fprintf (stderr, "%s: %s [in %s:%d (%s)]\n", loglevels[loglevel], msg, file, line, function);
} }
#endif
dc_status_t dc_status_t
dc_context_new (dc_context_t **out) dc_context_new (dc_context_t **out)
@ -57,11 +61,18 @@ dc_context_new (dc_context_t **out)
if (context == NULL) if (context == NULL)
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;
#ifdef ENABLE_LOGGING
context->loglevel = DC_LOGLEVEL_WARNING; context->loglevel = DC_LOGLEVEL_WARNING;
context->logfunc = logfunc; context->logfunc = logfunc;
#else
context->loglevel = DC_LOGLEVEL_NONE;
context->logfunc = NULL;
#endif
context->userdata = NULL; context->userdata = NULL;
#ifdef ENABLE_LOGGING
memset (context->msg, 0, sizeof (context->msg)); memset (context->msg, 0, sizeof (context->msg));
#endif
*out = context; *out = context;
@ -82,7 +93,9 @@ dc_context_set_loglevel (dc_context_t *context, dc_loglevel_t loglevel)
if (context == NULL) if (context == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
#ifdef ENABLE_LOGGING
context->loglevel = loglevel; context->loglevel = loglevel;
#endif
return DC_STATUS_SUCCESS; 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) if (context == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
#ifdef ENABLE_LOGGING
context->logfunc = logfunc; context->logfunc = logfunc;
context->userdata = userdata; context->userdata = userdata;
#endif
return DC_STATUS_SUCCESS; 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_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, ...) 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; va_list ap;
#endif
if (context == NULL) if (context == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
#ifdef ENABLE_LOGGING
if (loglevel > context->loglevel) if (loglevel > context->loglevel)
return DC_STATUS_SUCCESS; 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); va_end (ap);
context->logfunc (context, loglevel, file, line, function, context->msg, context->userdata); context->logfunc (context, loglevel, file, line, function, context->msg, context->userdata);
#endif
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} }