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
--- /dev/null
+package torrent
+
+import (
+ "log/slog"
+)
+
+type lazyLogValuer func() any
+
+func (me lazyLogValuer) LogValue() slog.Value {
+ return slog.AnyValue(me())
+}
+
+var _ slog.LogValuer = lazyLogValuer(nil)
--- /dev/null
+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())
+}
--- /dev/null
+package torrent
"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"
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) {
// 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()
}
}
package torrent
import (
- "github.com/anacrolix/chansync"
"reflect"
"testing"
+ "github.com/anacrolix/chansync"
g "github.com/anacrolix/generics"
"github.com/go-quicktest/qt"
)