Skip to content

Commit 2db926f

Browse files
committed
Merge bitcoin/bitcoin#30889: log: Use ConstevalFormatString
facbcd4 log: Use ConstevalFormatString (MarcoFalke) fae9b60 test: Use LogPrintStr to test m_log_sourcelocations (MarcoFalke) fa39b1c doc: move-only logging warning (MarcoFalke) Pull request description: This changes all logging (including the wallet logging) to produce a `ConstevalFormatString` at compile time, so that the format string can be validated at compile-time. I tested with `clang` and found that the compiler will use less than 1% more of time and memory. When an error is found, the compile-time error depends on the compiler, but it may look similar to: ``` src/util/string.h: In function ‘int main(int, char**)’: src/bitcoind.cpp:265:5: in ‘constexpr’ expansion of ‘util::ConstevalFormatString<1>(((const char*)"Hi %s %s"))’ src/util/string.h:38:98: in ‘constexpr’ expansion of ‘util::ConstevalFormatString<1>::Detail_CheckNumFormatSpecifiers(std::basic_string_view<char>(((const char*)((util::ConstevalFormatString<1>*)this)->util::ConstevalFormatString<1>::fmt)))’ src/util/string.h:78:34: error: expression ‘<throw-expression>’ is not a constant expression 78 | if (num_params != count) throw "Format specifier count must match the argument count!"; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` This refactor does not change behavior of the compiled executables. ACKs for top commit: hodlinator: re-ACK facbcd4 l0rinc: ACK facbcd4 ryanofsky: Code review ACK facbcd4 pablomartin4btc: re-ACK facbcd4 stickies-v: Approach ACK and code LGTM facbcd4 modulo a `tinyformat::format_error` concern. Tree-SHA512: 852f74d360897020f0d0f6e5064edc5e7f7dacc2bec1d5feff22c634a2fcd2eb535aa75be0b7191d9053728be6108484c737154b02d68ad3186a2e5544ba0db8
2 parents 9ba5688 + facbcd4 commit 2db926f

File tree

7 files changed

+19
-34
lines changed

7 files changed

+19
-34
lines changed

src/logging.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -103,7 +103,6 @@ void BCLog::Logger::DisconnectTestLogger()
103103
m_cur_buffer_memusage = 0;
104104
m_buffer_lines_discarded = 0;
105105
m_msgs_before_open.clear();
106-
107106
}
108107

109108
void BCLog::Logger::DisableLogging()

src/logging.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -245,28 +245,27 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve
245245
/** Return true if str parses as a log category and set the flag */
246246
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);
247247

248-
// Be conservative when using functions that
249-
// unconditionally log to debug.log! It should not be the case that an inbound
250-
// peer can fill up a user's disk with debug.log entries.
251-
252248
template <typename... Args>
253-
static inline void LogPrintf_(std::string_view logging_function, std::string_view source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char* fmt, const Args&... args)
249+
inline void LogPrintFormatInternal(std::string_view logging_function, std::string_view source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
254250
{
255251
if (LogInstance().Enabled()) {
256252
std::string log_msg;
257253
try {
258254
log_msg = tfm::format(fmt, args...);
259255
} catch (tinyformat::format_error& fmterr) {
260256
/* Original format string will have newline so don't add one here */
261-
log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
257+
log_msg = "Error \"" + std::string{fmterr.what()} + "\" while formatting log message: " + fmt.fmt;
262258
}
263259
LogInstance().LogPrintStr(log_msg, logging_function, source_file, source_line, flag, level);
264260
}
265261
}
266262

267-
#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
263+
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
268264

269265
// Log unconditionally.
266+
// Be conservative when using functions that unconditionally log to debug.log!
267+
// It should not be the case that an inbound peer can fill up a user's storage
268+
// with debug.log entries.
270269
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
271270
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, __VA_ARGS__)
272271
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)

src/test/logging_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ BOOST_AUTO_TEST_CASE(logging_timer)
8383
BOOST_CHECK_EQUAL(micro_timer.LogMsg("msg").substr(0, result_prefix.size()), result_prefix);
8484
}
8585

