diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -131,54 +131,31 @@ // Be conservative when using LogPrintf/error or other things which // unconditionally log to debug.log! It should not be the case that an inbound // peer can fill up a user's disk with debug.log entries. - -static inline void MarkUsed() {} -template -static inline void MarkUsed(const T &t, const Args &... args) { - (void)t; - MarkUsed(args...); -} template -std::string FormatStringFromLogArgs(const char *fmt, const Args &... args) { - return fmt; +static inline void LogPrintf(const char *fmt, const Args &... args) { + if (LogInstance().Enabled()) { + std::string log_msg; + try { + log_msg = tfm::format(fmt, args...); + } catch (tinyformat::format_error &fmterr) { + /** + * Original format string will have newline so don't add one here + */ + log_msg = "Error \"" + std::string(fmterr.what()) + + "\" while formatting log message: " + fmt; + } + LogInstance().LogPrintStr(log_msg); + } } -#ifdef USE_COVERAGE -#define LogPrintf(...) \ - do { \ - MarkUsed(__VA_ARGS__); \ - } while (0) -#define LogPrint(category, ...) \ - do { \ - MarkUsed(__VA_ARGS__); \ - } while (0) -#else -#define LogPrintf(...) \ - do { \ - if (LogInstance().Enabled()) { \ - /* Unlikely name to avoid shadowing variables */ \ - std::string _log_msg_; \ - try { \ - _log_msg_ = tfm::format(__VA_ARGS__); \ - } catch (tinyformat::format_error & fmterr) { \ - /** \ - * Original format string will have newline so don't add one \ - * here \ - */ \ - _log_msg_ = "Error \"" + std::string(fmterr.what()) + \ - "\" while formatting log message: " + \ - FormatStringFromLogArgs(__VA_ARGS__); \ - } \ - LogInstance().LogPrintStr(_log_msg_); \ - } \ - } while (0) +// Use a macro instead of a function for conditional logging to prevent +// evaluating arguments when logging for the category is not enabled. #define LogPrint(category, ...) \ do { \ if (LogAcceptCategory((category))) { \ LogPrintf(__VA_ARGS__); \ } \ } while (0) -#endif /** * These are aliases used to explicitly state that the message should not end diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py --- a/test/lint/lint-format-strings.py +++ b/test/lint/lint-format-strings.py @@ -24,6 +24,7 @@ ("src/tinyformat.h", "printf(const char *fmt, TINYFORMAT_VARARGS(n))"), ("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"), + ("src/logging.h", "LogPrintf(const char *fmt, const Args &... args)"), ] FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS = [