Skip to content

Commit

Permalink
ltc: better ltcwallet logging (#2764)
Browse files Browse the repository at this point in the history
* update ltc testnet seeds. better ltcwallet logging
  • Loading branch information
buck54321 authored May 20, 2024
1 parent 43fa030 commit 90fd94d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 66 deletions.
104 changes: 50 additions & 54 deletions client/asset/ltc/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sync/atomic"
"time"
Expand Down Expand Up @@ -39,7 +38,6 @@ import (
_ "github.com/dcrlabs/ltcwallet/walletdb/bdb"
ltcwtxmgr "github.com/dcrlabs/ltcwallet/wtxmgr"
"github.com/decred/slog"
"github.com/jrick/logrotate/rotator"
btcneutrino "github.com/lightninglabs/neutrino"
"github.com/lightninglabs/neutrino/headerfs"
ltcchaincfg "github.com/ltcsuite/ltcd/chaincfg"
Expand Down Expand Up @@ -1029,18 +1027,6 @@ var (
logFileName = "neutrino.log"
)

// logRotator initializes a rotating file logger.
func logRotator(walletDir string) (*rotator.Rotator, error) {
const maxLogRolls = 8
logDir := filepath.Join(walletDir, logDirName)
if err := os.MkdirAll(logDir, 0744); err != nil {
return nil, fmt.Errorf("error creating log directory: %w", err)
}

logFilename := filepath.Join(logDir, logFileName)
return rotator.New(logFilename, 32*1024, false, maxLogRolls)
}

// logNeutrino initializes logging in the neutrino + wallet packages. Logging
// only has to be initialized once, so an atomic flag is used internally to
// return early on subsequent invocations.
Expand All @@ -1049,79 +1035,89 @@ func logRotator(walletDir string) (*rotator.Rotator, error) {
// there are concurrency issues with that since btcd and btcwallet have
// unsupervised goroutines still running after shutdown. So we leave the rotator
// running at the risk of losing some logs.
func logNeutrino(walletDir string, errorLogger dex.Logger) error {
func logNeutrino(walletDir string, baseLogger dex.Logger) error {
if !atomic.CompareAndSwapUint32(&loggingInited, 0, 1) {
return nil
}

logSpinner, err := logRotator(walletDir)
logDir := filepath.Join(walletDir, logDirName)
logSpinner, err := dex.LogRotator(logDir, logFileName)
if err != nil {
return fmt.Errorf("error initializing log rotator: %w", err)
}

backendLog := btclog.NewBackend(logSpinner)
fileLogger := baseLogger.FileLogger(logSpinner)
log := newFileLoggerPlus(baseLogger, fileLogger)

logger := func(name string, lvl btclog.Level) btclog.Logger {
l := backendLog.Logger(name)
l.SetLevel(lvl)
return &fileLoggerPlus{Logger: l, log: errorLogger.SubLogger(name)}
}

neutrino.UseLogger(logger("NTRNO", btclog.LevelDebug))
wallet.UseLogger(logger("LTCW", btclog.LevelInfo))
ltcwtxmgr.UseLogger(logger("TXMGR", btclog.LevelInfo))
chain.UseLogger(logger("CHAIN", btclog.LevelInfo))
neutrino.UseLogGenerator(log)
wallet.UseLogger(log)

return nil
}

// logAdapter adapts dex.Logger to the btclog.Logger interface.
type logAdapter struct {
dex.Logger
}

var _ btclog.Logger = (*logAdapter)(nil)

func (a *logAdapter) Level() btclog.Level {
return btclog.Level(a.Logger.Level())
}

func (a *logAdapter) SetLevel(lvl btclog.Level) {
a.Logger.SetLevel(slog.Level(lvl))
}

// fileLoggerPlus logs everything to a file, and everything with level >= warn
// to both file and a specified dex.Logger.
type fileLoggerPlus struct {
btclog.Logger
log dex.Logger
fileLogger dex.Logger
baseLogger dex.Logger
}

func newFileLoggerPlus(baseLogger, fileLogger dex.Logger) *fileLoggerPlus {
return &fileLoggerPlus{
Logger: &logAdapter{fileLogger},
fileLogger: fileLogger,
baseLogger: baseLogger,
}
}

// NewLogger satisfies LogGenerator interface.
func (f *fileLoggerPlus) NewLogger(name string) btclog.Logger {
fileLogger := f.fileLogger.SubLogger(name)
return newFileLoggerPlus(f.baseLogger.SubLogger(name), fileLogger)
}

func (f *fileLoggerPlus) Warnf(format string, params ...any) {
f.log.Warnf(format, params...)
f.Logger.Warnf(format, params...)
f.baseLogger.Warnf(format, params...)
f.fileLogger.Warnf(format, params...)
}

func (f *fileLoggerPlus) Errorf(format string, params ...any) {
f.log.Errorf(format, params...)
f.Logger.Errorf(format, params...)
f.baseLogger.Errorf(format, params...)
f.fileLogger.Errorf(format, params...)
}

func (f *fileLoggerPlus) Criticalf(format string, params ...any) {
f.log.Criticalf(format, params...)
f.Logger.Criticalf(format, params...)
f.baseLogger.Criticalf(format, params...)
f.fileLogger.Criticalf(format, params...)
}

func (f *fileLoggerPlus) Warn(v ...any) {
f.log.Warn(v...)
f.Logger.Warn(v...)
f.baseLogger.Warn(v...)
f.fileLogger.Warn(v...)
}

func (f *fileLoggerPlus) Error(v ...any) {
f.log.Error(v...)
f.Logger.Error(v...)
f.baseLogger.Error(v...)
f.fileLogger.Error(v...)
}

func (f *fileLoggerPlus) Critical(v ...any) {
f.log.Critical(v...)
f.Logger.Critical(v...)
}

type logAdapter struct {
dex.Logger
}

var _ btclog.Logger = (*logAdapter)(nil)

func (a *logAdapter) Level() btclog.Level {
return btclog.Level(a.Logger.Level())
}

func (a *logAdapter) SetLevel(lvl btclog.Level) {
a.Logger.SetLevel(slog.Level(lvl))
f.baseLogger.Critical(v...)
f.fileLogger.Critical(v...)
}
2 changes: 1 addition & 1 deletion client/cmd/dexc-desktop/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
)

require (
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822 // indirect
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a // indirect
github.com/ltcsuite/lnd/tlv v0.0.0-20240222214433-454d35886119 // indirect
github.com/ltcsuite/ltcd/chaincfg/chainhash v1.0.2 // indirect
)
Expand Down
4 changes: 2 additions & 2 deletions client/cmd/dexc-desktop/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIX
github.com/dcrlabs/bchwallet v0.0.0-20240114115928-2a995d024eed/go.mod h1:dX7SZgs+dmGL56e6KHn+MktfriF/aM2qI4yYK56ySEQ=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be h1:F61HTz77YatFuMgleG4Hp4QpXWJgjSG+S72YlXD4jPA=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be/go.mod h1:Km7vMslEkg88jIFE2TA/XX7kAvXvnsoGTXN9ktYSi98=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822 h1:JZGfXO22Vv0gMOv0zjPqcFnPAGcNN1WV+fs7Xer10gE=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a h1:KJtx5FcK8oXCiXGoBpzQvGtRnhC1Q3UFKh7UVQ8YKSI=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095 h1:oGXgq+wZ2FvBzBTqXnOJ+ZP6f79zhfJNd46nDkJaUdU=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095/go.mod h1:aymH7e5PUsdsLagAIIMbpyJIff0Kv8/BJg7nolIVJbU=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
Expand Down
31 changes: 28 additions & 3 deletions dex/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/decred/slog"
"github.com/jrick/logrotate/rotator"
)

// Disabled is a Logger that will never output anything.
Expand All @@ -36,6 +38,7 @@ const (
type Logger interface {
slog.Logger
SubLogger(name string) Logger
FileLogger(r *rotator.Rotator) Logger
}

// LoggerMaker allows creation of new log subsystems with predefined levels.
Expand All @@ -60,22 +63,44 @@ type logger struct {
// used.
func (lggr *logger) SubLogger(name string) Logger {
combinedName := fmt.Sprintf("%s[%s]", lggr.name, name)
newLggr := lggr.backend.Logger(combinedName)
return lggr.newLoggerWithBackend(lggr.backend, combinedName)
}

// FileLogger creates a logger that logs to a file rotator. Subloggers will also
// log to the file only.
func (lggr *logger) FileLogger(r *rotator.Rotator) Logger {
return lggr.newLoggerWithBackend(slog.NewBackend(r), lggr.name)
}

func (lggr *logger) newLoggerWithBackend(backend *slog.Backend, name string) *logger {
level := lggr.level
// If name is in the levels map, use that level.
if lvl, ok := lggr.levels[name]; ok {
level = lvl
}

newLggr := backend.Logger(name)
newLggr.SetLevel(level)
return &logger{
Logger: newLggr,
name: combinedName,
name: name,
level: level,
levels: lggr.levels,
backend: lggr.backend,
backend: backend,
}
}

// LogRotator creates a file logger that rotates up to 8 files of 32 MiB each.
func LogRotator(dir, name string) (*rotator.Rotator, error) {
const maxLogRolls = 8
if err := os.MkdirAll(dir, 0744); err != nil {
return nil, fmt.Errorf("error creating log directory: %w", err)
}

logFilename := filepath.Join(dir, name)
return rotator.New(logFilename, 32*1024, false, maxLogRolls)
}

func inUTC() slog.BackendOption {
return slog.WithFlags(slog.LUTC)
}
Expand Down
2 changes: 1 addition & 1 deletion dex/testing/loadbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
)

require (
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822 // indirect
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a // indirect
github.com/ltcsuite/lnd/tlv v0.0.0-20240222214433-454d35886119 // indirect
github.com/ltcsuite/ltcd/chaincfg/chainhash v1.0.2 // indirect
)
Expand Down
4 changes: 2 additions & 2 deletions dex/testing/loadbot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIX
github.com/dcrlabs/bchwallet v0.0.0-20240114115928-2a995d024eed/go.mod h1:dX7SZgs+dmGL56e6KHn+MktfriF/aM2qI4yYK56ySEQ=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be h1:F61HTz77YatFuMgleG4Hp4QpXWJgjSG+S72YlXD4jPA=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be/go.mod h1:Km7vMslEkg88jIFE2TA/XX7kAvXvnsoGTXN9ktYSi98=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822 h1:JZGfXO22Vv0gMOv0zjPqcFnPAGcNN1WV+fs7Xer10gE=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a h1:KJtx5FcK8oXCiXGoBpzQvGtRnhC1Q3UFKh7UVQ8YKSI=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095 h1:oGXgq+wZ2FvBzBTqXnOJ+ZP6f79zhfJNd46nDkJaUdU=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095/go.mod h1:aymH7e5PUsdsLagAIIMbpyJIff0Kv8/BJg7nolIVJbU=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/dchest/blake2b v1.0.0
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095
github.com/decred/base58 v1.0.5
github.com/decred/dcrd/addrmgr/v2 v2.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIX
github.com/dcrlabs/bchwallet v0.0.0-20240114115928-2a995d024eed/go.mod h1:dX7SZgs+dmGL56e6KHn+MktfriF/aM2qI4yYK56ySEQ=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be h1:F61HTz77YatFuMgleG4Hp4QpXWJgjSG+S72YlXD4jPA=
github.com/dcrlabs/bchwallet v0.0.0-20240114124852-0e95005810be/go.mod h1:Km7vMslEkg88jIFE2TA/XX7kAvXvnsoGTXN9ktYSi98=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822 h1:JZGfXO22Vv0gMOv0zjPqcFnPAGcNN1WV+fs7Xer10gE=
github.com/dcrlabs/ltcwallet v0.0.0-20240510140852-5b5906621822/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a h1:KJtx5FcK8oXCiXGoBpzQvGtRnhC1Q3UFKh7UVQ8YKSI=
github.com/dcrlabs/ltcwallet v0.0.0-20240518141247-13553c8fce6a/go.mod h1:UwMkslDKQGQx7UCGjolqPKulzX3YQxMhDvVZ0KukIQk=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095 h1:oGXgq+wZ2FvBzBTqXnOJ+ZP6f79zhfJNd46nDkJaUdU=
github.com/dcrlabs/neutrino-bch v0.0.0-20240114121828-d656bce11095/go.mod h1:aymH7e5PUsdsLagAIIMbpyJIff0Kv8/BJg7nolIVJbU=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
Expand Down

0 comments on commit 90fd94d

Please sign in to comment.