Core: Add toggleable debug logging
authorPete Batard <pete@akeo.ie>
Thu, 31 May 2012 17:31:34 +0000 (18:31 +0100)
committerPete Batard <pete@akeo.ie>
Mon, 4 Jun 2012 00:31:06 +0000 (01:31 +0100)
* Also fix a missing space in debug log messages introduced
  with the previous timestamp logging update

configure.ac
libusb/core.c
libusb/libusbi.h
libusb/os/windows_usb.c
libusb/version_nano.h
msvc/config.h

index d91d042..96978a8 100644 (file)
@@ -171,11 +171,11 @@ if test "x$log_enabled" != "xno"; then
 fi
 
 AC_ARG_ENABLE([debug-log], [AS_HELP_STRING([--enable-debug-log],
-       [enable debug logging (default n)])],
+       [start with debug message logging enabled (default n)])],
        [debug_log_enabled=$enableval],
        [debug_log_enabled='no'])
 if test "x$debug_log_enabled" != "xno"; then
-       AC_DEFINE([ENABLE_DEBUG_LOGGING], 1, [Debug message logging])
+       AC_DEFINE([ENABLE_DEBUG_LOGGING], 1, [Start with debug message logging enabled])
 fi
 
 # Examples build
index 6540fd3..d4ff789 100644 (file)
@@ -1634,12 +1634,22 @@ int API_EXPORTED libusb_init(libusb_context **context)
        }
        memset(ctx, 0, sizeof(*ctx));
 
+#ifdef ENABLE_DEBUG_LOGGING
+       ctx->debug = LOG_LEVEL_DEBUG;
+#endif
+
        if (dbg) {
                ctx->debug = atoi(dbg);
                if (ctx->debug)
                        ctx->debug_fixed = 1;
        }
 
+       /* default context should be initialized before calling usbi_dbg */
+       if (!usbi_default_context) {
+               usbi_default_context = ctx;
+               usbi_dbg("created default context");
+       }
+
        usbi_dbg("");
 
        if (usbi_backend->init) {
@@ -1793,6 +1803,8 @@ void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
        global_debug = 1;
 #else
        USBI_GET_CONTEXT(ctx);
+       if (ctx == NULL)
+               return;
        global_debug = (ctx->debug == LOG_LEVEL_DEBUG);
        if (!ctx->debug)
                return;
@@ -1800,6 +1812,8 @@ void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
                return;
        if (level == LOG_LEVEL_INFO && ctx->debug < LOG_LEVEL_INFO)
                return;
+       if (level == LOG_LEVEL_DEBUG && ctx->debug < LOG_LEVEL_DEBUG)
+               return;
 #endif
 
        usbi_gettimeofday(&now, NULL);
@@ -1836,7 +1850,7 @@ void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
        }
 
        if (global_debug) {
-               fprintf(stderr, "[%2d.%06d] [%08x] libusbx: %s [%s]",
+               fprintf(stderr, "[%2d.%06d] [%08x] libusbx: %s [%s] ",
                        (int)now.tv_sec, (int)now.tv_usec, usbi_get_tid(), prefix, function);
        } else {
                fprintf(stderr, "libusbx: %s [%s] ", prefix, function);
index 41a6ba1..9de56a1 100644 (file)
@@ -129,13 +129,9 @@ void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
 
 #ifdef ENABLE_LOGGING
 #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
-#else
-#define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
-#endif
-
-#ifdef ENABLE_DEBUG_LOGGING
 #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
 #else
+#define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
 #define usbi_dbg(...) do {} while(0)
 #endif
 
@@ -168,11 +164,7 @@ static inline void usbi_err( struct libusb_context *ctx, const char *format,
        LOG_BODY(ctx,LOG_LEVEL_ERROR)
 
 static inline void usbi_dbg(const char *format, ...)
-#ifdef ENABLE_DEBUG_LOGGING
        LOG_BODY(NULL,LOG_LEVEL_DEBUG)
-#else
-{ }
-#endif
 
 #endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
 
index 1d9a80b..dbebfaf 100644 (file)
@@ -129,7 +129,7 @@ static inline BOOLEAN guid_eq(const GUID *guid1, const GUID *guid2) {
        return false;
 }
 
-#if defined(ENABLE_DEBUG_LOGGING) || (defined(_MSC_VER) && _MSC_VER < 1400)
+#if defined(ENABLE_LOGGING)
 static char* guid_to_string(const GUID* guid)
 {
        static char guid_string[MAX_GUID_STRING_LENGTH];
index 9687da3..264a5e3 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10518
+#define LIBUSB_NANO 10519
index 43b4d4e..e14d91d 100644 (file)
@@ -8,7 +8,7 @@
 /* Default visibility */
 #define DEFAULT_VISIBILITY /**/
 
-/* Debug message logging */
+/* Start with debug message logging enabled */
 //#define ENABLE_DEBUG_LOGGING 1
 
 /* Message logging */