diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-10-18 08:56:42 +0200 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-10-18 08:56:42 +0200 |
commit | 80a474883260aa45dca6c4674ea95c110fac6a9c (patch) | |
tree | 667a2344de70f552764b32e1a4d7983d9e02d503 /log.h | |
parent | d80134afe11289bf68630d59e0db5edcbb898c20 (diff) | |
download | cpp-utils-80a474883260aa45dca6c4674ea95c110fac6a9c.tar.gz cpp-utils-80a474883260aa45dca6c4674ea95c110fac6a9c.zip |
log: add check for fmt args against fmt string if -Wformat is enabled
Diffstat (limited to 'log.h')
-rw-r--r-- | log.h | 29 |
1 files changed, 17 insertions, 12 deletions
@@ -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 { |