diff options
Diffstat (limited to 'libusb')
-rw-r--r-- | libusb/core.c | 66 | ||||
-rw-r--r-- | libusb/libusb.h | 41 | ||||
-rw-r--r-- | libusb/libusbi.h | 1 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
4 files changed, 75 insertions, 35 deletions
diff --git a/libusb/core.c b/libusb/core.c index 17e6117..23ab43b 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -2219,6 +2219,29 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level) #endif } +static void libusb_set_log_cb_internal(libusb_context *ctx, libusb_log_cb cb, + int mode) +{ +#if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY)) +#if !defined(USE_SYSTEM_LOGGING_FACILITY) + if (mode & LIBUSB_LOG_CB_GLOBAL) + log_handler = cb; +#endif +#if !defined(ENABLE_DEBUG_LOGGING) + if (mode & LIBUSB_LOG_CB_CONTEXT) { + ctx = usbi_get_context(ctx); + ctx->log_handler = cb; + } +#else + UNUSED(ctx); +#endif +#else + UNUSED(ctx); + UNUSED(cb); + UNUSED(mode); +#endif +} + /** \ingroup libusb_lib * Set log handler. * @@ -2245,24 +2268,7 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level) void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode) { -#if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY)) -#if !defined(USE_SYSTEM_LOGGING_FACILITY) - if (mode & LIBUSB_LOG_CB_GLOBAL) - log_handler = cb; -#endif -#if !defined(ENABLE_DEBUG_LOGGING) - if (mode & LIBUSB_LOG_CB_CONTEXT) { - ctx = usbi_get_context(ctx); - ctx->log_handler = cb; - } -#else - UNUSED(ctx); -#endif -#else - UNUSED(ctx); - UNUSED(cb); - UNUSED(mode); -#endif + libusb_set_log_cb_internal(ctx, cb, mode); } /** \ingroup libusb_lib @@ -2292,6 +2298,7 @@ int API_EXPORTEDV libusb_set_option(libusb_context *ctx, enum libusb_option option, ...) { int arg = 0, r = LIBUSB_SUCCESS; + libusb_log_cb log_cb = NULL; va_list ap; va_start(ap, option); @@ -2301,6 +2308,9 @@ int API_EXPORTEDV libusb_set_option(libusb_context *ctx, r = LIBUSB_ERROR_INVALID_PARAM; } } + if (LIBUSB_OPTION_LOG_CB == option) { + log_cb = (libusb_log_cb) va_arg(ap, libusb_log_cb); + } va_end(ap); if (LIBUSB_SUCCESS != r) { @@ -2316,12 +2326,15 @@ int API_EXPORTEDV libusb_set_option(libusb_context *ctx, default_context_options[option].is_set = 1; if (LIBUSB_OPTION_LOG_LEVEL == option) { default_context_options[option].arg.ival = arg; + } else if (LIBUSB_OPTION_LOG_CB) { + default_context_options[option].arg.log_cbval = log_cb; } usbi_mutex_static_unlock(&default_context_lock); } ctx = usbi_get_context(ctx); if (NULL == ctx) { + libusb_set_log_cb_internal(NULL, log_cb, LIBUSB_LOG_CB_GLOBAL); return LIBUSB_SUCCESS; } @@ -2343,6 +2356,9 @@ int API_EXPORTEDV libusb_set_option(libusb_context *ctx, return LIBUSB_ERROR_NOT_SUPPORTED; break; + case LIBUSB_OPTION_LOG_CB: + libusb_set_log_cb_internal(ctx, log_cb, LIBUSB_LOG_CB_CONTEXT); + break; default: return LIBUSB_ERROR_INVALID_PARAM; } @@ -2452,14 +2468,24 @@ int API_EXPORTED libusb_init_context(libusb_context **ctx, const struct libusb_i if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) { continue; } - r = libusb_set_option(_ctx, option); + if (LIBUSB_OPTION_LOG_CB != option) { + r = libusb_set_option(_ctx, option); + } else { + r = libusb_set_option(_ctx, option, default_context_options[option].arg.log_cbval); + } if (LIBUSB_SUCCESS != r) goto err_free_ctx; } /* apply any options provided by the user */ for (int i = 0 ; i < num_options ; ++i) { - r = libusb_set_option(_ctx, options[i].option, options[i].value.ival); + switch(options[i].option) { + case LIBUSB_OPTION_LOG_CB: + r = libusb_set_option(_ctx, options[i].option, options[i].value.log_cbval); + break; + default: + r = libusb_set_option(_ctx, options[i].option, options[i].value.ival); + } if (LIBUSB_SUCCESS != r) goto err_free_ctx; } diff --git a/libusb/libusb.h b/libusb/libusb.h index 0b039d4..99cc017 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -3,7 +3,7 @@ * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com> * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org> * Copyright © 2012 Pete Batard <pete@akeo.ie> - * Copyright © 2012-2021 Nathan Hjelm <hjelmn@cs.unm.edu> + * Copyright © 2012-2023 Nathan Hjelm <hjelmn@cs.unm.edu> * Copyright © 2014-2020 Chris Dickens <christopher.a.dickens@gmail.com> * For more information, please visit: http://libusb.info * @@ -1534,20 +1534,18 @@ enum libusb_option { */ LIBUSB_OPTION_WINUSB_RAW_IO = 3, - LIBUSB_OPTION_MAX = 4 -}; + /** Set the context log callback functon. + * + * Set the log callback function either on a context or globally. This + * option must be provided an argument of type libusb_log_cb. Using this + * option with a NULL context is equivalent to calling libusb_set_log_cb + * with mode LIBUSB_LOG_CB_GLOBAL. Using it with a non-NULL context is + * equivalent to calling libusb_set_log_cb with mode + * LIBUSB_LOG_CB_CONTEXT. + */ + LIBUSB_OPTION_LOG_CB = 4, -/** \ingroup libusb_lib - * Structure used for setting options through \ref libusb_init_context. - * - */ -struct libusb_init_option { - /** Which option to set */ - enum libusb_option option; - /** An integer value used by the option (if applicable). */ - union { - int64_t ival; - } value; + LIBUSB_OPTION_MAX = 5 }; /** \ingroup libusb_lib @@ -1564,10 +1562,25 @@ struct libusb_init_option { typedef void (LIBUSB_CALL *libusb_log_cb)(libusb_context *ctx, enum libusb_log_level level, const char *str); +/** \ingroup libusb_lib + * Structure used for setting options through \ref libusb_init_context. + * + */ +struct libusb_init_option { + /** Which option to set */ + enum libusb_option option; + /** An integer value used by the option (if applicable). */ + union { + int64_t ival; + libusb_log_cb log_cbval; + } value; +}; + int LIBUSB_CALL libusb_init(libusb_context **ctx); int LIBUSB_CALL libusb_init_context(libusb_context **ctx, const struct libusb_init_option options[], int num_options); void LIBUSB_CALL libusb_exit(libusb_context *ctx); void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); +/* may be deprecated in the future in favor of lubusb_init_context()+libusb_set_option() */ void LIBUSB_CALL libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode); const struct libusb_version * LIBUSB_CALL libusb_get_version(void); int LIBUSB_CALL libusb_has_capability(uint32_t capability); diff --git a/libusb/libusbi.h b/libusb/libusbi.h index 72d2568..030e6b6 100644 --- a/libusb/libusbi.h +++ b/libusb/libusbi.h @@ -814,6 +814,7 @@ struct usbi_option { int is_set; union { int ival; + libusb_log_cb log_cbval; } arg; }; diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 34620df..cb1eb95 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11791 +#define LIBUSB_NANO 11792 |