From b06ab16e8d40010876ccab3bb63f8aa3d4f8ae5d Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Fri, 3 Apr 2015 18:23:49 +0200 Subject: [PATCH] Add function to set custom log function. --- libjaylink/core.c | 3 ++ libjaylink/libjaylink-internal.h | 18 +++++++++--- libjaylink/libjaylink.h | 7 +++++ libjaylink/log.c | 62 +++++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/libjaylink/core.c b/libjaylink/core.c index 5e62291..f8a100d 100644 --- a/libjaylink/core.c +++ b/libjaylink/core.c @@ -63,6 +63,9 @@ JAYLINK_API int jaylink_init(struct jaylink_context **ctx) /* Show error and warning messages by default. */ context->log_level = JAYLINK_LOG_LEVEL_WARNING; + context->log_callback = &log_vprintf; + context->log_callback_data = NULL; + *ctx = context; return JAYLINK_OK; diff --git a/libjaylink/libjaylink-internal.h b/libjaylink/libjaylink-internal.h index 661ec36..9b97493 100644 --- a/libjaylink/libjaylink-internal.h +++ b/libjaylink/libjaylink-internal.h @@ -24,6 +24,8 @@ #include #include +#include "libjaylink.h" + /** * @file * @@ -51,6 +53,10 @@ struct jaylink_context { struct list *devs; /** Current log level. */ int log_level; + /** Log callback function. */ + jaylink_log_callback log_callback; + /** User data to be passed to the log callback function. */ + void *log_callback_data; }; struct jaylink_device { @@ -145,12 +151,16 @@ JAYLINK_PRIV void list_free(struct list *list); /*--- log.c -----------------------------------------------------------------*/ -JAYLINK_PRIV void log_err(struct jaylink_context *ctx, const char *format, ...); -JAYLINK_PRIV void log_warn(struct jaylink_context *ctx, +JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx, int level, + const char *format, va_list args, void *user_data); +JAYLINK_PRIV void log_err(const struct jaylink_context *ctx, + const char *format, ...); +JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx, + const char *format, ...); +JAYLINK_PRIV void log_info(const struct jaylink_context *ctx, const char *format, ...); -JAYLINK_PRIV void log_info(struct jaylink_context *ctx, +JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx, const char *format, ...); -JAYLINK_PRIV void log_dbg(struct jaylink_context *ctx, const char *format, ...); /*--- transport.c -----------------------------------------------------------*/ diff --git a/libjaylink/libjaylink.h b/libjaylink/libjaylink.h index 9b99adb..cd31755 100644 --- a/libjaylink/libjaylink.h +++ b/libjaylink/libjaylink.h @@ -21,6 +21,7 @@ #define LIBJAYLINK_LIBJAYLINK_H #include +#include #include /** @@ -254,6 +255,10 @@ struct jaylink_device_handle; #define JAYLINK_API #endif +/** Log callback function type. */ +typedef int (*jaylink_log_callback)(const struct jaylink_context *ctx, + int level, const char *format, va_list args, void *user_data); + /*--- core.c ----------------------------------------------------------------*/ JAYLINK_API int jaylink_init(struct jaylink_context **ctx); @@ -317,6 +322,8 @@ JAYLINK_API int jaylink_jtag_set_trst(struct jaylink_device_handle *devh); JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx, int level); JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx); +JAYLINK_API int jaylink_log_set_callback(struct jaylink_context *ctx, + jaylink_log_callback callback, void *user_data); /*--- swd.c -----------------------------------------------------------------*/ diff --git a/libjaylink/log.c b/libjaylink/log.c index 27c1761..b30c0ee 100644 --- a/libjaylink/log.c +++ b/libjaylink/log.c @@ -68,24 +68,57 @@ JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx) return ctx->log_level; } +/** + * Set the libjaylink log callback function. + * + * @param[in,out] ctx libjaylink context. + * @param[in] callback Callback function to use, or NULL to use the default log + * function. + * @param[in] user_data User data to be passed to the callback function. + * + * @retval JAYLINK_OK Success. + * @retval JAYLINK_ERR_ARG Invalid arguments. + */ +JAYLINK_API int jaylink_log_set_callback(struct jaylink_context *ctx, + jaylink_log_callback callback, void *user_data) +{ + if (!ctx) + return JAYLINK_ERR_ARG; + + if (callback) { + ctx->log_callback = callback; + ctx->log_callback_data = user_data; + } else { + ctx->log_callback = &log_vprintf; + ctx->log_callback_data = NULL; + } + + return JAYLINK_OK; +} + /** @private */ -static void log_vprintf(struct jaylink_context *ctx, int level, - const char *format, va_list args) +JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx, int level, + const char *format, va_list args, void *user_data) { + (void)user_data; + /* * Filter out messages with higher verbosity than the verbosity of the * current log level. */ if (level > ctx->log_level) - return; + return 0; fprintf(stderr, "jaylink: "); vfprintf(stderr, format, args); fprintf(stderr, "\n"); + + return 0; } /** @private */ -JAYLINK_PRIV void log_err(struct jaylink_context *ctx, const char *format, ...) +JAYLINK_PRIV void log_err(const struct jaylink_context *ctx, + const char *format, ...) { va_list args; @@ -93,12 +126,14 @@ JAYLINK_PRIV void log_err(struct jaylink_context *ctx, const char *format, ...) return; va_start(args, format); - log_vprintf(ctx, JAYLINK_LOG_LEVEL_ERROR, format, args); + ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_ERROR, format, args, + ctx->log_callback_data); va_end(args); } /** @private */ -JAYLINK_PRIV void log_warn(struct jaylink_context *ctx, const char *format, ...) +JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx, + const char *format, ...) { va_list args; @@ -106,12 +141,14 @@ JAYLINK_PRIV void log_warn(struct jaylink_context *ctx, const char *format, ...) return; va_start(args, format); - log_vprintf(ctx, JAYLINK_LOG_LEVEL_WARNING, format, args); + ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_WARNING, format, args, + ctx->log_callback_data); va_end(args); } /** @private */ -JAYLINK_PRIV void log_info(struct jaylink_context *ctx, const char *format, ...) +JAYLINK_PRIV void log_info(const struct jaylink_context *ctx, + const char *format, ...) { va_list args; @@ -119,12 +156,14 @@ JAYLINK_PRIV void log_info(struct jaylink_context *ctx, const char *format, ...) return; va_start(args, format); - log_vprintf(ctx, JAYLINK_LOG_LEVEL_INFO, format, args); + ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_INFO, format, args, + ctx->log_callback_data); va_end(args); } /** @private */ -JAYLINK_PRIV void log_dbg(struct jaylink_context *ctx, const char *format, ...) +JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx, + const char *format, ...) { va_list args; @@ -132,6 +171,7 @@ JAYLINK_PRIV void log_dbg(struct jaylink_context *ctx, const char *format, ...) return; va_start(args, format); - log_vprintf(ctx, JAYLINK_LOG_LEVEL_DEBUG, format, args); + ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_DEBUG, format, args, + ctx->log_callback_data); va_end(args); } -- 2.11.4.GIT