From 4d788967e3f8d75eaf3a1ac1ee8e2e8bed0601c1 Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Fri, 23 May 2008 15:58:11 +0100 Subject: don't print messages by default Add libusb_set_debug() API to set message verbosity. Ludovic Rousseau pointed out that applications may close stdout/stderr descriptors, which might then be reused. --- libusb/core.c | 34 ++++++++++++++++++++++++++++++++++ libusb/libusb.h | 1 + 2 files changed, 35 insertions(+) diff --git a/libusb/core.c b/libusb/core.c index 45229be..2e46d20 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -31,6 +31,8 @@ #include "libusb.h" #include "libusbi.h" +static int usbi_debug = 0; + #ifdef OS_LINUX const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend; #else @@ -987,6 +989,29 @@ API_EXPORTED int libusb_detach_kernel_driver(libusb_device_handle *dev, return LIBUSB_ERROR_NOT_SUPPORTED; } +/** \ingroup lib + * Set message verbosity. + * - Level 0: no messages ever printed by the library (default) + * - Level 1: error messages are printed to stderr + * - Level 2: warning and error messages are printed to stderr + * - Level 3: informational messages are printed to stdout, warning and error + * messages are printed to stderr + * + * The default level is 0, which means no messages are ever printed. If you + * choose to increase the message verbosity level, ensure that your + * application does not close the stdout/stderr file descriptors. + * + * libusb also offers the compile-time option to print verbose debug messages + * to stderr. If libusb was compiled with this option, this function + * effectively does nothing: messages of all types are always printed. + * + * \param level debug level to set + */ +API_EXPORTED void libusb_set_debug(int level) +{ + usbi_debug = level; +} + /** \ingroup lib * Initialize libusb. This function must be called before calling any other * libusb function. @@ -1041,6 +1066,15 @@ void usbi_log(enum usbi_log_level level, const char *function, FILE *stream = stdout; const char *prefix; +#ifndef ENABLE_DEBUG_LOGGING + if (!usbi_debug) + return; + if (level == LOG_LEVEL_WARNING && usbi_debug < 2) + return; + if (level == LOG_LEVEL_INFO && usbi_debug < 3) + return; +#endif + switch (level) { case LOG_LEVEL_INFO: prefix = "info"; diff --git a/libusb/libusb.h b/libusb/libusb.h index 3e77beb..3d222e3 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -723,6 +723,7 @@ struct libusb_transfer { int libusb_init(void); void libusb_exit(void); +void libusb_set_debug(int level); ssize_t libusb_get_device_list(libusb_device ***list); void libusb_free_device_list(libusb_device **list, int unref_devices); -- cgit v1.2.1