core: Slightly refactor and improve logging code
authorChris Dickens <christopher.a.dickens@gmail.com>
Wed, 12 Jul 2017 18:29:37 +0000 (11:29 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Wed, 12 Jul 2017 18:29:37 +0000 (11:29 -0700)
This change refactors the code that reads the LIBUSB_DEBUG environment
variable into a function to avoid code duplication and ensure that the
value is coerced into a valid libusb_log_level value.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/core.c
libusb/libusbi.h
libusb/version_nano.h

index f842fe9..5b47194 100644 (file)
@@ -2030,14 +2030,36 @@ 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;
+       if (!ctx->debug_fixed) {
+               level = CLAMP(level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
+               ctx->debug = (enum libusb_log_level)level;
+       }
 #else
        UNUSED(ctx);
        UNUSED(level);
 #endif
 }
 
+#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
+/* returns the log level as defined in the LIBUSB_DEBUG environment variable.
+ * if LIBUSB_DEBUG is not present or not a number, returns LIBUSB_LOG_LEVEL_NONE.
+ * value is clamped to ensure it is within the valid range of possibilities.
+ */
+static enum libusb_log_level get_env_debug_level(void)
+{
+       const char *dbg = getenv("LIBUSB_DEBUG");
+       enum libusb_log_level level;
+       if (dbg) {
+               int dbg_level = atoi(dbg);
+               dbg_level = CLAMP(dbg_level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
+               level = (enum libusb_log_level)dbg_level;
+       } else {
+               level = LIBUSB_LOG_LEVEL_NONE;
+       }
+       return level;
+}
+#endif
+
 /** \ingroup libusb_lib
  * Initialize libusb. This function must be called before calling any other
  * libusb function.
@@ -2079,14 +2101,9 @@ int API_EXPORTED libusb_init(libusb_context **context)
        }
 
 #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;
-               }
-       }
+       ctx->debug = get_env_debug_level();
+       if (ctx->debug != LIBUSB_LOG_LEVEL_NONE)
+               ctx->debug_fixed = 1;
 #endif
 
        /* default context should be initialized before calling usbi_dbg */
@@ -2351,18 +2368,15 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
        global_debug = 1;
        UNUSED(ctx);
 #else
-       int ctx_level = 0;
+       enum libusb_log_level ctx_level = LIBUSB_LOG_LEVEL_NONE;
 
        USBI_GET_CONTEXT(ctx);
-       if (ctx) {
+       if (ctx)
                ctx_level = ctx->debug;
-       } else {
-               const char *dbg = getenv("LIBUSB_DEBUG");
-               if (dbg)
-                       ctx_level = atoi(dbg);
-       }
-       global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
-       if (!ctx_level)
+       else
+               ctx_level = get_env_debug_level();
+
+       if (ctx_level == LIBUSB_LOG_LEVEL_NONE)
                return;
        if (level == LIBUSB_LOG_LEVEL_WARNING && ctx_level < LIBUSB_LOG_LEVEL_WARNING)
                return;
@@ -2370,6 +2384,8 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
                return;
        if (level == LIBUSB_LOG_LEVEL_DEBUG && ctx_level < LIBUSB_LOG_LEVEL_DEBUG)
                return;
+
+       global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
 #endif
 
        usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &now);
index 9e364ca..2818125 100644 (file)
@@ -178,6 +178,9 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
        const typeof( ((type *)0)->member ) *mptr = (ptr);      \
        (type *)( (char *)mptr - offsetof(type,member) );})
 
+#ifndef CLAMP
+#define CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
+#endif
 #ifndef MIN
 #define MIN(a, b)      ((a) < (b) ? (a) : (b))
 #endif
@@ -288,7 +291,7 @@ struct pollfd;
 
 struct libusb_context {
 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
-       int debug;
+       enum libusb_log_level debug;
        int debug_fixed;
 #endif
 
index e245291..f8a1bfb 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11209
+#define LIBUSB_NANO 11210