From c796d32ab710d35b482684fb865946a33a5b7e80 Mon Sep 17 00:00:00 2001 From: Zofia Abramowska Date: Tue, 16 Aug 2016 17:29:26 +0200 Subject: [PATCH] Fix string formatting in logs 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 | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/common/log/alog.h b/src/common/log/alog.h index 8acfa1f..ff7e545 100644 --- a/src/common/log/alog.h +++ b/src/common/log/alog.h @@ -27,14 +27,29 @@ #include #include +#include + extern int __alog_level; +namespace { +// One of these templates might not get instantiated, so they have to be marked as (possibly) unused + template + void UNUSED __ALOG_FUN(int level, const std::stringstream &format, + Args&&... args) { + sd_journal_print(level, format.str().c_str(), std::forward(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) -- 2.7.4