From 80a474883260aa45dca6c4674ea95c110fac6a9c Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 18 Oct 2023 08:56:42 +0200 Subject: log: add check for fmt args against fmt string if -Wformat is enabled --- log.h | 29 +++++++++++++++++------------ 1 file 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(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(fmt, ##__VA_ARGS__); \ } while (0) namespace logging { -- cgit v1.2.3