From 917fc7ecf1eb4f741d3ca4c7635bf8eb7db5a56d Mon Sep 17 00:00:00 2001 From: roynatech2544 Date: Tue, 1 Nov 2022 13:01:49 +0900 Subject: [PATCH] universal7885: liblogformat: Add helpers to avoid -Wformat-security warns/errors - Also make warning if LOG_TAG is not defined --- .../apps/aidl-support/include/LogFormat.h | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/universal7885-common/apps/aidl-support/include/LogFormat.h b/universal7885-common/apps/aidl-support/include/LogFormat.h index 18c4627..e772bb3 100644 --- a/universal7885-common/apps/aidl-support/include/LogFormat.h +++ b/universal7885-common/apps/aidl-support/include/LogFormat.h @@ -1,8 +1,19 @@ #include #include +#ifdef LOG_TAG + +#include + #define make_str(a, ...) _make_str(__FILE__, __LINE__, a, ##__VA_ARGS__).c_str() +// Helpers to avoid -Wformat-security +#define LOG_E(fmt, ...) ALOGE("%s", make_str(fmt, ##__VA_ARGS__)) +#define LOG_W(fmt, ...) ALOGW("%s", make_str(fmt, ##__VA_ARGS__)) +#define LOG_I(fmt, ...) ALOGI("%s", make_str(fmt, ##__VA_ARGS__)) +#define LOG_D(fmt, ...) ALOGD("%s", make_str(fmt, ##__VA_ARGS__)) +#define LOG_V(fmt, ...) ALOGV("%s", make_str(fmt, ##__VA_ARGS__)) + template std::string _make_str(const std::string& filename, int line, const std::string& fmt, Args... args) { #pragma clang diagnostic push @@ -14,4 +25,16 @@ std::string _make_str(const std::string& filename, int line, const std::string& std::stringstream ss; ss << "[" << filename << ":" << line << "] " << std::string(buf.begin(), buf.end()); return ss.str(); -} +} + +#else + +#warning LOG_TAG is not defined, disabling logging. + +#define LOG_E(...) do {} while(0) +#define LOG_W(...) do {} while(0) +#define LOG_I(...) do {} while(0) +#define LOG_D(...) do {} while(0) +#define LOG_V(...) do {} while(0) + +#endif