universal7885: liblogformat: Add helpers to avoid -Wformat-security warns/errors

- Also make warning if LOG_TAG is not defined
This commit is contained in:
roynatech2544 2022-11-01 13:01:49 +09:00
commit 917fc7ecf1

View file

@ -1,8 +1,19 @@
#include <sstream>
#include <vector>
#ifdef LOG_TAG
#include <log/log.h>
#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 <typename... Args>
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