]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Derive PeerConn logger from Torrent after setTorrent
authorMatt Joiner <anacrolix@gmail.com>
Tue, 27 May 2025 14:28:46 +0000 (00:28 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 27 May 2025 14:28:46 +0000 (00:28 +1000)
client.go
peer.go
peerconn.go
torrent.go
webseed-peer.go

index 3bd5fa489a34bd7f641c073e9e20e3c4161102a7..f7ee73e7af9a113ba17b8f91caa26fd8f919b544 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1735,8 +1735,7 @@ func (cl *Client) newConnection(nc net.Conn, opts newConnectionOpts) (c *PeerCon
        }
        c.legacyPeerImpl = c
        c.peerImpl = c
-       c.logger = cl.logger.WithDefaultLevel(log.Warning).WithContextText(fmt.Sprintf("%T %p", c, c))
-       c.protocolLogger = c.logger.WithNames(protocolLoggingName)
+       c.setPeerLoggers(cl.logger, cl.slogger)
        c.setRW(connStatsReadWriter{nc, c})
        c.r = &rateLimitedReader{
                l: cl.config.DownloadRateLimiter,
diff --git a/peer.go b/peer.go
index 530631eccbf38b69cf5d2e6e3e4149b09bb64a58..988eb07bc79c486ad10525e7e929e84290f37f45 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -4,6 +4,7 @@ import (
        "errors"
        "fmt"
        "io"
+       "log/slog"
        "net"
        "strings"
        "sync"
@@ -95,7 +96,8 @@ type (
 
                PeerMaxRequests maxRequests // Maximum pending requests the peer allows.
 
-               logger log.Logger
+               logger  log.Logger
+               slogger *slog.Logger
        }
 
        PeerSource string
index 6c643f83400514981fe43fcf6f093019e6624b1c..d72e00f3c5d64fb74434c7bf2cb340498b18d85a 100644 (file)
@@ -7,6 +7,7 @@ import (
        "errors"
        "fmt"
        "io"
+       "log/slog"
        "math/rand"
        "net"
        "net/netip"
@@ -1119,13 +1120,14 @@ func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRe
        })
 }
 
-func (c *Peer) setTorrent(t *Torrent) {
+func (c *PeerConn) setTorrent(t *Torrent) {
        if c.t != nil {
                panic("connection already associated with a torrent")
        }
        c.t = t
        c.logger.WithDefaultLevel(log.Debug).Printf("set torrent=%v", t)
-       t.reconcileHandshakeStats(c)
+       c.setPeerLoggers(t.logger, t.slogger())
+       t.reconcileHandshakeStats(c.peerPtr())
 }
 
 func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
@@ -1458,3 +1460,11 @@ func (me *PeerConn) peerPtr() *Peer {
 func (cn *PeerConn) nominalMaxRequests() maxRequests {
        return max(1, min(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests))
 }
+
+// Set the Peer loggers. This is given Client loggers, and later Torrent loggers when the Torrent is
+// set.
+func (me *PeerConn) setPeerLoggers(a log.Logger, s *slog.Logger) {
+       me.Peer.logger = a.WithDefaultLevel(log.Warning).WithContextText(fmt.Sprintf("%T %p", me, me))
+       me.Peer.slogger = s.With(fmt.Sprintf("%T", me), fmt.Sprintf("%p", me))
+       me.protocolLogger = me.logger.WithNames(protocolLoggingName)
+}
index a3a484eb394fbe4a220f94cf73284317e5c9d5a2..d68c3f49cd8f37f233edd844212926ef69be1c7f 100644 (file)
@@ -3060,6 +3060,7 @@ func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) bool {
                f(&ws.peer)
        }
        ws.peer.logger = t.logger.WithContextValue(&ws).WithNames("webseed")
+       ws.peer.slogger = t.slogger().With("webseed", url)
        // TODO: Abstract out a common struct initializer for this...
        ws.peer.legacyPeerImpl = &ws
        ws.peer.peerImpl = &ws
index 43bb78e3a366e669895ec2bc3aef1b6da7b16f8d..bedbb927e12cd10b74a361dbbf86cc1d2484f8a2 100644 (file)
@@ -134,7 +134,7 @@ func (ws *webseedPeer) spawnRequest(begin, end RequestIndex) {
        }
        ws.activeRequests[&wsReq] = struct{}{}
        ws.peer.t.cl.numWebSeedRequests++
-       ws.peer.logger.Slogger().Debug(
+       ws.slogger().Debug(
                "starting webseed request",
                "begin", begin,
                "end", end,
@@ -362,5 +362,5 @@ func (cn *webseedPeer) peerHasAllPieces() (all, known bool) {
 }
 
 func (me *webseedPeer) slogger() *slog.Logger {
-       return me.peer.logger.Slogger()
+       return me.peer.slogger
 }