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.
This commit is contained in:
Dan Williams 2011-03-19 12:42:29 -05:00
parent 3997cc77bf
commit e7be98260e
2 changed files with 23 additions and 19 deletions

View file

@ -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;

View file

@ -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);