From de7e66c3692073eb967f01cc8281441709701e2d Mon Sep 17 00:00:00 2001 From: Shawn Willden Date: Fri, 22 May 2015 19:58:06 -0600 Subject: Change handling of debug log statments in non-debug builds. The previous way had a problem when used in statments like: if (foo) LOG_D(...); When built without debugging, this became: if (foo) ; Which is sort of okay, but the compiler complains. The new way also has the advantage that the compiler always sees and checks the log arguments. Given that it ends up compiling something like: do { if (0) Logger::Debug(...); } while (0); It should optimize the entire block out, and should even discard the literal string used for the format. So it's better all around. Change-Id: I895141077f627a2d08dcb0d7d2d0799067a2c957 --- include/keymaster/logger.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/include/keymaster/logger.h b/include/keymaster/logger.h index da7d557..7b9e6d5 100644 --- a/include/keymaster/logger.h +++ b/include/keymaster/logger.h @@ -55,24 +55,26 @@ class Logger { static Logger* instance_; }; -#ifdef DEBUG - #define STR(x) #x #define STRINGIFY(x) STR(x) #define FILE_LINE __FILE__ ", Line " STRINGIFY(__LINE__) ": " -#define LOG_D(fmt, ...) Logger::Debug(FILE_LINE fmt, __VA_ARGS__) -#define LOG_I(fmt, ...) Logger::Info(FILE_LINE fmt, __VA_ARGS__) - -#else // not DEBUG - -// For production builds, don't bloat the executable with FILE_LINE strings or DEBUG or INFO log -// messages. -#define FILE_LINE -#define LOG_D(fmt, ...) -#define LOG_I(fmt, ...) - -#endif // not DEBUG +#ifdef DEBUG +#define DEBUG_LOGS 1 +#else +#define DEBUG_LOGS 0 +#endif + +#define LOG_D(fmt, ...) \ + do { \ + if (DEBUG_LOGS) \ + Logger::Debug(FILE_LINE fmt, __VA_ARGS__); \ + } while (0) +#define LOG_I(fmt, ...) \ + do { \ + if (DEBUG_LOGS) \ + Logger::Info(FILE_LINE fmt, __VA_ARGS__); \ + } while (0) #define LOG_W(fmt, ...) Logger::Warning(FILE_LINE fmt, __VA_ARGS__) #define LOG_E(fmt, ...) Logger::Error(FILE_LINE fmt, __VA_ARGS__) -- cgit v1.2.3