diff options
author | Daniel Drake <dsd@gentoo.org> | 2008-05-23 15:58:11 +0100 |
---|---|---|
committer | Daniel Drake <dsd@gentoo.org> | 2008-05-23 15:58:11 +0100 |
commit | 4d788967e3f8d75eaf3a1ac1ee8e2e8bed0601c1 (patch) | |
tree | a636a13ca134ed589eff2202591345a75826a5c1 | |
parent | bef33bb9eba0da04ee7488d9cd5e6ab12bc61c0c (diff) | |
download | libusb-4d788967e3f8d75eaf3a1ac1ee8e2e8bed0601c1.tar.gz |
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.
-rw-r--r-- | libusb/core.c | 34 | ||||
-rw-r--r-- | libusb/libusb.h | 1 |
2 files changed, 35 insertions, 0 deletions
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 @@ -988,6 +990,29 @@ API_EXPORTED int libusb_detach_kernel_driver(libusb_device_handle *dev, } /** \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. * \returns 0 on success, or a LIBUSB_ERROR code on failure @@ -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); |