Use the GCC printf format attribute.

This enables the compiler to check the arguments against a printf style
format string in all calls to the logging functions.
This commit is contained in:
Jef Driesen 2015-11-04 19:42:17 +01:00
parent 0a20eae342
commit fb5f7ad971
2 changed files with 14 additions and 2 deletions

View File

@ -26,9 +26,15 @@
extern "C" {
#endif /* __cplusplus */
#if defined(__GNUC__)
#define ATTR_FORMAT_PRINTF(a,b) __attribute__((format(printf, a, b)))
#else
#define ATTR_FORMAT_PRINTF(a,b)
#endif
#define WARNING(expr) message ("%s:%d: %s\n", __FILE__, __LINE__, expr)
int message (const char* fmt, ...);
int message (const char* fmt, ...) ATTR_FORMAT_PRINTF(1, 2);
void message_set_logfile (const char* filename);

View File

@ -40,6 +40,12 @@ extern "C" {
#define FUNCTION __FUNCTION__
#endif
#if defined(__GNUC__)
#define ATTR_FORMAT_PRINTF(a,b) __attribute__((format(printf, a, b)))
#else
#define ATTR_FORMAT_PRINTF(a,b)
#endif
#ifdef ENABLE_LOGGING
#define HEXDUMP(context, loglevel, prefix, data, size) dc_context_hexdump (context, loglevel, __FILE__, __LINE__, FUNCTION, prefix, data, size)
#define SYSERROR(context, errcode) dc_context_syserror (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, FUNCTION, errcode)
@ -57,7 +63,7 @@ extern "C" {
#endif
dc_status_t
dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...);
dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...) ATTR_FORMAT_PRINTF(6, 7);
dc_status_t
dc_context_syserror (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, int errcode);