From: Antonio Ospite Date: Tue, 28 Feb 2012 22:51:37 +0000 (+0100) Subject: am7xxx: add a simple logging infrastructure X-Git-Tag: v0.1.0~1^2~29 X-Git-Url: https://git.ao2.it/libam7xxx.git/commitdiff_plain/d2b3bd440d4700abf2e6b503d3a864af1496026e am7xxx: add a simple logging infrastructure --- diff --git a/src/am7xxx.c b/src/am7xxx.c index 20b0ddf..4cfc9a9 100644 --- a/src/am7xxx.c +++ b/src/am7xxx.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,27 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +/* If we're not using GNU C, elide __attribute__ + * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html) + */ +#ifndef __GNUC__ +# define __attribute__(x) /*NOTHING*/ +#endif + +static void log_message(am7xxx_context *ctx, + int level, + const char *function, + int line, + const char *fmt, + ...) __attribute__ ((format (printf, 5, 6))); + +#define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__) +#define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__) +#define warning(ctx, ...) log_message(ctx, AM7XXX_LOG_WARNING, __func__, 0, __VA_ARGS__) +#define info(ctx, ...) log_message(ctx, AM7XXX_LOG_INFO, __func__, 0, __VA_ARGS__) +#define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) +#define trace(ctx, ...) log_message(ctx, AM7XXX_LOG_TRACE, NULL, 0, __VA_ARGS__) + struct am7xxx_usb_device_descriptor { const char *name; uint16_t vendor_id; @@ -61,6 +83,7 @@ struct _am7xxx_device { struct _am7xxx_context { libusb_context *usb_context; + int log_level; am7xxx_device *devices_list; }; @@ -322,6 +345,35 @@ static int send_header(am7xxx_device *dev, struct am7xxx_header *h) return ret; } +/* When level == AM7XXX_LOG_FATAL do not check the log_level from the context + * and print the message unconditionally, this makes it possible to print + * fatal messages even early on initialization, before the context has been + * set up */ +static void log_message(am7xxx_context *ctx, + int level, + const char *function, + int line, + const char *fmt, + ...) +{ + va_list ap; + + if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) { + if (function) { + fprintf(stderr, "%s", function); + if (line) + fprintf(stderr, "[%d]", line); + fprintf(stderr, ": "); + } + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + } + + return; +} + static am7xxx_device *add_new_device(am7xxx_context *ctx) { am7xxx_device **devices_list; @@ -500,6 +552,9 @@ int am7xxx_init(am7xxx_context **ctx) } memset(*ctx, 0, sizeof(**ctx)); + /* Set the highest log level during initialization */ + (*ctx)->log_level = AM7XXX_LOG_TRACE; + ret = libusb_init(&((*ctx)->usb_context)); if (ret < 0) goto out_free_context; @@ -513,6 +568,8 @@ int am7xxx_init(am7xxx_context **ctx) goto out; } + /* Set a quieter log level as default for normal operation */ + (*ctx)->log_level = AM7XXX_LOG_ERROR; return 0; out_free_context: @@ -544,6 +601,11 @@ void am7xxx_shutdown(am7xxx_context *ctx) ctx = NULL; } +void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level) +{ + ctx->log_level = log_level; +} + int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev, unsigned int device_index) { diff --git a/src/am7xxx.h b/src/am7xxx.h index b6a2a9a..13cb83d 100644 --- a/src/am7xxx.h +++ b/src/am7xxx.h @@ -31,6 +31,15 @@ struct _am7xxx_device; typedef struct _am7xxx_device am7xxx_device; typedef enum { + AM7XXX_LOG_FATAL = 0, + AM7XXX_LOG_ERROR = 1, + AM7XXX_LOG_WARNING = 2, + AM7XXX_LOG_INFO = 3, + AM7XXX_LOG_DEBUG = 4, + AM7XXX_LOG_TRACE = 5, +} am7xxx_log_level; + +typedef enum { AM7XXX_IMAGE_FORMAT_JPEG = 1, AM7XXX_IMAGE_FORMAT_NV12 = 2, } am7xxx_image_format; @@ -47,6 +56,8 @@ int am7xxx_init(am7xxx_context **ctx); void am7xxx_shutdown(am7xxx_context *ctx); +void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level); + int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev, unsigned int device_index);