2022-06-14 17:58:35 +02:00
|
|
|
// Copyright (c) 2015-2017 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-06-14 19:06:45 +02:00
|
|
|
package claimtrie
|
2022-06-14 17:58:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btclog"
|
|
|
|
)
|
|
|
|
|
2022-06-14 19:38:43 +02:00
|
|
|
// claimProgressLogger provides periodic logging for other services in order
|
2022-06-14 17:58:35 +02:00
|
|
|
// to show users progress of certain "actions" involving some or all current
|
2022-06-14 19:06:45 +02:00
|
|
|
// claim names. Ex: rebuilding claimtrie.
|
2022-06-14 19:38:43 +02:00
|
|
|
type claimProgressLogger struct {
|
2022-06-14 19:06:45 +02:00
|
|
|
totalLogName int64
|
|
|
|
recentLogName int64
|
|
|
|
lastLogNameTime time.Time
|
2022-06-14 17:58:35 +02:00
|
|
|
|
|
|
|
subsystemLogger btclog.Logger
|
|
|
|
progressAction string
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:38:43 +02:00
|
|
|
// newClaimProgressLogger returns a new name progress logger.
|
2022-06-14 17:58:35 +02:00
|
|
|
// The progress message is templated as follows:
|
2022-06-14 19:06:45 +02:00
|
|
|
// {progressAction} {numProcessed} {names|name} in the last {timePeriod} (total {totalProcessed})
|
2022-06-14 19:38:43 +02:00
|
|
|
func newClaimProgressLogger(progressMessage string, logger btclog.Logger) *claimProgressLogger {
|
|
|
|
return &claimProgressLogger{
|
2022-06-14 19:06:45 +02:00
|
|
|
lastLogNameTime: time.Now(),
|
|
|
|
progressAction: progressMessage,
|
|
|
|
subsystemLogger: logger,
|
2022-06-14 17:58:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:38:43 +02:00
|
|
|
// LogName logs a new claim name as an information message to show progress
|
|
|
|
// to the user. In order to prevent spam, it limits logging to one message
|
|
|
|
// every 10 seconds with duration and totals included.
|
|
|
|
func (n *claimProgressLogger) LogName(name []byte) {
|
2022-06-14 19:06:45 +02:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2022-06-14 17:58:35 +02:00
|
|
|
|
2022-06-14 19:06:45 +02:00
|
|
|
n.totalLogName++
|
|
|
|
n.recentLogName++
|
2022-06-14 17:58:35 +02:00
|
|
|
|
|
|
|
now := time.Now()
|
2022-06-14 19:06:45 +02:00
|
|
|
duration := now.Sub(n.lastLogNameTime)
|
2022-06-14 17:58:35 +02:00
|
|
|
if duration < time.Second*10 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the duration to 10s of milliseconds.
|
|
|
|
durationMillis := int64(duration / time.Millisecond)
|
|
|
|
tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10)
|
|
|
|
|
2022-06-14 19:06:45 +02:00
|
|
|
// Log information about progress.
|
|
|
|
nameStr := "names"
|
|
|
|
if n.recentLogName == 1 {
|
|
|
|
nameStr = "name"
|
2022-06-14 17:58:35 +02:00
|
|
|
}
|
2022-06-14 19:38:43 +02:00
|
|
|
n.subsystemLogger.Infof("%s %d claim %s in the last %s (total %d)",
|
2022-06-14 19:06:45 +02:00
|
|
|
n.progressAction, n.recentLogName, nameStr, tDuration, n.totalLogName)
|
2022-06-14 17:58:35 +02:00
|
|
|
|
2022-06-14 19:06:45 +02:00
|
|
|
n.recentLogName = 0
|
|
|
|
n.lastLogNameTime = now
|
2022-06-14 17:58:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 19:38:43 +02:00
|
|
|
func (n *claimProgressLogger) SetLastLogTime(time time.Time) {
|
2022-06-14 19:06:45 +02:00
|
|
|
n.lastLogNameTime = time
|
2022-06-14 17:58:35 +02:00
|
|
|
}
|