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