aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-10-18 08:56:42 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-10-18 08:56:42 +0200
commit80a474883260aa45dca6c4674ea95c110fac6a9c (patch)
tree667a2344de70f552764b32e1a4d7983d9e02d503
parentd80134afe11289bf68630d59e0db5edcbb898c20 (diff)
downloadcpp-utils-80a474883260aa45dca6c4674ea95c110fac6a9c.tar.gz
cpp-utils-80a474883260aa45dca6c4674ea95c110fac6a9c.zip
log: add check for fmt args against fmt string if -Wformat is enabled
-rw-r--r--log.h29
1 files changed, 17 insertions, 12 deletions
diff --git a/log.h b/log.h
index c0880fb..795bac1 100644
--- a/log.h
+++ b/log.h
@@ -16,18 +16,23 @@
// -- LOGGER MACROS DETAILS ----------------------------------------------------
-#define LOG(log_, lvl, fmt, ...) \
- do { \
- struct fmt_str_must_be_str_literal { \
- constexpr fmt_str_must_be_str_literal(const char* ptr) : ptr{ptr} {} \
- const char* ptr; \
- }; \
- /* Check if FMT is a string literal, construction fails otherwise. */ \
- constexpr fmt_str_must_be_str_literal _{fmt}; \
- (void)_; \
- \
- if (logging::log_level::lvl <= (log_)->get_level()) \
- (log_)->template log<logging::log_level::lvl>(fmt, ##__VA_ARGS__); \
+#define LOG(log_, lvl, fmt, ...) \
+ do { \
+ struct fmt_str_must_be_str_literal { \
+ constexpr fmt_str_must_be_str_literal(const char* ptr) : ptr{ptr} {} \
+ const char* ptr; \
+ }; \
+ /* Check if FMT is a string literal, construction fails otherwise. */ \
+ constexpr fmt_str_must_be_str_literal _{fmt}; \
+ (void)_; \
+ \
+ int lint_fmt_string(const char*, ...) \
+ __attribute__((format(printf, 1, 2))); \
+ /* Check fmt args against fmt string and warn if -Wformat is enabled. */ \
+ (void)sizeof(lint_fmt_string(fmt, ##__VA_ARGS__)); \
+ \
+ if (logging::log_level::lvl <= (log_)->get_level()) \
+ (log_)->template log<logging::log_level::lvl>(fmt, ##__VA_ARGS__); \
} while (0)
namespace logging {