From: Daniel Elstner Date: Sat, 12 Sep 2015 20:41:22 +0000 (+0200) Subject: log: Use generalized sr_log() to implement logging helpers X-Git-Tag: libsigrok-0.4.0~310 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=be92d5b4ee4a7ebc5096e9e4718be0ed34ce690c;p=libsigrok.git log: Use generalized sr_log() to implement logging helpers Get rid of the specicialized sr_err(), sr_warn(), etc. functions. Instead, define the logging helper macros in terms of sr_log(), and remove the sr_log() helper macro so that no function is hidden by a macro anymore. Decorate sr_log() with G_GNUC_PRINTF to detect varargs errors. This unearthed a gazillion warnings all over the place which will have to be fixed. Also convert the helper macros to ISO C99 __VA_ARGS__ style instead of relying on a GNU C extension. Paste the log prefix directly into the format string to make this work. --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index fc436014..b4280325 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -595,22 +595,14 @@ struct drv_context { /*--- log.c -----------------------------------------------------------------*/ -SR_PRIV int sr_log(int loglevel, const char *format, ...); -SR_PRIV int sr_spew(const char *format, ...); -SR_PRIV int sr_dbg(const char *format, ...); -SR_PRIV int sr_info(const char *format, ...); -SR_PRIV int sr_warn(const char *format, ...); -SR_PRIV int sr_err(const char *format, ...); +SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3); /* Message logging helpers with subsystem-specific prefix string. */ -#ifndef NO_LOG_WRAPPERS -#define sr_log(l, s, args...) sr_log(l, "%s: " s, LOG_PREFIX, ## args) -#define sr_spew(s, args...) sr_spew("%s: " s, LOG_PREFIX, ## args) -#define sr_dbg(s, args...) sr_dbg("%s: " s, LOG_PREFIX, ## args) -#define sr_info(s, args...) sr_info("%s: " s, LOG_PREFIX, ## args) -#define sr_warn(s, args...) sr_warn("%s: " s, LOG_PREFIX, ## args) -#define sr_err(s, args...) sr_err("%s: " s, LOG_PREFIX, ## args) -#endif +#define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__) +#define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__) +#define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__) +#define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__) +#define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__) /*--- device.c --------------------------------------------------------------*/ diff --git a/src/log.c b/src/log.c index 36aa5c00..750ada05 100644 --- a/src/log.c +++ b/src/log.c @@ -21,11 +21,10 @@ #include #include #include -/** @cond PRIVATE */ -#define NO_LOG_WRAPPERS -/** @endcond */ #include "libsigrok-internal.h" +#define LOG_PREFIX "log" + /** * @file * @@ -118,28 +117,31 @@ SR_API int sr_log_loglevel_get(void) * messages from now on. Must not be NULL. The maximum * length of the string is 30 characters (this does not * include the trailing NUL-byte). Longer strings are - * silently truncated. + * truncated. * In order to not use a logdomain, pass an empty string. * The function makes its own copy of the input string, i.e. * the caller does not need to keep it around. * - * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain. + * @retval SR_OK upon success. + * @retval SR_ERR_ARG @a logdomain was NULL. + * @retval SR_ERR @a logdomain was truncated. * * @since 0.1.0 */ SR_API int sr_log_logdomain_set(const char *logdomain) { + size_t len; + if (!logdomain) { - sr_err("log: %s: logdomain was NULL", __func__); + sr_err("%s: logdomain was NULL", __func__); return SR_ERR_ARG; } - /* TODO: Error handling. */ - snprintf(sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain); + len = g_strlcpy(sr_log_domain, logdomain, sizeof sr_log_domain); sr_dbg("Log domain set to '%s'.", sr_log_domain); - return SR_OK; + return (len < sizeof sr_log_domain) ? SR_OK : SR_ERR; } /** @@ -174,7 +176,7 @@ SR_API char *sr_log_logdomain_get(void) SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data) { if (!cb) { - sr_err("log: %s: cb was NULL", __func__); + sr_err("%s: cb was NULL", __func__); return SR_ERR_ARG; } @@ -253,69 +255,4 @@ SR_PRIV int sr_log(int loglevel, const char *format, ...) return ret; } -/** @private */ -SR_PRIV int sr_spew(const char *format, ...) -{ - int ret; - va_list args; - - va_start(args, format); - ret = sr_log_cb(sr_log_cb_data, SR_LOG_SPEW, format, args); - va_end(args); - - return ret; -} - -/** @private */ -SR_PRIV int sr_dbg(const char *format, ...) -{ - int ret; - va_list args; - - va_start(args, format); - ret = sr_log_cb(sr_log_cb_data, SR_LOG_DBG, format, args); - va_end(args); - - return ret; -} - -/** @private */ -SR_PRIV int sr_info(const char *format, ...) -{ - int ret; - va_list args; - - va_start(args, format); - ret = sr_log_cb(sr_log_cb_data, SR_LOG_INFO, format, args); - va_end(args); - - return ret; -} - -/** @private */ -SR_PRIV int sr_warn(const char *format, ...) -{ - int ret; - va_list args; - - va_start(args, format); - ret = sr_log_cb(sr_log_cb_data, SR_LOG_WARN, format, args); - va_end(args); - - return ret; -} - -/** @private */ -SR_PRIV int sr_err(const char *format, ...) -{ - int ret; - va_list args; - - va_start(args, format); - ret = sr_log_cb(sr_log_cb_data, SR_LOG_ERR, format, args); - va_end(args); - - return ret; -} - /** @} */