From e7be98260e4b29c6b8fdf92760a3ac4e48b78d13 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Sat, 19 Mar 2011 12:42:29 -0500 Subject: [PATCH] logging: clean up logging macros and helper There were two specific problems with the logging macros: 1) the existing varargs usage didn't allow for format string checking, which is bad, since it could make logging segfault if the arguments don't match the format string 2) it didn't allow logging usage without wrapping {}, ie this didn't work: if (foo) nm_log_dbg (...) blah blah Fix all that by using the varargs stuff correctly. --- src/logging/nm-logging.c | 13 +++++++------ src/logging/nm-logging.h | 29 ++++++++++++++++------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/logging/nm-logging.c b/src/logging/nm-logging.c index fa113ffabc..1e289d3719 100644 --- a/src/logging/nm-logging.c +++ b/src/logging/nm-logging.c @@ -218,12 +218,13 @@ nm_logging_level_enabled (guint32 level) return !!(log_level & level); } -void _nm_log (const char *loc, - const char *func, - guint32 domain, - guint32 level, - const char *fmt, - ...) +void +_nm_log (const char *loc, + const char *func, + guint32 domain, + guint32 level, + const char *fmt, + ...) { va_list args; char *msg; diff --git a/src/logging/nm-logging.h b/src/logging/nm-logging.h index 3e8ef696ef..44e49a712e 100644 --- a/src/logging/nm-logging.h +++ b/src/logging/nm-logging.h @@ -70,24 +70,27 @@ GQuark nm_logging_error_quark (void); GType nm_logging_error_get_type (void); -#define nm_log_err(domain, fmt, args...) \ - { _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_ERR, fmt, ##args); } +#define nm_log_err(domain, ...) \ + _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_ERR, ## __VA_ARGS__ ) -#define nm_log_warn(domain, fmt, args...) \ - { _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_WARN, fmt, ##args); } +#define nm_log_warn(domain, ...) \ + _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_WARN, ## __VA_ARGS__ ) -#define nm_log_info(domain, fmt, args...) \ - { _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_INFO, fmt, ##args); } +#define nm_log_info(domain, ...) \ + _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_INFO, ## __VA_ARGS__ ) -#define nm_log_dbg(domain, fmt, args...) \ - { _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_DEBUG, fmt, ##args); } +#define nm_log_dbg(domain, ...) \ + _nm_log (G_STRLOC, G_STRFUNC, domain, LOGL_DEBUG, ## __VA_ARGS__ ) -#define nm_log(domain, level, fmt, args...) \ - { _nm_log (G_STRLOC, G_STRFUNC, domain, level, fmt, ##args); } +#define nm_log(domain, level, ...) \ + _nm_log (G_STRLOC, G_STRFUNC, domain, level, ## __VA_ARGS__ ) -void _nm_log (const char *loc, const char *func, - guint32 domain, guint32 level, - const char *fmt, ...); +void _nm_log (const char *loc, + const char *func, + guint32 domain, + guint32 level, + const char *fmt, + ...) __attribute__((__format__ (__printf__, 5, 6))); const char *nm_logging_level_to_string (void); char *nm_logging_domains_to_string (void);