Skip to content

Commit 4774e1e

Browse files
laanwjknst
authored andcommitted
Merge bitcoin#19809: log: Prefix log messages with function name and source code location if -logsourcelocations is set
b4511e2 log: Prefix log messages with function name if -logsourcelocations is set (practicalswift) Pull request description: Prefix log messages with function name if `-logfunctionnames` is set. Yes, exactly like `-logthreadnames` but for function names instead of thread names :) This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function. For me it beats the ordinary cycle of 1.) try to figure out a regexp matching the static part of the dynamic log message, 2.) `git grep -E 'Using .* MiB out of .* requested for signature cache'`, 3.) `mcedit filename.cpp` (`openemacs filename.cpp` works too!) and 4.) search for log message and scroll up to find the function name :) Without any logging parameters: ``` $ src/bitcoind -regtest 2020-08-25T03:29:04Z Using RdRand as an additional entropy source 2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements 2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements 2020-08-25T03:29:04Z Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000 2020-08-25T03:29:04Z block tree size = 1 2020-08-25T03:29:04Z nBestHeight = 0 2020-08-25T03:29:04Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast 2020-08-25T03:29:04Z 0 addresses found from DNS seeds ``` With `-logthreadnames` and `-logfunctionnames`: ``` $ src/bitcoind -regtest -logthreadnames -logfunctionnames 2020-08-25T03:29:04Z [init] [ReportHardwareRand] Using RdRand as an additional entropy source 2020-08-25T03:29:04Z [init] [InitSignatureCache] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements 2020-08-25T03:29:04Z [init] [InitScriptExecutionCache] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements 2020-08-25T03:29:04Z [init] [LoadChainTip] Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000 2020-08-25T03:29:04Z [init] [AppInitMain] block tree size = 1 2020-08-25T03:29:04Z [init] [AppInitMain] nBestHeight = 0 2020-08-25T03:29:04Z [loadblk] [LoadMempool] Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast 2020-08-25T03:29:04Z [dnsseed] [ThreadDNSAddressSeed] 0 addresses found from DNS seeds ``` ACKs for top commit: laanwj: Code review ACK b4511e2 MarcoFalke: review ACK b4511e2 🌃 Tree-SHA512: d100f5364630c323f31d275259864c597f7725e462d5f4bdedcc7033ea616d7fc0d16ef1b2af557e692f4deea73c6773ccfc681589e7bf6ba970b9ec169040c7
1 parent 43a94f0 commit 4774e1e

File tree

12 files changed

+104
-69
lines changed

12 files changed

+104
-69
lines changed

src/batchedlogger.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
#include <batchedlogger.h>
66

7-
CBatchedLogger::CBatchedLogger(BCLog::LogFlags _category, const std::string& _header) :
8-
accept(LogAcceptCategory(_category)), header(_header)
7+
CBatchedLogger::CBatchedLogger(BCLog::LogFlags _category, const std::string& logging_function, const std::string& source_file, int source_line) :
8+
accept(LogAcceptCategory(_category)),
9+
m_logging_function(logging_function),
10+
m_source_file(source_file),
11+
m_source_line(source_line)
912
{
1013
}
1114

@@ -19,6 +22,6 @@ void CBatchedLogger::Flush()
1922
if (!accept || msg.empty()) {
2023
return;
2124
}
22-
LogInstance().LogPrintStr(strprintf("%s:\n%s", header, msg));
25+
LogInstance().LogPrintStr(msg, m_logging_function, m_source_file, m_source_line);
2326
msg.clear();
2427
}

