Skip to content

Commit b53fab1

Browse files
committed
Merge bitcoin/bitcoin#32948: refactor: cleanup index logging
c18bf0b refactor: cleanup index logging (Sjors Provoost) Pull request description: This PR removes the use of `__func__` from index logging, since we have `-logsourcelocations`. It also improves readability by putting `GetName()` in a more logical place. Before > coinstatsindex: best block of the index not found. Please rebuild the index. After: > best block of coinstatsindex not found. Please rebuild the index. I found myself maintaining this commit as part of Sjors/bitcoin#86, but since that might never land here, it seemed better to split it into its own PR (or get rid of it). ACKs for top commit: l0rinc: Lightweight code review ACK c18bf0b maflcko: review ACK c18bf0b 🚣 Tree-SHA512: 755948371e3ff7a5515b63ce48075631ec7868d69c3c1469176d5be0e8b28e1c071e206ae3f7320f87d8c441f815894acfef61621f05795b5ff6b8a5a3031e3b
2 parents e72cb20 + c18bf0b commit b53fab1

File tree

4 files changed

+72
-72
lines changed

4 files changed

+72
-72
lines changed

src/index/base.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ bool BaseIndex::Init()
112112
// best chain, we will rewind to the fork point during index sync
113113
const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
114114
if (!locator_index) {
115-
return InitError(Untranslated(strprintf("%s: best block of the index not found. Please rebuild the index.", GetName())));
115+
return InitError(Untranslated(strprintf("best block of %s not found. Please rebuild the index.", GetName())));
116116
}
117117
SetBestBlockIndex(locator_index);
118118
}
@@ -156,8 +156,8 @@ bool BaseIndex::ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data
156156
CBlock block;
157157
if (!block_data) { // disk lookup if block data wasn't provided
158158
if (!m_chainstate->m_blockman.ReadBlock(block, *pindex)) {
159-
FatalErrorf("%s: Failed to read block %s from disk",
160-
__func__, pindex->GetBlockHash().ToString());
159+
FatalErrorf("Failed to read block %s from disk",
160+
pindex->GetBlockHash().ToString());
161161
return false;
162162
}
163163
block_info.data = █
@@ -166,16 +166,16 @@ bool BaseIndex::ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data
166166
CBlockUndo block_undo;
167167
if (CustomOptions().connect_undo_data) {
168168
if (pindex->nHeight > 0 && !m_chainstate->m_blockman.ReadBlockUndo(block_undo, *pindex)) {
169-
FatalErrorf("%s: Failed to read undo block data %s from disk",
170-
__func__, pindex->GetBlockHash().ToString());
169+
FatalErrorf("Failed to read undo block data %s from disk",
170+
pindex->GetBlockHash().ToString());
171171
return false;
172172
}
173173
block_info.undo_data = &block_undo;
174174
}
175175

176176
if (!CustomAppend(block_info)) {
177-
FatalErrorf("%s: Failed to write block %s to index database",
178-
__func__, pindex->GetBlockHash().ToString());
177+
FatalErrorf("Failed to write block %s to index database",
178+
pindex->GetBlockHash().ToString());
179179
return false;
180180
}
181181

@@ -190,7 +190,7 @@ void BaseIndex::Sync()
190190
std::chrono::steady_clock::time_point last_locator_write_time{0s};
191191
while (true) {
192192
if (m_interrupt) {
193-
LogPrintf("%s: m_interrupt set; exiting ThreadSync\n", GetName());
193+
LogInfo("%s: m_interrupt set; exiting ThreadSync", GetName());
194194

195195
SetBestBlockIndex(pindex);
196196
// No need to handle errors in Commit. If it fails, the error will be already be
@@ -221,7 +221,7 @@ void BaseIndex::Sync()
221221
}
222222
}
223223
if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
224-
FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName());
224+
FatalErrorf("Failed to rewind %s to a previous chain tip", GetName());
225225
return;
226226
}
227227
pindex = pindex_next;
@@ -231,7 +231,7 @@ void BaseIndex::Sync()
231231

232232
auto current_time{std::chrono::steady_clock::now()};
233233
if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
234-
LogPrintf("Syncing %s with block chain from height %d\n",
234+
LogInfo("Syncing %s with block chain from height %d",
235235
GetName(), pindex->nHeight);
236236
last_log_time = current_time;
237237
}
@@ -246,9 +246,9 @@ void BaseIndex::Sync()
246246
}
247247

248248
if (pindex) {
249-
LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
249+
LogInfo("%s is enabled at height %d", GetName(), pindex->nHeight);
250250
} else {
251-
LogPrintf("%s is enabled\n", GetName());
251+
LogInfo("%s is enabled", GetName());
252252
}
253253
}
254254

