Skip to content

Commit

Permalink
setting up a libusb log level accordingly to verbosity.
Browse files Browse the repository at this point in the history
  • Loading branch information
slyshykO committed Mar 25, 2020
1 parent a09ef54 commit 49f887d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
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

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

4 comments on commit 49f887d

@Crest
Copy link
Contributor

@Crest Crest commented on 49f887d Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code causes libstlink to output lots of debug messages to stdout of all places on FreeBSD. This is the output of st-probe --info on FreeBSD 12.1 to stdout:

LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
LIBUSB_TRANSFER: sync I/O done
Found 1 stlink programmers
 serial:     303637324646353035323530383237383637303733343039
 hla-serial: "\x30\x36\x37\x32\x46\x46\x35\x30\x35\x32\x35\x30\x38\x32\x37\x38\x36\x37\x30\x37\x33\x34\x30\x39"
 flash:      1048576 (pagesize: 2048)
 sram:       98304
 chipid:     0x0415
 descr:      L4xx

@Nightwalker-87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see the reason for the if-clause here anyway.
@slyshykO: Can you comment on this?

@slyshykO
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this issue caused because FreeBSD uses custom libusb variant. So, I propose to change ugly_libusb_log_level function for FreeBSD so that it properly compare log levels.

@Nightwalker-87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you submit a PR?

Please sign in to comment.