src/batchedlogger.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class CBatchedLogger
1111
{
1212
private:
1313
bool accept;
14-
std::string header;
14+
std::string m_logging_function;;
15+
std::string m_source_file;
16+
const int m_source_line;
1517
std::string msg;
1618
public:
17-
CBatchedLogger(BCLog::LogFlags _category, const std::string& _header);
19+
CBatchedLogger(BCLog::LogFlags _category, const std::string& logging_function, const std::string& m_source_file, int m_source_line);
1820
virtual ~CBatchedLogger();
1921

2022
template<typename... Args>

src/init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ void SetupServerArgs(NodeContext& node)
719719
argsman.AddArg("-debugexclude=<category>", strprintf("Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except the specified category. This option can be specified multiple times to exclude multiple categories."), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
720720
argsman.AddArg("-disablegovernance", strprintf("Disable governance validation (0-1, default: %u)", 0), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
721721
argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
722+
argsman.AddArg("-logsourcelocations", strprintf("Prepend debug output with name of the originating source location (source file, line number and function name) (default: %u)", DEFAULT_LOGSOURCELOCATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
722723
argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
723724
#ifdef HAVE_THREAD_LOCAL
724725
argsman.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
@@ -1084,6 +1085,8 @@ void InitLogging(const ArgsManager& args)
10841085
#ifdef HAVE_THREAD_LOCAL
10851086
LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
10861087
#endif
1088+
LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
1089+
10871090
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
10881091

10891092
std::string version_string = FormatFullVersion();

src/llmq/commitment.cpp

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,29 @@ CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const ui
2727
{
2828
}
2929

30-
template<typename... Types>
31-
void LogPrintfFinalCommitment(Types... out) {
32-
if (LogAcceptCategory(BCLog::LLMQ)) {
33-
LogInstance().LogPrintStr(strprintf("CFinalCommitment::%s -- %s", __func__, tinyformat::format(out...)));
34-
}
35-
}
36-
3730
bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool checkSigs) const
3831
{
3932
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
4033
if (!llmq_params_opt.has_value()) {
41-
LogPrintfFinalCommitment("q[%s] invalid llmqType=%d\n", quorumHash.ToString(), ToUnderlying(llmqType));
34+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid llmqType=%d\n", quorumHash.ToString(), ToUnderlying(llmqType));
4235
return false;
4336
}
4437
const auto& llmq_params = llmq_params_opt.value();
4538

4639
const uint16_t expected_nversion{CFinalCommitment::GetVersion(IsQuorumRotationEnabled(llmq_params, pQuorumBaseBlockIndex),
4740
DeploymentActiveAfter(pQuorumBaseBlockIndex, Params().GetConsensus(), Consensus::DEPLOYMENT_V19))};
4841
if (nVersion == 0 || nVersion != expected_nversion) {
49-
LogPrintfFinalCommitment("q[%s] invalid nVersion=%d expectednVersion\n", quorumHash.ToString(), nVersion, expected_nversion);
42+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid nVersion=%d expectednVersion\n", quorumHash.ToString(), nVersion, expected_nversion);
5043
return false;
5144
}
5245

5346
if (pQuorumBaseBlockIndex->GetBlockHash() != quorumHash) {
54-
LogPrintfFinalCommitment("q[%s] invalid quorumHash\n", quorumHash.ToString());
47+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumHash\n", quorumHash.ToString());
5548
return false;
5649
}
5750

5851
if ((pQuorumBaseBlockIndex->nHeight % llmq_params.dkgInterval) != quorumIndex) {
59-
LogPrintfFinalCommitment("q[%s] invalid quorumIndex=%d\n", quorumHash.ToString(), quorumIndex);
52+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumIndex=%d\n", quorumHash.ToString(), quorumIndex);
6053
return false;
6154
}
6255

@@ -65,27 +58,27 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, gsl::not_null<con
6558
}
6659

6760
if (CountValidMembers() < llmq_params.minSize) {
68-
LogPrintfFinalCommitment("q[%s] invalid validMembers count. validMembersCount=%d\n", quorumHash.ToString(), CountValidMembers());
61+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid validMembers count. validMembersCount=%d\n", quorumHash.ToString(), CountValidMembers());
6962
return false;
7063
}
7164
if (CountSigners() < llmq_params.minSize) {
72-
LogPrintfFinalCommitment("q[%s] invalid signers count. signersCount=%d\n", quorumHash.ToString(), CountSigners());
65+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid signers count. signersCount=%d\n", quorumHash.ToString(), CountSigners());
7366
return false;
7467
}
7568
if (!quorumPublicKey.IsValid()) {
76-
LogPrintfFinalCommitment("q[%s] invalid quorumPublicKey\n", quorumHash.ToString());
69+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumPublicKey\n", quorumHash.ToString());
7770
return false;
7871
}
7972
if (quorumVvecHash.IsNull()) {
80-
LogPrintfFinalCommitment("q[%s] invalid quorumVvecHash\n", quorumHash.ToString());
73+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorumVvecHash\n", quorumHash.ToString());
8174
return false;
8275
}
8376
if (!membersSig.IsValid()) {
84-
LogPrintfFinalCommitment("q[%s] invalid membersSig\n", quorumHash.ToString());
77+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid membersSig\n", quorumHash.ToString());
8578
return false;
8679
}
8780
if (!quorumSig.IsValid()) {
88-
LogPrintfFinalCommitment("q[%s] invalid vvecSig\n");
81+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid vvecSig\n");
8982
return false;
9083
}
9184
auto members = utils::GetAllQuorumMembers(llmqType, dmnman, pQuorumBaseBlockIndex);
@@ -96,16 +89,16 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, gsl::not_null<con
9689
ss << "v[" << i << "]=" << validMembers[i];
9790
ss2 << "s[" << i << "]=" << signers[i];
9891
}
99-
LogPrintfFinalCommitment("CFinalCommitment::%s mns[%d] validMembers[%s] signers[%s]\n", __func__, members.size(), ss.str(), ss2.str());
92+
LogPrint(BCLog::LLMQ, "CFinalCommitment::%s mns[%d] validMembers[%s] signers[%s]\n", __func__, members.size(), ss.str(), ss2.str());
10093
}
10194

10295
for (const auto i : irange::range(members.size(), size_t(llmq_params.size))) {
10396
if (validMembers[i]) {
104-
LogPrintfFinalCommitment("q[%s] invalid validMembers bitset. bit %d should not be set\n", quorumHash.ToString(), i);
97+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid validMembers bitset. bit %d should not be set\n", quorumHash.ToString(), i);
10598
return false;
10699
}
107100
if (signers[i]) {
108-
LogPrintfFinalCommitment("q[%s] invalid signers bitset. bit %d should not be set\n", quorumHash.ToString(), i);
101+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid signers bitset. bit %d should not be set\n", quorumHash.ToString(), i);
109102
return false;
110103
}
111104
}
@@ -118,7 +111,7 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, gsl::not_null<con
118111
for (const auto &mn: members) {
119112
ss3 << mn->proTxHash.ToString().substr(0, 4) << " | ";
120113
}
121-
LogPrintfFinalCommitment("CFinalCommitment::%s members[%s] quorumPublicKey[%s] commitmentHash[%s]\n",
114+
LogPrint(BCLog::LLMQ, "CFinalCommitment::%s members[%s] quorumPublicKey[%s] commitmentHash[%s]\n",
122115
__func__, ss3.str(), quorumPublicKey.ToString(), commitmentHash.ToString());
123116
}
124117
std::vector<CBLSPublicKey> memberPubKeys;
@@ -130,17 +123,17 @@ bool CFinalCommitment::Verify(CDeterministicMNManager& dmnman, gsl::not_null<con
130123
}
131124

132125
if (!membersSig.VerifySecureAggregated(memberPubKeys, commitmentHash)) {
133-
LogPrintfFinalCommitment("q[%s] invalid aggregated members signature\n", quorumHash.ToString());
126+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid aggregated members signature\n", quorumHash.ToString());
134127
return false;
135128
}
136129

137130
if (!quorumSig.VerifyInsecure(quorumPublicKey, commitmentHash)) {
138-
LogPrintfFinalCommitment("q[%s] invalid quorum signature\n", quorumHash.ToString());
131+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid quorum signature\n", quorumHash.ToString());
139132
return false;
140133
}
141134
}
142135

143-
LogPrintfFinalCommitment("q[%s] VALID\n", quorumHash.ToString());
136+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] VALID QUORUM\n", quorumHash.ToString());
144137

