Moving on on the roadmap.
This patch ensures that debug logging messages are always included in
the library and can be turned on or off by changing the log level.
I tried to keep things simple, so this modifies the existing behaviour
where debug messages were only included (and enable) with the
--enable-debug-log, which may have an impact for people building libusbx
on embedded systems who want to keep the library as small as possible.
However, the reasoning is that in such a scenario, --disable-log would
probably be used to also disable errors and warnings log messages, so I
expect the impact to be minimal.
The visible behaviour --enable-debug-log is left unchanged however and
the option is still valid.
With this patch, unless you use --disable-log, then all logging messages
are available, and debug logging can be turned on with either a call to
libusb_set_debug (relies on a context having been initialized, even if
NULL is passed, so MUST be called after libusb_init - because of this
annoying context business, enabling the setting of debug level before a
call to init would add a lot of complexity) or by setting the
LIBUSB_DEBUG environment variable to 4.
Oh, and because LIBUSB_DEBUG has precedence over anything else, this has
the nice side effect of leaving us with the standard ability to get
debug logging output from any user coming to us with a libusbx based
console application, no matter what the app does with regards to
libusb_set_debug and regardless of whether the user can recompile the
app. This could come very handy.
Finally, still because of these pesky contexts, there's an addon to
create the default context before calling on usb_dbg. This is because we
can very much get into usbi_log_v with a NULL context otherwise, which
won't cause a crash (as we also now check for NULL) but will drop messages.
Regards,
/Pete
PS: this patch also fixes a missing space between the function name and
the message for debug output
>From d3f1391b142e24100756926579b7d4d3d14f50a0 Mon Sep 17 00:00:00 2001
From: Pete Batard <p...@akeo.ie>
Date: Thu, 31 May 2012 18:31:34 +0100
Subject: [PATCH] Core: Add toggleable debug logging
* Also fix a missing space in debug log messages introduced
with the previous timestamp logging update
---
configure.ac | 4 ++--
libusb/core.c | 16 +++++++++++++++-
libusb/libusbi.h | 10 +---------
libusb/os/windows_usb.c | 2 +-
msvc/config.h | 2 +-
5 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/configure.ac b/configure.ac
index 792035f..d27f031 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/libusb/core.c b/libusb/core.c
index 62dd15a..0b93af2 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -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);
@@ -1834,7 +1848,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);
diff --git a/libusb/libusbi.h b/libusb/libusbi.h
index 41a6ba1..9de56a1 100644
--- a/libusb/libusbi.h
+++ b/libusb/libusbi.h
@@ -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 */
diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c
index 1d9a80b..dbebfaf 100644
--- a/libusb/os/windows_usb.c
+++ b/libusb/os/windows_usb.c
@@ -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];
diff --git a/msvc/config.h b/msvc/config.h
index 43b4d4e..e14d91d 100644
--- a/msvc/config.h
+++ b/msvc/config.h
@@ -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 */
--
1.7.10.msysgit.1
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel