*/
void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
{
+#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
USBI_GET_CONTEXT(ctx);
if (!ctx->debug_fixed)
ctx->debug = level;
+#else
+ UNUSED(ctx);
+ UNUSED(level);
+#endif
}
/** \ingroup libusb_lib
int API_EXPORTED libusb_init(libusb_context **context)
{
struct libusb_device *dev, *next;
- char *dbg = getenv("LIBUSB_DEBUG");
size_t priv_size = usbi_backend.context_priv_size;
struct libusb_context *ctx;
static int first_init = 1;
goto err_unlock;
}
-#ifdef ENABLE_DEBUG_LOGGING
- ctx->debug = LIBUSB_LOG_LEVEL_DEBUG;
-#endif
-
- if (dbg) {
- ctx->debug = atoi(dbg);
- if (ctx->debug)
- ctx->debug_fixed = 1;
+#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
+ {
+ const char *dbg = getenv("LIBUSB_DEBUG");
+ if (dbg) {
+ ctx->debug = atoi(dbg);
+ if (ctx->debug)
+ ctx->debug_fixed = 1;
+ }
}
+#endif
/* default context should be initialized before calling usbi_dbg */
if (!usbi_default_context) {
return 0;
}
+#ifdef ENABLE_LOGGING
+
/* this is defined in libusbi.h if needed */
#ifdef LIBUSB_PRINTF_WIN32
/*
return ret;
}
-#endif
+#endif /* LIBUSB_PRINTF_WIN32 */
-static void usbi_log_str(struct libusb_context *ctx,
- enum libusb_log_level level, const char * str)
+static void usbi_log_str(enum libusb_log_level level, const char *str)
{
#if defined(USE_SYSTEM_LOGGING_FACILITY)
#if defined(OS_WINDOWS)
#elif defined(__ANDROID__)
int priority = ANDROID_LOG_UNKNOWN;
switch (level) {
- case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
- case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
+ case LIBUSB_LOG_LEVEL_NONE: return;
case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break;
+ case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
+ case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
- case LIBUSB_LOG_LEVEL_NONE: return;
}
__android_log_write(priority, "libusb", str);
#elif defined(HAVE_SYSLOG_FUNC)
int syslog_level = LOG_INFO;
switch (level) {
- case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
- case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
+ case LIBUSB_LOG_LEVEL_NONE: return;
case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break;
+ case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
+ case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break;
- case LIBUSB_LOG_LEVEL_NONE: return;
}
syslog(syslog_level, "%s", str);
#else /* All of gcc, Clang, XCode seem to use #warning */
#else
fputs(str, stderr);
#endif /* USE_SYSTEM_LOGGING_FACILITY */
- UNUSED(ctx);
UNUSED(level);
}
void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
const char *function, const char *format, va_list args)
{
- const char *prefix = "";
+ const char *prefix;
char buf[USBI_MAX_LOG_LEN];
struct timespec now;
int global_debug, header_len, text_len;
if (ctx) {
ctx_level = ctx->debug;
} else {
- char *dbg = getenv("LIBUSB_DEBUG");
+ const char *dbg = getenv("LIBUSB_DEBUG");
if (dbg)
ctx_level = atoi(dbg);
}
usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &now);
if ((global_debug) && (!has_debug_header_been_displayed)) {
has_debug_header_been_displayed = 1;
- usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
- usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
+ usbi_log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
+ usbi_log_str(LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
}
if (now.tv_nsec < timestamp_origin.tv_nsec) {
now.tv_sec--;
now.tv_nsec -= timestamp_origin.tv_nsec;
switch (level) {
- case LIBUSB_LOG_LEVEL_INFO:
- prefix = "info";
+ case LIBUSB_LOG_LEVEL_NONE:
+ return;
+ case LIBUSB_LOG_LEVEL_ERROR:
+ prefix = "error";
break;
case LIBUSB_LOG_LEVEL_WARNING:
prefix = "warning";
break;
- case LIBUSB_LOG_LEVEL_ERROR:
- prefix = "error";
+ case LIBUSB_LOG_LEVEL_INFO:
+ prefix = "info";
break;
case LIBUSB_LOG_LEVEL_DEBUG:
prefix = "debug";
break;
- case LIBUSB_LOG_LEVEL_NONE:
- return;
default:
prefix = "unknown";
break;
}
strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
- usbi_log_str(ctx, level, buf);
+ usbi_log_str(level, buf);
}
void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
va_end (args);
}
+#endif /* ENABLE_LOGGING */
+
/** \ingroup libusb_misc
* Returns a constant NULL-terminated string with the ASCII name of a libusb
* error or transfer status code. The caller must not free() the returned
} while (0)
#endif
+#ifdef ENABLE_LOGGING
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+#define snprintf usbi_snprintf
+#define vsnprintf usbi_vsnprintf
+int usbi_snprintf(char *dst, size_t size, const char *format, ...);
+int usbi_vsnprintf(char *dst, size_t size, const char *format, va_list ap);
+#define LIBUSB_PRINTF_WIN32
+#endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
+
void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
const char *function, const char *format, ...);
void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
const char *function, const char *format, va_list args);
-#if !defined(_MSC_VER) || _MSC_VER >= 1400
+#if !defined(_MSC_VER) || (_MSC_VER >= 1400)
-#ifdef ENABLE_LOGGING
#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
-#define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
-#else
-#define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
-#define usbi_dbg(...) do {} while(0)
-#endif
-#define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
-#define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
#define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
+#define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
+#define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
+#define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
-#else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
+#else /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
-#ifdef ENABLE_LOGGING
#define LOG_BODY(ctxt, level) \
{ \
va_list args; \
usbi_log_v(ctxt, level, "", format, args); \
va_end(args); \
}
-#else
-#define LOG_BODY(ctxt, level) \
-{ \
- (void)(ctxt); \
-}
-#endif
-static inline void usbi_info(struct libusb_context *ctx, const char *format, ...)
- LOG_BODY(ctx, LIBUSB_LOG_LEVEL_INFO)
-static inline void usbi_warn(struct libusb_context *ctx, const char *format, ...)
- LOG_BODY(ctx, LIBUSB_LOG_LEVEL_WARNING)
static inline void usbi_err(struct libusb_context *ctx, const char *format, ...)
LOG_BODY(ctx, LIBUSB_LOG_LEVEL_ERROR)
-
+static inline void usbi_warn(struct libusb_context *ctx, const char *format, ...)
+ LOG_BODY(ctx, LIBUSB_LOG_LEVEL_WARNING)
+static inline void usbi_info(struct libusb_context *ctx, const char *format, ...)
+ LOG_BODY(ctx, LIBUSB_LOG_LEVEL_INFO)
static inline void usbi_dbg(const char *format, ...)
LOG_BODY(NULL, LIBUSB_LOG_LEVEL_DEBUG)
-#endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
+#endif /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
+
+#else /* ENABLE_LOGGING */
+
+#define usbi_err(ctx, ...) do { (void)ctx; } while (0)
+#define usbi_warn(ctx, ...) do { (void)ctx; } while (0)
+#define usbi_info(ctx, ...) do { (void)ctx; } while (0)
+#define usbi_dbg(...) do {} while (0)
+
+#endif /* ENABLE_LOGGING */
#define USBI_GET_CONTEXT(ctx) \
do { \
struct pollfd;
struct libusb_context {
+#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
int debug;
int debug_fixed;
+#endif
/* internal event pipe, used for signalling occurrence of an internal event. */
int event_pipe[2];
#include "os/poll_windows.h"
#endif
-#if defined(_MSC_VER) && (_MSC_VER < 1900)
-#define snprintf usbi_snprintf
-#define vsnprintf usbi_vsnprintf
-int usbi_snprintf(char *dst, size_t size, const char *format, ...);
-int usbi_vsnprintf(char *dst, size_t size, const char *format, va_list ap);
-#define LIBUSB_PRINTF_WIN32
-#endif
-
struct usbi_pollfd {
/* must come first */
struct libusb_pollfd pollfd;
// Global variables
int windows_version = WINDOWS_UNDEFINED;
-static char windows_version_str[128] = "Undefined";
// Concurrency
static int concurrent_usage = -1;
static usbi_mutex_t autoclaim_lock;
arch = is_x64() ? "64-bit" : "32-bit";
if (w == NULL)
- snprintf(windows_version_str, sizeof(windows_version_str), "%s %u.%u %s",
- (vi.dwPlatformId == VER_PLATFORM_WIN32_NT ? "NT" : "??"),
+ usbi_dbg("Windows %s %u.%u %s", (vi.dwPlatformId == VER_PLATFORM_WIN32_NT ? "NT" : "??"),
(unsigned int)vi.dwMajorVersion, (unsigned int)vi.dwMinorVersion, arch);
else if (vi.wServicePackMinor)
- snprintf(windows_version_str, sizeof(windows_version_str), "%s SP%u.%u %s",
- w, vi.wServicePackMajor, vi.wServicePackMinor, arch);
+ usbi_dbg("Windows %s SP%u.%u %s", w, vi.wServicePackMajor, vi.wServicePackMinor, arch);
else if (vi.wServicePackMajor)
- snprintf(windows_version_str, sizeof(windows_version_str), "%s SP%u %s",
- w, vi.wServicePackMajor, arch);
+ usbi_dbg("Windows %s SP%u %s", w, vi.wServicePackMajor, arch);
else
- snprintf(windows_version_str, sizeof(windows_version_str), "%s %s",
- w, arch);
+ usbi_dbg("Windows %s %s", w, arch);
}
/*
// exit calls. If init is called more than exit, we will not exit properly
if (++concurrent_usage == 0) { // First init?
get_windows_version();
- usbi_dbg("Windows %s", windows_version_str);
if (windows_version == WINDOWS_UNSUPPORTED) {
usbi_err(ctx, "This version of Windows is NOT supported");
-#define LIBUSB_NANO 11207
+#define LIBUSB_NANO 11208