145138
return true;
146139
}
@@ -149,7 +142,7 @@ bool CFinalCommitment::VerifyNull() const
149142
{
150143
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
151144
if (!llmq_params_opt.has_value()) {
152-
LogPrintfFinalCommitment("q[%s]invalid llmqType=%d\n", quorumHash.ToString(), ToUnderlying(llmqType));
145+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s]invalid llmqType=%d\n", quorumHash.ToString(), ToUnderlying(llmqType));
153146
return false;
154147
}
155148

@@ -163,11 +156,11 @@ bool CFinalCommitment::VerifyNull() const
163156
bool CFinalCommitment::VerifySizes(const Consensus::LLMQParams& params) const
164157
{
165158
if (signers.size() != size_t(params.size)) {
166-
LogPrintfFinalCommitment("q[%s] invalid signers.size=%d\n", quorumHash.ToString(), signers.size());
159+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid signers.size=%d\n", quorumHash.ToString(), signers.size());
167160
return false;
168161
}
169162
if (validMembers.size() != size_t(params.size)) {
170-
LogPrintfFinalCommitment("q[%s] invalid signers.size=%d\n", quorumHash.ToString(), signers.size());
163+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- q[%s] invalid signers.size=%d\n", quorumHash.ToString(), signers.size());
171164
return false;
172165
}
173166
return true;
@@ -177,14 +170,14 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx
177170
{
178171
const auto opt_qcTx = GetTxPayload<CFinalCommitmentTxPayload>(tx);
179172
if (!opt_qcTx) {
180-
LogPrintfFinalCommitment("h[%d] GetTxPayload failed\n", pindexPrev->nHeight);
173+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetTxPayload LLMQCommitment failed\n", pindexPrev->nHeight);
181174
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-payload");
182175
}
183176
auto& qcTx = *opt_qcTx;
184177

185178
const auto& llmq_params_opt = Params().GetLLMQ(qcTx.commitment.llmqType);
186179
if (!llmq_params_opt.has_value()) {
187-
LogPrintfFinalCommitment("h[%d] GetLLMQ failed for llmqType[%d]\n", pindexPrev->nHeight, ToUnderlying(qcTx.commitment.llmqType));
180+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] GetLLMQ failed for llmqType[%d]\n", pindexPrev->nHeight, ToUnderlying(qcTx.commitment.llmqType));
188181
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-commitment-type");
189182
}
190183

@@ -193,17 +186,17 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx
193186
for (const auto i: irange::range(llmq_params_opt->size)) {
194187
ss << "v[" << i << "]=" << qcTx.commitment.validMembers[i];
195188
}
196-
LogPrintfFinalCommitment("%s llmqType[%d] validMembers[%s] signers[]\n", __func__,
189+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- %s llmqType[%d] validMembers[%s] signers[]\n", __func__,
197190
int(qcTx.commitment.llmqType), ss.str());
198191
}
199192

