Skip to content

Commit facbcd4

Browse files
author
MarcoFalke
committed
log: Use ConstevalFormatString
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. Also, while touching the wallet logging, avoid a copy of the template Params by using const Params&.
1 parent fae9b60 commit facbcd4

File tree

5 files changed

+7
-20
lines changed

5 files changed

+7
-20
lines changed

src/logging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,21 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve
246246
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);
247247

248248
template <typename... Args>
249-
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)
250250
{
251251
if (LogInstance().Enabled()) {
252252
std::string log_msg;
253253
try {
254254
log_msg = tfm::format(fmt, args...);
255255
} catch (tinyformat::format_error& fmterr) {
256256
/* Original format string will have newline so don't add one here */
257-
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;
258258
}
259259
LogInstance().LogPrintStr(log_msg, logging_function, source_file, source_line, flag, level);
260260
}
261261
}
262262

263-
#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__)
264264

265265
// Log unconditionally.
266266
// Be conservative when using functions that unconditionally log to debug.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)