Fix string formatting in logs 03/84103/3
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 16 Aug 2016 15:29:26 +0000 (17:29 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Tue, 16 Aug 2016 16:14:07 +0000 (18:14 +0200)
Logs using C++ formatting do not conform to -Wformat-string.
Add templates, which properly handle both C++ and C formatting.

Change-Id: I0e27ece67598a93adb2c6d4e44c04b5afe091457

src/common/log/alog.h

index 8acfa1f..ff7e545 100644 (file)
 #include <sstream>
 #include <systemd/sd-journal.h>
 
+#include <attributes/attributes.h>
+
 extern int __alog_level;
 
+namespace {
+// One of these templates might not get instantiated, so they have to be marked as (possibly) unused
+    template <typename ...Args>
+    void UNUSED __ALOG_FUN(int level, const std::stringstream &format,
+                                            Args&&... args) {
+        sd_journal_print(level, format.str().c_str(), std::forward<Args>(args)...);
+    }
+    template <>
+    void UNUSED __ALOG_FUN(int level, const std::stringstream &format) {
+        sd_journal_print(level, "%s", format.str().c_str());
+    }
+}
+
 #define __ALOG(LEVEL, FORMAT, ...) \
     do { \
         if (LEVEL <= __alog_level) { \
             std::stringstream __LOG_MACRO_format; \
             __LOG_MACRO_format << FORMAT; \
-            sd_journal_print(LEVEL, __LOG_MACRO_format.str().c_str(), ##__VA_ARGS__); \
+            __ALOG_FUN(LEVEL, __LOG_MACRO_format, ##__VA_ARGS__); \
         } \
     } while (0)