github.com/decred/dcrd/blockchain@v1.2.1/internal/progresslog/blocklogger.go (about) 1 // Copyright (c) 2016 The btcsuite developers 2 // Copyright (c) 2016-2018 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package progresslog 7 8 import ( 9 "sync" 10 "time" 11 12 "github.com/decred/slog" 13 14 "github.com/decred/dcrd/wire" 15 ) 16 17 // BlockProgressLogger provides periodic logging for other services in order 18 // to show users progress of certain "actions" involving some or all current 19 // blocks. Ex: syncing to best chain, indexing all blocks, etc. 20 type BlockProgressLogger struct { 21 receivedLogBlocks int64 22 receivedLogTx int64 23 lastBlockLogTime time.Time 24 25 subsystemLogger slog.Logger 26 progressAction string 27 sync.Mutex 28 } 29 30 // NewBlockProgressLogger returns a new block progress logger. 31 // The progress message is templated as follows: 32 // {progressAction} {numProcessed} {blocks|block} in the last {timePeriod} 33 // ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp}) 34 func NewBlockProgressLogger(progressMessage string, logger slog.Logger) *BlockProgressLogger { 35 return &BlockProgressLogger{ 36 lastBlockLogTime: time.Now(), 37 progressAction: progressMessage, 38 subsystemLogger: logger, 39 } 40 } 41 42 // LogBlockHeight logs a new block height as an information message to show 43 // progress to the user. In order to prevent spam, it limits logging to one 44 // message every 10 seconds with duration and totals included. 45 func (b *BlockProgressLogger) LogBlockHeight(block, parent *wire.MsgBlock) { 46 b.Lock() 47 defer b.Unlock() 48 49 b.receivedLogBlocks++ 50 b.receivedLogTx += int64(len(block.Transactions)) 51 b.receivedLogTx += int64(len(block.STransactions)) 52 53 now := time.Now() 54 duration := now.Sub(b.lastBlockLogTime) 55 if duration < time.Second*10 { 56 return 57 } 58 59 // Truncate the duration to 10s of milliseconds. 60 durationMillis := int64(duration / time.Millisecond) 61 tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10) 62 63 // Log information about new block height. 64 blockStr := "blocks" 65 if b.receivedLogBlocks == 1 { 66 blockStr = "block" 67 } 68 txStr := "transactions" 69 if b.receivedLogTx == 1 { 70 txStr = "transaction" 71 } 72 b.subsystemLogger.Infof("%s %d %s in the last %s (%d %s, height %d, %s)", 73 b.progressAction, b.receivedLogBlocks, blockStr, tDuration, 74 b.receivedLogTx, txStr, block.Header.Height, 75 block.Header.Timestamp) 76 77 b.receivedLogBlocks = 0 78 b.receivedLogTx = 0 79 b.lastBlockLogTime = now 80 }