@@ -266,7 +266,7 @@ bool BaseIndex::Commit()
266266
}
267267
}
268268
if (!ok) {
269-
LogError("%s: Failed to commit latest %s state\n", __func__, GetName());
269+
LogError("Failed to commit latest %s state", GetName());
270270
return false;
271271
}
272272
return true;
@@ -284,8 +284,8 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
284284
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(iter_tip);
285285
if (CustomOptions().disconnect_data) {
286286
if (!m_chainstate->m_blockman.ReadBlock(block, *iter_tip)) {
287-
LogError("%s: Failed to read block %s from disk",
288-
__func__, iter_tip->GetBlockHash().ToString());
287+
LogError("Failed to read block %s from disk",
288+
iter_tip->GetBlockHash().ToString());
289289
return false;
290290
}
291291
block_info.data = &block;
@@ -336,8 +336,8 @@ void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const
336336
const CBlockIndex* best_block_index = m_best_block_index.load();
337337
if (!best_block_index) {
338338
if (pindex->nHeight != 0) {
339-
FatalErrorf("%s: First block connected is not the genesis block (height=%d)",
340-
__func__, pindex->nHeight);
339+
FatalErrorf("First block connected is not the genesis block (height=%d)",
340+
pindex->nHeight);
341341
return;
342342
}
343343
} else {
@@ -347,15 +347,15 @@ void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const
347347
// in the ValidationInterface queue backlog even after the sync thread has caught up to the
348348
// new chain tip. In this unlikely event, log a warning and let the queue clear.
349349
if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
350-
LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of "
351-
"known best chain (tip=%s); not updating index\n",
352-
__func__, pindex->GetBlockHash().ToString(),
350+
LogWarning("Block %s does not connect to an ancestor of "
351+
"known best chain (tip=%s); not updating index",
352+
pindex->GetBlockHash().ToString(),
353353
best_block_index->GetBlockHash().ToString());
354354
return;
355355
}
356356
if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
357-
FatalErrorf("%s: Failed to rewind index %s to a previous chain tip",
358-
__func__, GetName());
357+
FatalErrorf("Failed to rewind %s to a previous chain tip",
358+
GetName());
359359
return;
360360
}
361361
}
@@ -390,8 +390,8 @@ void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& loca
390390
}
391391

392392
if (!locator_tip_index) {
393-
FatalErrorf("%s: First block (hash=%s) in locator was not found",
394-
__func__, locator_tip_hash.ToString());
393+
FatalErrorf("First block (hash=%s) in locator was not found",
394+
locator_tip_hash.ToString());
395395
return;
396396
}
397397

@@ -402,9 +402,9 @@ void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& loca
402402
// event, log a warning and let the queue clear.
403403
const CBlockIndex* best_block_index = m_best_block_index.load();
404404
if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
405-
LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best "
406-
"chain (tip=%s); not writing index locator\n",
407-
__func__, locator_tip_hash.ToString(),
405+
LogWarning("Locator contains block (hash=%s) not on known best "
406+
"chain (tip=%s); not writing index locator",
407+
locator_tip_hash.ToString(),
408408
best_block_index->GetBlockHash().ToString());
409409
return;
410410
}
@@ -434,7 +434,7 @@ bool BaseIndex::BlockUntilSyncedToCurrentChain() const
434434
}
435435
}
436436

437-
LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
437+
LogInfo("%s is catching up on block notifications", GetName());
438438
m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
439439
return true;
440440
}

src/index/blockfilterindex.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& blo
126126
// indicate database corruption or a disk failure, and starting the index would cause
127127
// further corruption.
128128
if (m_db->Exists(DB_FILTER_POS)) {
129-
LogError("%s: Cannot read current %s state; index may be corrupted\n",
130-
__func__, GetName());
129+
LogError("Cannot read current %s state; index may be corrupted",
130+
GetName());
131131
return false;
132132
}
133133

