diff --git a/examples/enroll.c b/examples/enroll.c index 7d07788..f2ba9be 100644 --- a/examples/enroll.c +++ b/examples/enroll.c @@ -115,6 +115,7 @@ int main(void) fprintf(stderr, "Failed to initialize libfprint\n"); exit(1); } + fp_set_debug(3); discovered_devs = fp_discover_devs(); if (!discovered_devs) { diff --git a/examples/img_capture.c b/examples/img_capture.c index d45b8b8..6528031 100644 --- a/examples/img_capture.c +++ b/examples/img_capture.c @@ -47,6 +47,7 @@ int main(void) fprintf(stderr, "Failed to initialize libfprint\n"); exit(1); } + fp_set_debug(3); discovered_devs = fp_discover_devs(); if (!discovered_devs) { diff --git a/examples/img_capture_continuous.c b/examples/img_capture_continuous.c index 752f710..701fd69 100644 --- a/examples/img_capture_continuous.c +++ b/examples/img_capture_continuous.c @@ -146,6 +146,7 @@ int main(void) fprintf(stderr, "Failed to initialize libfprint\n"); exit(1); } + fp_set_debug(3); discovered_devs = fp_discover_devs(); if (!discovered_devs) { diff --git a/examples/verify.c b/examples/verify.c index 1e1dd4f..60a2ccb 100644 --- a/examples/verify.c +++ b/examples/verify.c @@ -91,6 +91,7 @@ int main(void) fprintf(stderr, "Failed to initialize libfprint\n"); exit(1); } + fp_set_debug(3); discovered_devs = fp_discover_devs(); if (!discovered_devs) { diff --git a/examples/verify_live.c b/examples/verify_live.c index 9a7b6a9..27acb3b 100644 --- a/examples/verify_live.c +++ b/examples/verify_live.c @@ -134,6 +134,7 @@ int main(void) fprintf(stderr, "Failed to initialize libfprint\n"); exit(1); } + fp_set_debug(3); discovered_devs = fp_discover_devs(); if (!discovered_devs) { diff --git a/libfprint/core.c b/libfprint/core.c index b3acea6..37a4e03 100644 --- a/libfprint/core.c +++ b/libfprint/core.c @@ -20,12 +20,16 @@ #include #include #include +#include #include #include #include "fp_internal.h" +static int log_level = 0; +static int log_level_fixed = 0; + libusb_context *fpi_usb_ctx = NULL; GSList *opened_devices = NULL; @@ -41,13 +45,13 @@ GSList *opened_devices = NULL; * designed so that you only have to do this once - by integrating your * software with libfprint, you'll be supporting all the fingerprint readers * that we have got our hands on. As such, the API is rather general (and - * therefore hopefully easy to comprehend!), and does it's best to hide the + * therefore hopefully easy to comprehend!), and does its best to hide the * technical details that required to operate the hardware. * * This documentation is not aimed at developers wishing to develop and * contribute fingerprint device drivers to libfprint. * - * Feedback on this API and it's associated documentation is appreciated. Was + * Feedback on this API and its associated documentation is appreciated. Was * anything unclear? Does anything seem unreasonably complicated? Is anything * missing? Let us know on the * mailing list. @@ -284,6 +288,15 @@ void fpi_log(enum fpi_log_level level, const char *component, FILE *stream = stdout; const char *prefix; +#ifndef ENABLE_DEBUG_LOGGING + if (!log_level) + return; + if (level == LOG_LEVEL_WARNING && log_level < 2) + return; + if (level == LOG_LEVEL_INFO && log_level < 3) + return; +#endif + switch (level) { case LOG_LEVEL_INFO: prefix = "info"; @@ -826,6 +839,44 @@ API_EXPORTED int fp_dev_get_img_height(struct fp_dev *dev) return fpi_imgdev_get_img_height(imgdev); } +/** \ingroup core + * Set message verbosity. + * - Level 0: no messages ever printed by the library (default) + * - Level 1: error messages are printed to stderr + * - Level 2: warning and error messages are printed to stderr + * - Level 3: informational messages are printed to stdout, warning and error + * messages are printed to stderr + * + * The default level is 0, which means no messages are ever printed. If you + * choose to increase the message verbosity level, ensure that your + * application does not close the stdout/stderr file descriptors. + * + * You are advised to set level 3. libfprint is conservative with its message + * logging and most of the time, will only log messages that explain error + * conditions and other oddities. This will help you debug your software. + * + * If the LIBFPRINT_DEBUG environment variable was set when libfprint was + * initialized, this function does nothing: the message verbosity is fixed + * to the value in the environment variable. + * + * If libfprint was compiled without any message logging, this function does + * nothing: you'll never get any messages. + * + * If libfprint was compiled with verbose debug message logging, this function + * does nothing: you'll always get messages from all levels. + * + * \param ctx the context to operate on, or NULL for the default context + * \param level debug level to set + */ +API_EXPORTED void fp_set_debug(int level) +{ + if (log_level_fixed) + return; + + log_level = level; + libusb_set_debug(fpi_usb_ctx, level); +} + /** \ingroup core * Initialise libfprint. This function must be called before you attempt to * use the library in any way. @@ -833,6 +884,7 @@ API_EXPORTED int fp_dev_get_img_height(struct fp_dev *dev) */ API_EXPORTED int fp_init(void) { + char *dbg = getenv("LIBFPRINT_DEBUG"); int r; fp_dbg(""); @@ -840,7 +892,14 @@ API_EXPORTED int fp_init(void) if (r < 0) return r; - libusb_set_debug(fpi_usb_ctx, 3); + if (dbg) { + log_level = atoi(dbg); + if (log_level) { + log_level_fixed = 1; + libusb_set_debug(fpi_usb_ctx, log_level); + } + } + register_drivers(); fpi_poll_init(); return 0; diff --git a/libfprint/fprint.h b/libfprint/fprint.h index 29e6eb1..319b600 100644 --- a/libfprint/fprint.h +++ b/libfprint/fprint.h @@ -297,6 +297,7 @@ void fp_set_pollfd_notifiers(fp_pollfd_added_cb added_cb, /* Library */ int fp_init(void); void fp_exit(void); +void fp_set_debug(int level); /* Asynchronous I/O */