]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Improve Torrent slogger use
authorMatt Joiner <anacrolix@gmail.com>
Fri, 16 May 2025 05:17:37 +0000 (15:17 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 16 May 2025 05:17:37 +0000 (15:17 +1000)
client.go
lazy-log-valuer.go [new file with mode: 0644]
logging_test.go [new file with mode: 0644]
math.go [new file with mode: 0644]
piece.go
torrent.go
type-sizes_test.go

index 4fffd5fd2fd5d894538fd945dd35ed033ae4ddb8..ce9ca2e4611dee09d6b1661b36bfa3b1bb3a6705 100644 (file)
--- 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 (file)
index 0000000..0529b8c
--- /dev/null
@@ -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 (file)
index 0000000..a108cc5
--- /dev/null
@@ -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 (file)
index 0000000..10cbafc
--- /dev/null
+++ b/math.go
@@ -0,0 +1 @@
+package torrent
index da0b94c4d68f1c44be5558780ce2649a0441eb25..2df0dd368ec485475d168ccf6764bb05e29cb321 100644 (file)
--- 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"
index b59202a8abc9663b831e579d38caa35381729f9a..5fbec4b3f9ca4dc4d025d5775b9835429046fa9a 100644 (file)
@@ -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()
                                }
                        }
index 116d2aa275e9dea04f6f899d3542791f4b58f375..a55946536274b9e0558f921d21028ef39cfddc6e 100644 (file)
@@ -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"
 )