From: Matt Joiner Date: Fri, 16 May 2025 05:17:37 +0000 (+1000) Subject: Improve Torrent slogger use X-Git-Tag: v1.59.0~138 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=606337eee5ee6ee1ff02e17d484196060c27003f;p=btrtrc.git Improve Torrent slogger use --- diff --git a/client.go b/client.go index 4fffd5fd..ce9ca2e4 100644 --- a/client.go +++ b/client.go @@ -1386,7 +1386,8 @@ func (cl *Client) newTorrentOpt(opts AddTorrentOpts) (t *Torrent) { t.networkingEnabled.Set() ihHex := t.InfoHash().HexString() t.logger = cl.logger.WithDefaultLevel(log.Debug).WithNames(ihHex).WithContextText(ihHex) - t._slogger = cl.slogger + t.name() + t._slogger = t.withSlogger(cl.slogger) t.sourcesLogger = t.logger.WithNames("sources") if opts.ChunkSize == 0 { opts.ChunkSize = defaultChunkSize diff --git a/lazy-log-valuer.go b/lazy-log-valuer.go new file mode 100644 index 00000000..0529b8cf --- /dev/null +++ b/lazy-log-valuer.go @@ -0,0 +1,13 @@ +package torrent + +import ( + "log/slog" +) + +type lazyLogValuer func() any + +func (me lazyLogValuer) LogValue() slog.Value { + return slog.AnyValue(me()) +} + +var _ slog.LogValuer = lazyLogValuer(nil) diff --git a/logging_test.go b/logging_test.go new file mode 100644 index 00000000..a108cc5e --- /dev/null +++ b/logging_test.go @@ -0,0 +1,29 @@ +package torrent + +import ( + "bytes" + "log/slog" + "math" + "testing" +) + +type testHandler struct{} + +func TestLazyLogValuer(t *testing.T) { + // This is a test to ensure that the lazyLogValuer type implements the slog.LogValuer interface. + // The test is intentionally left empty because the implementation is already verified by the + // compiler. + + //h := testHandler{} + //l := slog.New(h) + var buf bytes.Buffer + h := slog.NewTextHandler(&buf, &slog.HandlerOptions{ + Level: slog.Level(math.MinInt), + }) + l := slog.New(h) + cl := newTestingClient(t) + tor := cl.newTorrentForTesting() + l2 := tor.withSlogger(l) + l2.Debug("hi") + t.Log(buf.String()) +} diff --git a/math.go b/math.go new file mode 100644 index 00000000..10cbafc7 --- /dev/null +++ b/math.go @@ -0,0 +1 @@ +package torrent diff --git a/piece.go b/piece.go index da0b94c4..2df0dd36 100644 --- a/piece.go +++ b/piece.go @@ -4,13 +4,13 @@ import ( "context" "errors" "fmt" - "github.com/anacrolix/missinggo/v2/panicif" "iter" "sync" "github.com/anacrolix/chansync" g "github.com/anacrolix/generics" "github.com/anacrolix/missinggo/v2/bitmap" + "github.com/anacrolix/missinggo/v2/panicif" "github.com/anacrolix/torrent/merkle" "github.com/anacrolix/torrent/metainfo" diff --git a/torrent.go b/torrent.go index b59202a8..5fbec4b3 100644 --- a/torrent.go +++ b/torrent.go @@ -672,18 +672,24 @@ func (t *Torrent) setMetadataSize(size int) (err error) { return } -// The current working name for the torrent. Either the name in the info dict, -// or a display name given such as by the dn value in a magnet link, or "". -func (t *Torrent) name() string { +// Returns the best name for the torrent. This is either the name in the info dict, or a display +// name if the info isn't known yet. +func (t *Torrent) bestName() (_ g.Option[string]) { t.nameMu.RLock() defer t.nameMu.RUnlock() if t.haveInfo() { - return t.info.BestName() + return g.Some(t.info.BestName()) } if t.displayName != "" { - return t.displayName + return g.Some(t.displayName) } - return "infohash:" + t.canonicalShortInfohash().HexString() + return +} + +// The current working name for the torrent. Either the name in the info dict, or a display name +// given such as by the dn value in a magnet link, or "". +func (t *Torrent) name() string { + return t.bestName().UnwrapOr("infohash:" + t.canonicalShortInfohash().HexString()) } func (t *Torrent) pieceState(index pieceIndex) (ret PieceState) { @@ -2523,12 +2529,10 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) { // single peer for a piece, and we never progress that piece to completion, we // will never smart-ban them. Discovered in // https://github.com/anacrolix/torrent/issues/715. - t.logger.Levelf( - log.Warning, - "banning %v for being sole dirtier of piece %v after failed piece check", - c, - piece, - ) + t.slogger().Warn( + "piece failed hash. banning peer", + "piece", piece, + "peer", c) c.ban() } } diff --git a/type-sizes_test.go b/type-sizes_test.go index 116d2aa2..a5594653 100644 --- a/type-sizes_test.go +++ b/type-sizes_test.go @@ -1,10 +1,10 @@ package torrent import ( - "github.com/anacrolix/chansync" "reflect" "testing" + "github.com/anacrolix/chansync" g "github.com/anacrolix/generics" "github.com/go-quicktest/qt" )