Merge #14209: logging: Replace LogPrint macros with regular functions

fae3fbd61a logging: Replace LogPrint macros with regular functions (MarcoFalke)

Pull request description:

  It is not possible to run the full test suite when configured with `--enable-lcov`, since logging is disabled currently so that "unnecessary branches are not analyzed". (See c8914b9dbb)

  Fix this instead by replacing the macros with functions.

Tree-SHA512: 101aa4f4a3ffcefc38faf70c9d3deb5fc63e0b11ca54a164d0463931c79eaf53ab0b0c6ae92a45355574e3b1d2c32233874a6b24293e7a09d188fc6698e212a5
This commit is contained in:
Wladimir J. van der Laan 2018-09-13 09:43:30 +02:00
commit 1bfcc0696c
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
2 changed files with 22 additions and 32 deletions

View file

@ -125,42 +125,31 @@ std::vector<CLogCategoryActive> ListActiveLogCategories();
/** Return true if str parses as a log category and set the flag */ /** Return true if str parses as a log category and set the flag */
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str); bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
/** Get format string from VA_ARGS for error reporting */
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
static inline void MarkUsed() {}
template<typename T, typename... Args> static inline void MarkUsed(const T& t, const Args&... args)
{
(void)t;
MarkUsed(args...);
}
// Be conservative when using LogPrintf/error or other things which // Be conservative when using LogPrintf/error or other things which
// unconditionally log to debug.log! It should not be the case that an inbound // 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. // peer can fill up a user's disk with debug.log entries.
#ifdef USE_COVERAGE template <typename... Args>
#define LogPrintf(...) do { MarkUsed(__VA_ARGS__); } while(0) static inline void LogPrintf(const char* fmt, const Args&... args)
#define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0) {
#else if (g_logger->Enabled()) {
#define LogPrintf(...) do { \ std::string log_msg;
if (g_logger->Enabled()) { \ try {
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \ log_msg = tfm::format(fmt, args...);
try { \ } catch (tinyformat::format_error& fmterr) {
_log_msg_ = tfm::format(__VA_ARGS__); \ /* Original format string will have newline so don't add one here */
} catch (tinyformat::format_error &fmterr) { \ log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
/* 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__); \ g_logger->LogPrintStr(log_msg);
} \ }
g_logger->LogPrintStr(_log_msg_); \ }
} \
} while(0)
#define LogPrint(category, ...) do { \ template <typename... Args>
if (LogAcceptCategory((category))) { \ static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args)
LogPrintf(__VA_ARGS__); \ {
} \ if (LogAcceptCategory((category))) {
} while(0) LogPrintf(args...);
#endif }
}
#endif // BITCOIN_LOGGING_H #endif // BITCOIN_LOGGING_H

View file

@ -20,6 +20,7 @@ FALSE_POSITIVES = [
("src/util.cpp", "strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION)"), ("src/util.cpp", "strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"), ("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"), ("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
("src/logging.h", "LogPrintf(const char* fmt, const Args&... args)"),
] ]