[Common] Added checking types of arguments passed to the logging macros 75/187075/3
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Sun, 19 Aug 2018 06:18:52 +0000 (08:18 +0200)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Mon, 20 Aug 2018 08:29:30 +0000 (10:29 +0200)
+ 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 <s.jastrzebsk@partner.samsung.com>
src/common/logger.h

index b59a31830ebcd4259ae67b4cf673d44e7ba18b5a..228c871b5a72c76818a2cfd9ed0ff50078bae1cf 100644 (file)
@@ -7,12 +7,31 @@
 
 #include <dlog.h>
 
+#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 <cstring>
@@ -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_