github.com/lbryio/lbcd@v0.22.119/claimtrie/logger.go (about) 1 // Copyright (c) 2015-2017 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package claimtrie 6 7 import ( 8 "sync" 9 "time" 10 11 "github.com/btcsuite/btclog" 12 ) 13 14 // claimProgressLogger provides periodic logging for other services in order 15 // to show users progress of certain "actions" involving some or all current 16 // claim names. Ex: rebuilding claimtrie. 17 type claimProgressLogger struct { 18 totalLogName int64 19 recentLogName int64 20 lastLogNameTime time.Time 21 22 subsystemLogger btclog.Logger 23 progressAction string 24 sync.Mutex 25 } 26 27 // newClaimProgressLogger returns a new name progress logger. 28 // The progress message is templated as follows: 29 // 30 // {progressAction} {numProcessed} {names|name} in the last {timePeriod} (total {totalProcessed}) 31 func newClaimProgressLogger(progressMessage string, logger btclog.Logger) *claimProgressLogger { 32 return &claimProgressLogger{ 33 lastLogNameTime: time.Now(), 34 progressAction: progressMessage, 35 subsystemLogger: logger, 36 } 37 } 38 39 // LogName logs a new claim name as an information message to show progress 40 // to the user. In order to prevent spam, it limits logging to one message 41 // every 10 seconds with duration and totals included. 42 func (n *claimProgressLogger) LogName(name []byte) { 43 n.Lock() 44 defer n.Unlock() 45 46 n.totalLogName++ 47 n.recentLogName++ 48 49 now := time.Now() 50 duration := now.Sub(n.lastLogNameTime) 51 if duration < time.Second*10 { 52 return 53 } 54 55 // Truncate the duration to 10s of milliseconds. 56 durationMillis := int64(duration / time.Millisecond) 57 tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10) 58 59 // Log information about progress. 60 nameStr := "names" 61 if n.recentLogName == 1 { 62 nameStr = "name" 63 } 64 n.subsystemLogger.Infof("%s %d claim %s in the last %s (total %d)", 65 n.progressAction, n.recentLogName, nameStr, tDuration, n.totalLogName) 66 67 n.recentLogName = 0 68 n.lastLogNameTime = now 69 } 70 71 func (n *claimProgressLogger) SetLastLogTime(time time.Time) { 72 n.lastLogNameTime = time 73 }