diff options
author | Toby Gray <toby.gray@realvnc.com> | 2013-07-09 16:05:39 +0100 |
---|---|---|
committer | Pete Batard <pete@akeo.ie> | 2013-08-09 01:55:04 +0100 |
commit | 9222a548bdea026c89057b65caf2832785ea702e (patch) | |
tree | 741eb234bb044a1f367fbd4f9393b117f647767c | |
parent | b50a433c164996761344efb6c90594de7906fe56 (diff) | |
download | android_external_libusbx-9222a548bdea026c89057b65caf2832785ea702e.tar.gz android_external_libusbx-9222a548bdea026c89057b65caf2832785ea702e.tar.bz2 android_external_libusbx-9222a548bdea026c89057b65caf2832785ea702e.zip |
Core: Add compile time option to use native OS logging facility
* This change makes it easier to debug issues in UI applications
which don't necessarily have a console connected to stderr.
* Outputting to the debugger shouldn't occur in normal situations so
this change has to be explicitly enabled by a build-time config flag.
* Uses OutputDebugString() on Windows platforms or the syslog facility
on other OSes, if available.
* Also align the report of configure defaults to autotool's.
-rw-r--r-- | configure.ac | 25 | ||||
-rw-r--r-- | libusb/core.c | 37 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 | ||||
-rw-r--r-- | msvc/config.h | 3 |
4 files changed, 56 insertions, 11 deletions
diff --git a/configure.ac b/configure.ac index 669fcf8..72ed647 100644 --- a/configure.ac +++ b/configure.ac @@ -89,7 +89,7 @@ linux) AC_SUBST(OS_LINUX) AC_SEARCH_LIBS(clock_gettime, rt, [], [], -pthread) AC_ARG_ENABLE([udev], - [AC_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended, default: yes)])], + [AC_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])], [], [enable_udev="yes"]) if test "x$enable_udev" = "xyes" ; then # system has udev. use it or fail! @@ -158,7 +158,7 @@ fi AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=1], [timerfd_h=0]) AC_ARG_ENABLE([timerfd], [AS_HELP_STRING([--enable-timerfd], - [use timerfd for timing (default auto)])], + [use timerfd for timing [default=auto]])], [use_timerfd=$enableval], [use_timerfd='auto']) if test "x$use_timerfd" = "xyes" -a "x$timerfd_h" = "x0"; then @@ -193,23 +193,38 @@ if test "x$log_enabled" != "xno"; then fi AC_ARG_ENABLE([debug-log], [AS_HELP_STRING([--enable-debug-log], - [start with debug message logging enabled (default n)])], + [start with debug message logging enabled [default=no]])], [debug_log_enabled=$enableval], [debug_log_enabled='no']) if test "x$debug_log_enabled" != "xno"; then AC_DEFINE([ENABLE_DEBUG_LOGGING], 1, [Start with debug message logging enabled]) fi +AC_ARG_ENABLE([system-log], [AS_HELP_STRING([--enable-system-log], + [output logging messages to system wide log, if supported by the OS [default=no]])], + [system_log_enabled=$enableval], + [system_log_enabled='no']) +if test "x$system_log_enabled" != "xno"; then + AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], 1, [Enable output to system log]) +fi + +# Check if syslog is available in standard C library +AC_CHECK_HEADERS(syslog.h) +AC_CHECK_FUNC([syslog], [have_syslog=yes], [have_syslog=no]) +if test "x$have_syslog" != "xno"; then + AC_DEFINE([HAVE_SYSLOG_FUNC], 1, [syslog() function available]) +fi + # Examples build AC_ARG_ENABLE([examples-build], [AS_HELP_STRING([--enable-examples-build], - [build example applications (default n)])], + [build example applications [default=no]])], [build_examples=$enableval], [build_examples='no']) AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != "xno"]) # Tests build AC_ARG_ENABLE([tests-build], [AS_HELP_STRING([--enable-tests-build], - [build test applications (default n)])], + [build test applications [default=no]])], [build_tests=$enableval], [build_tests='no']) AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != "xno"]) diff --git a/libusb/core.c b/libusb/core.c index 4f2f366..1418415 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -33,6 +33,9 @@ #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #endif +#ifdef HAVE_SYSLOG_H +#include <syslog.h> +#endif #ifdef __ANDROID__ #include <android/log.h> @@ -2026,10 +2029,34 @@ int usbi_gettimeofday(struct timeval *tp, void *tzp) } #endif -static void usbi_log_str(struct libusb_context *ctx, const char * str) +static void usbi_log_str(struct libusb_context *ctx, + enum libusb_log_level level, const char * str) { - UNUSED(ctx); +#if defined(USE_SYSTEM_LOGGING_FACILITY) +#if defined(OS_WINDOWS) || defined(OS_WINCE) + /* Windows CE only supports the Unicode version of OutputDebugString. */ + WCHAR wbuf[USBI_MAX_LOG_LEN]; + MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf)); + OutputDebugStringW(wbuf); +#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_ERROR: syslog_level = LOG_ERR; break; + case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break; + case LIBUSB_LOG_LEVEL_NONE: break; + } + syslog(syslog_level, "%s", str); +#else /* All of gcc, Clang, XCode seem to use #warning */ +#warning System logging is not supported on this platform. Logging to stderr will be used instead. fputs(str, stderr); +#endif +#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, @@ -2084,8 +2111,8 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level, usbi_gettimeofday(&now, NULL); if ((global_debug) && (!has_debug_header_been_displayed)) { has_debug_header_been_displayed = 1; - usbi_log_str(ctx, "[timestamp] [threadID] facility level [function call] <message>\n"); - usbi_log_str(ctx, "--------------------------------------------------------------------------------\n"); + usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>\n"); + usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------\n"); } if (now.tv_usec < timestamp_origin.tv_usec) { now.tv_sec--; @@ -2143,7 +2170,7 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level, } strcpy(buf + header_len + text_len, USBI_LOG_LINE_END); - usbi_log_str(ctx, buf); + usbi_log_str(ctx, level, buf); #endif } diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 7ee3d41..ab03695 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10805 +#define LIBUSB_NANO 10806 diff --git a/msvc/config.h b/msvc/config.h index bb542c5..4b418db 100644 --- a/msvc/config.h +++ b/msvc/config.h @@ -25,6 +25,9 @@ /* Uncomment to start with debug message logging enabled */ // #define ENABLE_DEBUG_LOGGING 1 +/* Uncomment to enabling logging to system log */ +// #define USE_SYSTEM_LOGGING_FACILITY + /* type of second poll() argument */ #define POLL_NFDS_TYPE unsigned int |