@@ -139,7 +139,7 @@ bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& blo
139139
if (block) {
140140
auto op_last_header = ReadFilterHeader(block->height, block->hash);
141141
if (!op_last_header) {
142-
LogError("Cannot read last block filter header; index may be corrupted\n");
142+
LogError("Cannot read last block filter header; index may be corrupted");
143143
return false;
144144
}
145145
m_last_header = *op_last_header;
@@ -155,11 +155,11 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
155155
// Flush current filter file to disk.
156156
AutoFile file{m_filter_fileseq->Open(pos)};
157157
if (file.IsNull()) {
158-
LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
158+
LogError("Failed to open filter file %d", pos.nFile);
159159
return false;
160160
}
161161
if (!file.Commit()) {
162-
LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
162+
LogError("Failed to commit filter file %d", pos.nFile);
163163
(void)file.fclose();
164164
return false;
165165
}
@@ -185,13 +185,13 @@ bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256&
185185
try {
186186
filein >> block_hash >> encoded_filter;
187187
if (Hash(encoded_filter) != hash) {
188-
LogError("Checksum mismatch in filter decode.\n");
188+
LogError("Checksum mismatch in filter decode.");
189189
return false;
190190
}
191191
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
192192
}
193193
catch (const std::exception& e) {
194-
LogError("%s: Failed to deserialize block filter from disk: %s\n", __func__, e.what());
194+
LogError("Failed to deserialize block filter from disk: %s", e.what());
195195
return false;
196196
}
197197

@@ -210,15 +210,15 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
210210
if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
211211
AutoFile last_file{m_filter_fileseq->Open(pos)};
212212
if (last_file.IsNull()) {
213-
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
213+
LogError("Failed to open filter file %d", pos.nFile);
214214
return 0;
215215
}
216216
if (!last_file.Truncate(pos.nPos)) {
217-
LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
217+
LogError("Failed to truncate filter file %d", pos.nFile);
218218
return 0;
219219
}
220220
if (!last_file.Commit()) {
221-
LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
221+
LogError("Failed to commit filter file %d", pos.nFile);
222222
(void)last_file.fclose();
223223
return 0;
224224
}
@@ -235,13 +235,13 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
235235
bool out_of_space;
236236
m_filter_fileseq->Allocate(pos, data_size, out_of_space);
237237
if (out_of_space) {
238-
LogPrintf("%s: out of disk space\n", __func__);
238+
LogError("out of disk space");
239239
return 0;
240240
}
241241

242242
AutoFile fileout{m_filter_fileseq->Open(pos)};
243243
if (fileout.IsNull()) {
244-
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
244+
LogError("Failed to open filter file %d", pos.nFile);
245245
return 0;
246246
}
247247

@@ -263,8 +263,8 @@ std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint
263263
}
264264

265265
if (read_out.first != expected_block_hash) {
266-
LogError("%s: previous block header belongs to unexpected block %s; expected %s\n",
267-
__func__, read_out.first.ToString(), expected_block_hash.ToString());
266+
LogError("previous block header belongs to unexpected block %s; expected %s",
267+
read_out.first.ToString(), expected_block_hash.ToString());
268268
return std::nullopt;
269269
}
270270

@@ -306,15 +306,15 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
306306
db_it.Seek(key);
307307

308308
if (!db_it.GetKey(key) || key.height != height) {
309-
LogError("%s: unexpected key in %s: expected (%c, %d)\n",
310-
__func__, index_name, DB_BLOCK_HEIGHT, height);
309+
LogError("unexpected key in %s: expected (%c, %d)",
310+
index_name, DB_BLOCK_HEIGHT, height);
311311
return false;
312312
}
313313

314314
std::pair<uint256, DBVal> value;
315315
if (!db_it.GetValue(value)) {
316-
LogError("%s: unable to read value in %s at key (%c, %d)\n",
317-
__func__, index_name, DB_BLOCK_HEIGHT, height);
316+
LogError("unable to read value in %s at key (%c, %d)",
317+
index_name, DB_BLOCK_HEIGHT, height);
318318
return false;
319319
}
320320

@@ -367,12 +367,12 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
367367
const CBlockIndex* stop_index, std::vector<DBVal>& results)
368368
{
369369
if (start_height < 0) {
370-
LogError("%s: start height (%d) is negative\n", __func__, start_height);
370+
LogError("start height (%d) is negative", start_height);
371371
return false;
372372
}
373373
if (start_height > stop_index->nHeight) {
374-
LogError("%s: start height (%d) is greater than stop height (%d)\n",
375-
__func__, start_height, stop_index->nHeight);
374+
LogError("start height (%d) is greater than stop height (%d)",
375+
start_height, stop_index->nHeight);
376376
return false;
377377
}
378378

@@ -389,8 +389,8 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
389389

390390
size_t i = static_cast<size_t>(height - start_height);
391391
if (!db_it->GetValue(values[i])) {
392-
LogError("%s: unable to read value in %s at key (%c, %d)\n",
393-
__func__, index_name, DB_BLOCK_HEIGHT, height);
392+
LogError("unable to read value in %s at key (%c, %d)",
393+
index_name, DB_BLOCK_HEIGHT, height);
394394
return false;
395395
}
396396

@@ -413,8 +413,8 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
413413
}
414414

415415
if (!db.Read(DBHashKey(block_hash), results[i])) {
416-
LogError("%s: unable to read value in %s at key (%c, %s)\n",
417-
__func__, index_name, DB_BLOCK_HASH, block_hash.ToString());
416+
LogError("unable to read value in %s at key (%c, %s)",
417+
index_name, DB_BLOCK_HASH, block_hash.ToString());
418418
return false;
419419
}
420420
}

0 commit comments

Comments
 (0)