Skip to content

Commit 1bfcc06

Browse files
committed
Merge #14209: logging: Replace LogPrint macros with regular functions
fae3fbd 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 c8914b9) Fix this instead by replacing the macros with functions. Tree-SHA512: 101aa4f4a3ffcefc38faf70c9d3deb5fc63e0b11ca54a164d0463931c79eaf53ab0b0c6ae92a45355574e3b1d2c32233874a6b24293e7a09d188fc6698e212a5
2 parents 8f46454 + fae3fbd commit 1bfcc06

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

src/logging.h

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -125,42 +125,31 @@ std::vector<CLogCategoryActive> ListActiveLogCategories();
125125
/** Return true if str parses as a log category and set the flag */
126126
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
127127

128-
/** Get format string from VA_ARGS for error reporting */
129-
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
130-
131-
static inline void MarkUsed() {}
132-
template<typename T, typename... Args> static inline void MarkUsed(const T& t, const Args&... args)
133-
{
134-
(void)t;
135-
MarkUsed(args...);
136-
}
137-
138128
// Be conservative when using LogPrintf/error or other things which
139129
// unconditionally log to debug.log! It should not be the case that an inbound
140130
// peer can fill up a user's disk with debug.log entries.
141131

142-
#ifdef USE_COVERAGE
143-
#define LogPrintf(...) do { MarkUsed(__VA_ARGS__); } while(0)
144-
#define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0)
145-
#else
146-
#define LogPrintf(...) do { \
147-
if (g_logger->Enabled()) { \
148-
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
149-
try { \
150-
_log_msg_ = tfm::format(__VA_ARGS__); \
151-
} catch (tinyformat::format_error &fmterr) { \
152-
/* Original format string will have newline so don't add one here */ \
153-
_log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
154-
} \
155-
g_logger->LogPrintStr(_log_msg_); \
156-
} \
157-
} while(0)
158-
159-
#define LogPrint(category, ...) do { \
160-
if (LogAcceptCategory((category))) { \
161-
LogPrintf(__VA_ARGS__); \
162-
} \
163-
} while(0)
164-
#endif
132+
template <typename... Args>
133+
static inline void LogPrintf(const char* fmt, const Args&... args)
134+
{
135+
if (g_logger->Enabled()) {
136+
std::string log_msg;
137+
try {
138+
log_msg = tfm::format(fmt, args...);
139+
} catch (tinyformat::format_error& fmterr) {
140+
/* Original format string will have newline so don't add one here */
141+
log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
142+
}
143+
g_logger->LogPrintStr(log_msg);
144+
}
145+
}
146+
147+
template <typename... Args>
148+
static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args)
149+
{
150+
if (LogAcceptCategory((category))) {
151+
LogPrintf(args...);
152+
}
153+
}
165154

166155
#endif // BITCOIN_LOGGING_H

test/lint/lint-format-strings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
("src/util.cpp", "strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
2121
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
2222
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
23+
("src/logging.h", "LogPrintf(const char* fmt, const Args&... args)"),
2324
]
2425

2526

0 commit comments

Comments
 (0)