lib: Use GLib and libusb directly for debug output

Use GLib internally to output debug information, and tell about
libusb's LIBUSB_DEBUG envvar for libusb debug.

fp_set_debug() is now a no-op.

https://bugs.freedesktop.org/show_bug.cgi?id=106552
This commit is contained in:
Bastien Nocera 2018-05-23 17:48:55 +02:00
parent 363a1b3371
commit 32fcfde86b
30 changed files with 90 additions and 151 deletions

View file

@ -19,13 +19,14 @@
#define FP_COMPONENT "aeslib"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <libusb.h>
#include <glib.h>
#include "fp_internal.h"
#include "assembling.h"
#include "aeslib.h"

View file

@ -21,13 +21,14 @@
#define FP_COMPONENT "assembling"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <libusb.h>
#include <glib.h>
#include "fp_internal.h"
#include "assembling.h"
static unsigned int calc_error(struct fpi_frame_asmbl_ctx *ctx,

View file

@ -19,12 +19,12 @@
#define FP_COMPONENT "async"
#include "fp_internal.h"
#include <config.h>
#include <errno.h>
#include <glib.h>
#include "fp_internal.h"
/* Drivers call this when device initialisation has completed */
void fpi_drvcb_open_complete(struct fp_dev *dev, int status)
{

View file

@ -27,9 +27,6 @@
#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;
@ -116,52 +113,6 @@ GSList *opened_devices = NULL;
static GSList *registered_drivers = NULL;
void fpi_log(enum fpi_log_level level, const char *component,
const char *function, const char *format, ...)
{
va_list args;
FILE *stream = stdout;
const char *prefix;
if (!log_level)
return;
if (level == FPRINT_LOG_LEVEL_WARNING && log_level < 2)
return;
if (level == FPRINT_LOG_LEVEL_INFO && log_level < 3)
return;
switch (level) {
case FPRINT_LOG_LEVEL_INFO:
prefix = "info";
break;
case FPRINT_LOG_LEVEL_WARNING:
stream = stderr;
prefix = "warning";
break;
case FPRINT_LOG_LEVEL_ERROR:
stream = stderr;
prefix = "error";
break;
case FPRINT_LOG_LEVEL_DEBUG:
stream = stderr;
prefix = "debug";
break;
default:
stream = stderr;
prefix = "unknown";
break;
}
fprintf(stream, "%s:%s [%s] ", component ? component : "fp", prefix,
function);
va_start (args, format);
vfprintf(stream, format, args);
va_end (args);
fprintf(stream, "\n");
}
static void register_driver(struct fp_driver *drv)
{
if (drv->id == 0) {
@ -787,38 +738,11 @@ API_EXPORTED int fp_dev_get_img_height(struct fp_dev *dev)
* fp_set_debug:
* @level: the verbosity level
*
* 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.
* This call does nothing, see fp_init() for details.
*/
API_EXPORTED void fp_set_debug(int level)
{
if (log_level_fixed)
return;
log_level = level;
libusb_set_debug(fpi_usb_ctx, level);
/* Nothing */
}
/**
@ -827,11 +751,27 @@ API_EXPORTED void fp_set_debug(int level)
* Initialise libfprint. This function must be called before you attempt to
* use the library in any way.
*
* To enable debug output of libfprint specifically, use GLib's `G_MESSAGES_DEBUG`
* environment variable as explained in [Running and debugging GLib Applications](https://developer.gnome.org/glib/stable/glib-running.html#G_MESSAGES_DEBUG).
*
* The log domains used in libfprint are either `libfprint` or `libfprint-FP_COMPONENT`
* where `FP_COMPONENT` is defined in the source code for each driver, or component
* of the library. Starting with `all` and trimming down is advised.
*
* To enable debugging of libusb, for USB-based fingerprint reader drivers, use
* libusb's `LIBUSB_DEBUG` environment variable as explained in the
* [libusb-1.0 API Reference](http://libusb.sourceforge.net/api-1.0/#msglog).
*
* Example:
*
* ```
* LIBUSB_DEBUG=4 G_MESSAGES_DEBUG=all my-libfprint-application
* ```
*
* Returns: 0 on success, non-zero on error.
*/
API_EXPORTED int fp_init(void)
{
char *dbg = getenv("LIBFPRINT_DEBUG");
int r;
G_DEBUG_HERE();
@ -839,14 +779,6 @@ API_EXPORTED int fp_init(void)
if (r < 0)
return r;
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;

View file

@ -25,6 +25,8 @@
#define FP_COMPONENT "aes1610"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
@ -32,7 +34,6 @@
#include <assembling.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "driver_ids.h"

View file

@ -19,6 +19,8 @@
#define FP_COMPONENT "aes1660"
#include "fp_internal.h"
#include <stdio.h>
#include <errno.h>
@ -26,7 +28,6 @@
#include <libusb.h>
#include <fp_internal.h>
#include <assembling.h>
#include <aeslib.h>

View file

@ -23,6 +23,8 @@
#define FP_COMPONENT "aes2501"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
@ -30,7 +32,6 @@
#include <assembling.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aes2501.h"
#include "driver_ids.h"

View file

@ -23,6 +23,8 @@
#define FP_COMPONENT "aes2550"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
@ -30,7 +32,6 @@
#include <assembling.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aes2550.h"
#include "driver_ids.h"

View file

@ -19,6 +19,8 @@
#define FP_COMPONENT "aes2660"
#include "fp_internal.h"
#include <stdio.h>
#include <errno.h>
@ -26,8 +28,6 @@
#include <libusb.h>
#include <fp_internal.h>
#include <assembling.h>
#include <aeslib.h>

View file

@ -29,13 +29,14 @@
#define FP_COMPONENT "aes3500"
#include "fp_internal.h"
#include <errno.h>
#include <glib.h>
#include <libusb.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aes3k.h"
#include "driver_ids.h"

View file

@ -36,13 +36,14 @@
#define FP_COMPONENT "aes3k"
#include "fp_internal.h"
#include <errno.h>
#include <glib.h>
#include <libusb.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aes3k.h"

View file

@ -26,13 +26,14 @@
#define FP_COMPONENT "aes4000"
#include "fp_internal.h"
#include <errno.h>
#include <glib.h>
#include <libusb.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aes3k.h"
#include "driver_ids.h"

View file

@ -23,6 +23,8 @@
#define FP_COMPONENT "aesX660"
#include "fp_internal.h"
#include <stdio.h>
#include <errno.h>
@ -32,7 +34,6 @@
#include <assembling.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "aesx660.h"

View file

@ -20,10 +20,11 @@
#define FP_COMPONENT "elan"
#include "fp_internal.h"
#include <errno.h>
#include <libusb.h>
#include <assembling.h>
#include <fp_internal.h>
#include <fprint.h>
#include "elan.h"

View file

@ -33,6 +33,10 @@
*
*/
#define FP_COMPONENT "etes603"
#include "fp_internal.h"
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
@ -41,8 +45,6 @@
#include <libusb.h>
#include <glib.h>
#define FP_COMPONENT "etes603"
#include <fp_internal.h>
#include "driver_ids.h"
/* libusb defines */

View file

@ -17,15 +17,16 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define FP_COMPONENT "fdu2000"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <libusb.h>
#define FP_COMPONENT "fdu2000"
#include <fp_internal.h>
#include "driver_ids.h"
#ifndef HAVE_MEMMEM

View file

@ -25,14 +25,14 @@
#define FP_COMPONENT "upeksonly"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <libusb.h>
#include <fp_internal.h>
#include <assembling.h>
#include "upeksonly.h"

View file

@ -20,10 +20,11 @@
#define FP_COMPONENT "upektc"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <libusb.h>
#include <fp_internal.h>
#include "upektc.h"
#include "driver_ids.h"

View file

@ -19,13 +19,14 @@
#define FP_COMPONENT "upektc_img"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <libusb.h>
#include <aeslib.h>
#include <fp_internal.h>
#include "upektc_img.h"
#include "driver_ids.h"

View file

@ -27,14 +27,14 @@
#define FP_COMPONENT "upekts"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <libusb.h>
#include <fp_internal.h>
#include "driver_ids.h"
#define EP_IN (1 | LIBUSB_ENDPOINT_IN)

View file

@ -20,6 +20,8 @@
#define FP_COMPONENT "uru4000"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
@ -28,8 +30,6 @@
#include <pk11pub.h>
#include <libusb.h>
#include <fp_internal.h>
#include "driver_ids.h"
#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)

View file

@ -19,6 +19,8 @@
#define FP_COMPONENT "vcom5s"
#include "fp_internal.h"
/* TODO:
* calibration?
* image size: windows gets 300x300 through vpas enrollment util?
@ -32,8 +34,6 @@
#include <glib.h>
#include <libusb.h>
#include <fp_internal.h>
#include "driver_ids.h"
#define CTRL_IN 0xc0

View file

@ -19,9 +19,10 @@
#define FP_COMPONENT "vfs0050"
#include "fp_internal.h"
#include <errno.h>
#include <string.h>
#include <fp_internal.h>
#include <assembling.h>
#include "driver_ids.h"

View file

@ -17,14 +17,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define FP_COMPONENT "vfs101"
#include "fp_internal.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define FP_COMPONENT "vfs101"
#include <fp_internal.h>
#include "driver_ids.h"
/* Input-Output usb endpoint */

View file

@ -21,6 +21,8 @@
#define FP_COMPONENT "vfs301"
#include "fp_internal.h"
#include <errno.h>
#include <signal.h>
#include <string.h>
@ -32,8 +34,6 @@
#include "vfs301_proto.h"
#include <unistd.h>
#include <fp_internal.h>
#include "driver_ids.h"
/************************** GENERIC STUFF *************************************/

View file

@ -19,11 +19,11 @@
#define FP_COMPONENT "drv"
#include "fp_internal.h"
#include <config.h>
#include <errno.h>
#include "fp_internal.h"
/* SSM: sequential state machine
* Asynchronous driver design encourages some kind of state machine behind it.
* In most cases, the state machine is entirely linear - you only go to the

View file

@ -21,8 +21,13 @@
#define __FPRINT_INTERNAL_H__
#include <config.h>
#include <stdint.h>
#ifdef FP_COMPONENT
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "libfprint-"FP_COMPONENT
#endif
#include <stdint.h>
#include <glib.h>
#include <libusb.h>
@ -34,26 +39,10 @@
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
enum fpi_log_level {
FPRINT_LOG_LEVEL_DEBUG,
FPRINT_LOG_LEVEL_INFO,
FPRINT_LOG_LEVEL_WARNING,
FPRINT_LOG_LEVEL_ERROR,
};
void fpi_log(enum fpi_log_level, const char *component, const char *function,
const char *format, ...);
#ifndef FP_COMPONENT
#define FP_COMPONENT NULL
#endif
#define _fpi_log(level, fmt...) fpi_log(level, FP_COMPONENT, __FUNCTION__, fmt)
#define fp_dbg(fmt...) _fpi_log(FPRINT_LOG_LEVEL_DEBUG, fmt)
#define fp_info(fmt...) _fpi_log(FPRINT_LOG_LEVEL_INFO, fmt)
#define fp_warn(fmt...) _fpi_log(FPRINT_LOG_LEVEL_WARNING, fmt)
#define fp_err(fmt...) _fpi_log(FPRINT_LOG_LEVEL_ERROR, fmt)
#define fp_dbg g_debug
#define fp_info g_debug
#define fp_warn g_warning
#define fp_err g_error
#ifndef NDEBUG
#define BUG_ON(condition) \

View file

@ -19,6 +19,8 @@
#define FP_COMPONENT "poll"
#include "fp_internal.h"
#include <config.h>
#include <errno.h>
#include <time.h>
@ -27,8 +29,6 @@
#include <glib.h>
#include <libusb.h>
#include "fp_internal.h"
/**
* SECTION:events
* @title: Initialisation and events handling

View file

@ -19,11 +19,11 @@
#define FP_COMPONENT "sync"
#include "fp_internal.h"
#include <config.h>
#include <errno.h>
#include "fp_internal.h"
struct sync_open_data {
struct fp_dev *dev;
int status;

View file

@ -9,6 +9,7 @@ project('libfprint', [ 'c', 'cpp' ],
meson_version: '>= 0.45.0')
add_project_arguments([ '-D_GNU_SOURCE' ], language: 'c')
add_project_arguments([ '-DG_LOG_DOMAIN="libfprint"' ], language: 'c')
libfprint_conf = configuration_data()