git: 25c225106475 - main - libusb: Implement libusb_set_log_cb
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 23 Jun 2026 14:29:32 UTC
The branch main has been updated by aokblast:
URL: https://cgit.FreeBSD.org/src/commit/?id=25c2251064752b0ced310b1ed3795c8a68849b1c
commit 25c2251064752b0ced310b1ed3795c8a68849b1c
Author: ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-06-23 14:03:09 +0000
Commit: ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-06-23 14:28:49 +0000
libusb: Implement libusb_set_log_cb
Reviewed by: adrian
Event: Halifax Hackathon 202606
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D57770
---
lib/libusb/Makefile | 2 ++
lib/libusb/libusb.3 | 18 +++++++++++++++++-
lib/libusb/libusb.h | 6 ++++++
lib/libusb/libusb10.c | 12 ++++++++++++
4 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile
index f11083c94c86..af031ecb0544 100644
--- a/lib/libusb/Makefile
+++ b/lib/libusb/Makefile
@@ -73,6 +73,8 @@ MLINKS += libusb.3 libusb_has_capability.3
MLINKS += libusb.3 libusb_strerror.3
MLINKS += libusb.3 libusb_error_name.3
MLINKS += libusb.3 libusb_set_debug.3
+MLINKS += libusb.3 libusb_set_option.3
+MLINKS += libusb.3 libusb_set_log_cb.3
MLINKS += libusb.3 libusb_get_device_list.3
MLINKS += libusb.3 libusb_free_device_list.3
MLINKS += libusb.3 libusb_get_bus_number.3
diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3
index ff1d0b950b4b..42b2e90f148e 100644
--- a/lib/libusb/libusb.3
+++ b/lib/libusb/libusb.3
@@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd June 22, 2026
+.Dd June 23, 2026
.Dt LIBUSB 3
.Os
.Sh NAME
@@ -125,6 +125,22 @@ customize default callback function with
.Fa value .
.El
.Pp
+.Ft void
+.Fn libusb_set_log_cb "libusb_context *ctx" "libusb_log_cb cb" "int mode"
+Set the log handler to
+.Fa cb .
+The
+.Fa mode
+argument is a bitmask that selects which log messages the callback handles:
+.Bl -tag -width LIBUSB_LOG_CB_CONTEXT -offset indent
+.It Va LIBUSB_LOG_CB_GLOBAL
+The callback handles global log messages and is inherited by all
+subsequently created contexts.
+.It Va LIBUSB_LOG_CB_CONTEXT
+The callback handles log messages of the context
+.Fa ctx .
+.El
+.Pp
.Ft const char *
.Fn libusb_strerror "int code"
Get the ASCII representation of the error given by the
diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h
index 95f7faf900f8..1cf860cbb8ab 100644
--- a/lib/libusb/libusb.h
+++ b/lib/libusb/libusb.h
@@ -296,6 +296,11 @@ enum libusb_option {
LIBUSB_OPTION_MAX = 4,
};
+enum libusb_log_cb_mode {
+ LIBUSB_LOG_CB_GLOBAL = (1 << 0),
+ LIBUSB_LOG_CB_CONTEXT = (1 << 1),
+};
+
/* libusb structures */
struct libusb_context;
@@ -675,6 +680,7 @@ int libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int
void libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id);
uint32_t libusb_transfer_get_stream_id(struct libusb_transfer *transfer);
int libusb_set_option(libusb_context *ctx, enum libusb_option option, ...);
+void libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode);
#if 0
{ /* indent fix */
diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c
index bd3c25c0f052..02507110d909 100644
--- a/lib/libusb/libusb10.c
+++ b/lib/libusb/libusb10.c
@@ -2160,3 +2160,15 @@ end:
va_end(args);
return (err);
}
+
+void
+libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode)
+{
+ if ((mode & LIBUSB_LOG_CB_GLOBAL) && usbi_default_context != NULL)
+ usbi_default_context->log_cb = cb;
+ if (mode & LIBUSB_LOG_CB_CONTEXT) {
+ ctx = GET_CONTEXT(ctx);
+ if (ctx != NULL)
+ ctx->log_cb = cb;
+ }
+}