Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting up a libusb log level accordingly to verbosity. #894

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ typedef struct flash_loader {
size_t sram_size; // stlink_chipid_params.sram_size, set by stlink_load_device_params()

// bootloader
// sys_base and sys_size are not used by the tools, but are only there to
// download the bootloader code (see tests/sg.c)
// sys_base and sys_size are not used by the tools, but are only there to
// download the bootloader code (see tests/sg.c)
stm32_addr_t sys_base; // stlink_chipid_params.bootrom_base, set by stlink_load_device_params()
size_t sys_size; // stlink_chipid_params.bootrom_size, set by stlink_load_device_params()

Expand Down Expand Up @@ -243,6 +243,7 @@ typedef struct flash_loader {
#include "stlink/chipid.h"
#include "stlink/flash_loader.h"
#include "stlink/version.h"
#include "stlink/logging.h"

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions include/stlink/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum ugly_loglevel {

int ugly_init(int maximum_threshold);
int ugly_log(int level, const char *tag, const char *format, ...);
int ugly_libusb_log_level(enum ugly_loglevel v);

#define UGLY_LOG_FILE (strstr(__FILE__, "/") != NULL ? \
strrchr(__FILE__, '/') + 1 : strstr(__FILE__, "\\") != NULL ? \
Expand Down
21 changes: 21 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,24 @@ int ugly_log(int level, const char *tag, const char *format, ...) {
va_end(args);
return 1;
}


/*
* Log message levels.
* - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
* - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr
* - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
* - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stderr
* - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stderr
*/
int ugly_libusb_log_level(enum ugly_loglevel v)
{
switch (v) {
case UDEBUG: return 4;
case UINFO: return 3;
case UWARN: return 2;
case UERROR: return 1;
};

return 2;
}
6 changes: 6 additions & 0 deletions src/sg.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,12 @@ static stlink_t* stlink_open(const int verbose) {
return NULL;
}

#if LIBUSB_API_VERSION < 0x01000106
libusb_set_debug(slsg->libusb_ctx, ugly_libusb_log_level(verbose));
#else
libusb_set_option(slsg->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, ugly_libusb_log_level(verbose));
#endif

slyshykO marked this conversation as resolved.
Show resolved Hide resolved
slsg->usb_handle = libusb_open_device_with_vid_pid(slsg->libusb_ctx, STLINK_USB_VID_ST, STLINK_USB_PID_STLINK);
if (slsg->usb_handle == NULL) {
WLOG("Failed to find an stlink v1 by VID:PID\n");
Expand Down
6 changes: 6 additions & 0 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,12 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[ST
goto on_error;
}

#if LIBUSB_API_VERSION < 0x01000106
libusb_set_debug(slu->libusb_ctx, ugly_libusb_log_level(verbose));
#else
libusb_set_option(slu->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, ugly_libusb_log_level(verbose));
#endif

libusb_device **list;
/** @todo We should use ssize_t and use it as a counter if > 0. As per libusb API: ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list) */
int cnt = (int) libusb_get_device_list(slu->libusb_ctx, &list);
Expand Down