86-
BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
86+
BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
8787
{
8888
LogInstance().m_log_sourcelocations = true;
89-
LogPrintf_("fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug, "foo1: %s\n", "bar1");
90-
LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::Info, "foo2: %s\n", "bar2");
91-
LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::ALL, BCLog::Level::Debug, "foo3: %s\n", "bar3");
92-
LogPrintf_("fn4", "src4", 4, BCLog::LogFlags::ALL, BCLog::Level::Info, "foo4: %s\n", "bar4");
93-
LogPrintf_("fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo5: %s\n", "bar5");
94-
LogPrintf_("fn6", "src6", 6, BCLog::LogFlags::NONE, BCLog::Level::Info, "foo6: %s\n", "bar6");
89+
LogInstance().LogPrintStr("foo1: bar1\n", "fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug);
90+
LogInstance().LogPrintStr("foo2: bar2\n", "fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::Info);
91+
LogInstance().LogPrintStr("foo3: bar3\n", "fn3", "src3", 3, BCLog::LogFlags::ALL, BCLog::Level::Debug);
92+
LogInstance().LogPrintStr("foo4: bar4\n", "fn4", "src4", 4, BCLog::LogFlags::ALL, BCLog::Level::Info);
93+
LogInstance().LogPrintStr("foo5: bar5\n", "fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Debug);
94+
LogInstance().LogPrintStr("foo6: bar6\n", "fn6", "src6", 6, BCLog::LogFlags::NONE, BCLog::Level::Info);
9595
std::ifstream file{tmp_log_path};
9696
std::vector<std::string> log_lines;
9797
for (std::string log; std::getline(file, log);) {

src/wallet/scriptpubkeyman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ class ScriptPubKeyMan
254254

255255
/** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
256256
template <typename... Params>
257-
void WalletLogPrintf(const char* fmt, Params... parameters) const
257+
void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
258258
{
259-
LogPrintf(("%s " + std::string{fmt}).c_str(), m_storage.GetDisplayName(), parameters...);
259+
LogInfo("%s %s", m_storage.GetDisplayName(), tfm::format(wallet_fmt, params...));
260260
};
261261

262262
/** Watch-only address added */

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
927927

928928
/** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
929929
template <typename... Params>
930-
void WalletLogPrintf(const char* fmt, Params... parameters) const
930+
void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
931931
{
932-
LogPrintf(("%s " + std::string{fmt}).c_str(), GetDisplayName(), parameters...);
932+
LogInfo("%s %s", GetDisplayName(), tfm::format(wallet_fmt, params...));
933933
};
934934

935935
/** Upgrade the wallet */

test/lint/lint-format-strings.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,7 @@
1717

1818
FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS = [
1919
'tfm::format,1', # Assuming tfm::::format(std::ostream&, ...
20-
'LogError,0',
21-
'LogWarning,0',
22-
'LogInfo,0',
23-
'LogDebug,1',
24-
'LogTrace,1',
25-
'LogPrintf,0',
26-
'LogPrintLevel,2',
2720
'strprintf,0',
28-
'WalletLogPrintf,0',
2921
]
3022
RUN_LINT_FILE = 'test/lint/run-lint-format-strings.py'
3123

test/lint/run-lint-format-strings.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
FALSE_POSITIVES = [
1616
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
1717
("src/test/translation_tests.cpp", "strprintf(format, arg)"),
18-
("src/validationinterface.cpp", "LogDebug(BCLog::VALIDATION, fmt \"\\n\", __VA_ARGS__)"),
19-
("src/wallet/wallet.h", "WalletLogPrintf(const char* fmt, Params... parameters)"),
20-
("src/wallet/wallet.h", "LogPrintf((\"%s \" + std::string{fmt}).c_str(), GetDisplayName(), parameters...)"),
21-
("src/wallet/scriptpubkeyman.h", "WalletLogPrintf(const char* fmt, Params... parameters)"),
22-
("src/wallet/scriptpubkeyman.h", "LogPrintf((\"%s \" + std::string{fmt}).c_str(), m_storage.GetDisplayName(), parameters...)"),
2318
]
2419

2520

0 commit comments

Comments
 (0)