200193
if (qcTx.nVersion == 0 || qcTx.nVersion > CFinalCommitmentTxPayload::CURRENT_VERSION) {
201-
LogPrintfFinalCommitment("h[%d] invalid qcTx.nVersion[%d]\n", pindexPrev->nHeight, qcTx.nVersion);
194+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nVersion[%d]\n", pindexPrev->nHeight, qcTx.nVersion);
202195
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-version");
203196
}
204197

205198
if (qcTx.nHeight != uint32_t(pindexPrev->nHeight + 1)) {
206-
LogPrintfFinalCommitment("h[%d] invalid qcTx.nHeight[%d]\n", pindexPrev->nHeight, qcTx.nHeight);
199+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.nHeight[%d]\n", pindexPrev->nHeight, qcTx.nHeight);
207200
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-height");
208201
}
209202

@@ -220,18 +213,18 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx
220213

221214
if (qcTx.commitment.IsNull()) {
222215
if (!qcTx.commitment.VerifyNull()) {
223-
LogPrintfFinalCommitment("h[%d] invalid qcTx.commitment[%s] VerifyNull failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
216+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] VerifyNull failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
224217
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-invalid-null");
225218
}
226219
return true;
227220
}
228221

229222
if (!qcTx.commitment.Verify(dmnman, pQuorumBaseBlockIndex, false)) {
230-
LogPrintfFinalCommitment("h[%d] invalid qcTx.commitment[%s] Verify failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
223+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] invalid qcTx.commitment[%s] Verify failed\n", pindexPrev->nHeight, qcTx.commitment.quorumHash.ToString());
231224
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-invalid");
232225
}
233226

234-
LogPrintfFinalCommitment("h[%d] CheckLLMQCommitment VALID\n", pindexPrev->nHeight);
227+
LogPrint(BCLog::LLMQ, "CFinalCommitment -- h[%d] CheckLLMQCommitment VALID\n", pindexPrev->nHeight);
235228

236229
return true;
237230
}

0 commit comments

Comments
 (0)