From 9723c352c361a48a500a2cfb4283890863a2296d Mon Sep 17 00:00:00 2001 From: Szymon Jastrzebski Date: Sun, 19 Aug 2018 08:18:52 +0200 Subject: [PATCH] [Common] Added checking types of arguments passed to the logging macros + The type check is made only in debug build. + Added WEBAPI_NOOP macro, used in the release builds. [Verification] The code builds for debug&release builds. Change-Id: Ie192f02b40a204dc97e713c0b2e280cd612efc77 Signed-off-by: Szymon Jastrzebski --- src/common/logger.h | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/common/logger.h b/src/common/logger.h index b59a3183..228c871b 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -7,12 +7,31 @@ #include +#ifdef TIZEN_DEBUG_ENABLE // Using static inline function with no operation inside to cause compiler types check. -// Using empty do {} while in WEBAPI_NOP macro will cause that when TIZEN_DEBUG_ENABLE flag -// is turned off, then no types checking will be performed. This could cause problems when -// developing code with this flag off and then enabling it. -static inline int _noop_print(const char *fmt __attribute__((unused)), ...) { return 0; } -#define WEBAPI_NOP(...) ({ do { _noop_print(__VA_ARGS__); } while (0); }) +// Using empty do {} while in WEBAPI_CHECK_PRINTF_FORMAT_ARGS macro will cause that when +// TIZEN_DEBUG_ENABLE flag is turned off, then no types checking will be performed. This could cause +// problems when developing code with this flag off and then enabling it. +static inline int _printf_format_checker(const char* fmt, ...) + __attribute__((format(printf, 1, 2))); + +int _printf_format_checker(const char* fmt, ...) { + return 0; +} + +#define WEBAPI_CHECK_PRINTF_FORMAT_ARGS(...) \ + ({ \ + do { \ + _printf_format_checker(__VA_ARGS__); \ + } while (0); \ + }) + +#else // TIZEN_DEBUG_ENABLE + +#define WEBAPI_NOOP() \ + do { \ + } while (0); +#endif // TIZEN_DEBUG_ENABLE // Tizen 3.0 uses different debug flag (DLOG_DEBUG_ENABLE) which is always // enabled, following code allows to disable logs with DLOG_DEBUG priority if @@ -24,6 +43,7 @@ static inline int _noop_print(const char *fmt __attribute__((unused)), ...) { re #define LOG_(id, prio, tag, fmt, arg...) \ ({ \ do { \ + WEBAPI_CHECK_PRINTF_FORMAT_ARGS(fmt, ##arg); \ __dlog_print(id, prio, tag, "%s: %s(%d) > " fmt, __MODULE__, __func__, __LINE__, ##arg); \ } while (0); \ }) @@ -43,12 +63,13 @@ static inline int _noop_print(const char *fmt __attribute__((unused)), ...) { re #define SECURE_LOG_(id, prio, tag, fmt, arg...) \ ({ \ do { \ + WEBAPI_CHECK_PRINTF_FORMAT_ARGS(fmt, ##arg); \ __dlog_print(id, prio, tag, "%s: %s(%d) > [SECURE_LOG] " fmt, __MODULE__, __func__, \ __LINE__, ##arg); \ } while (0); \ }) #else // TIZEN_DEBUG_ENABLE -#define SECURE_LOG_(id, prio, tag, fmt, arg...) WEBAPI_NOP(fmt, ##arg) +#define SECURE_LOG_(id, prio, tag, fmt, arg...) WEBAPI_NOOP() #endif // TIZEN_DEBUG_ENABLE #include @@ -254,13 +275,14 @@ class ScopeLogger { #ifdef TIZEN_DEBUG_ENABLE #define ScopeLogger(EX, args...) \ + WEBAPI_CHECK_PRINTF_FORMAT_ARGS("noop" EX, ##args); \ __dlog_print(LOG_ID_MAIN, DLOG_DEBUG, LOGGER_TAG, \ "logger.h: ScopeLogger > %s: %s(%d) > Enter " EX, __MODULE__, __func__, __LINE__, \ ##args); \ const common::ScopeLogger __sl__{__MODULE__, __func__}; #else -#define ScopeLogger(EX, args...) +#define ScopeLogger(EX, args...) WEBAPI_NOOP() #endif #endif // COMMON_LOGGER_H_ -- 2.34.1