From f4b01ddc6a55acea49273e35f19e97ef9ef88d28 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Mon, 22 Aug 2022 12:04:40 +0200 Subject: scd: Add a libusb debug level. * scd/ccid-driver.c (USE_LIBUSB_DEBUG_CB): New const. (debug_libusb_cb): new. (ccid_set_debug_level): Use it. -- This allows to see the libusb log in our usual debug output. For this the option debug-ccid-driver needs to be given 5 or more times. --- scd/ccid-driver.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'scd/ccid-driver.c') diff --git a/scd/ccid-driver.c b/scd/ccid-driver.c index f63219d97..f527b49ff 100644 --- a/scd/ccid-driver.c +++ b/scd/ccid-driver.c @@ -166,6 +166,11 @@ #endif /* This source is not used by scdaemon. */ +#undef USE_LIBUSB_DEBUG_CB +#if LIBUSB_API_VERSION >= 0x01000107 +# define USE_LIBUSB_DEBUG_CB 1 +#endif + #ifndef EAGAIN #define EAGAIN EWOULDBLOCK @@ -1231,6 +1236,23 @@ scan_devices (char **r_rid) } +#ifdef USE_LIBUSB_DEBUG_CB +static void +debug_libusb_cb (libusb_context *ctx, enum libusb_log_level level, + const char *str) +{ + int n = str? strlen (str):0; + + (void)ctx; + + /* Strip the LF so that our logging filter does not escape it. */ + if (n && str[n-1] == '\n') + n--; + log_debug ("libusb{%d}: %.*s\n", level, n, str); +} +#endif /* USE_LIBUSB_DEBUG_CB */ + + /* Set the level of debugging to LEVEL and return the old level. -1 just returns the old level. A level of 0 disables debugging, 1 enables debugging, 2 enables additional tracing of the T=1 @@ -1245,6 +1267,16 @@ ccid_set_debug_level (int level) int old = debug_level; if (level != -1) debug_level = level; +#ifdef USE_LIBUSB_DEBUG_CB + if (level > 4) + { + log_debug ("libusb: Enable logging\n"); + libusb_set_log_cb (NULL, debug_libusb_cb, LIBUSB_LOG_CB_GLOBAL); + libusb_set_option (NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG); + } + else + libusb_set_log_cb (NULL, NULL, LIBUSB_LOG_CB_GLOBAL); +#endif /* USE_LIBUSB_DEBUG_CB */ return old; } @@ -4050,7 +4082,7 @@ main (int argc, char **argv) } /* - * Local Variables: + * Disabled Local Variables: * compile-command: "gcc -DTEST -DGPGRT_ENABLE_ES_MACROS -DHAVE_NPTH -DUSE_NPTH -Wall -I/usr/include/libusb-1.0 -I/usr/local/include -lusb-1.0 -g ccid-driver.c -lnpth -lgpg-error" * End: */ -- cgit v1.2.3