client.go | 12 +++--------- go.mod | 2 +- go.sum | 2 ++ peer-impl.go | 1 + torrent.go | 1 + web_seed.go | 8 ++++++-- diff --git a/client.go b/client.go index a3f0a78a8a1e650c8299de0d3c5a412b3896e534..3832fdee35716c13c768a37d84f511780072da38 100644 --- a/client.go +++ b/client.go @@ -354,9 +354,7 @@ }(), StartingNodes: cl.config.DhtStartingNodes(conn.LocalAddr().Network()), ConnectionTracking: cl.config.ConnTracker, OnQuery: cl.config.DHTOnQuery, - Logger: cl.logger.WithText(func(m log.Msg) string { - return fmt.Sprintf("dht server on %v: %s", conn.LocalAddr().String(), m.Text()) - }), + Logger: cl.logger.WithContextText(fmt.Sprintf("dht server on %v", conn.LocalAddr().String())), } s, err = dht.NewServer(&cfg) if err == nil { @@ -1096,9 +1094,7 @@ webSeeds: make(map[string]*peer), } t._pendingPieces.NewSet = priorityBitmapStableNewSet t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu) - t.logger = cl.logger.WithValues(t).WithText(func(m log.Msg) string { - return fmt.Sprintf("%v: %s", t, m.Text()) - }) + t.logger = cl.logger.WithContextValue(t) t.setChunkSize(defaultChunkSize) return } @@ -1348,9 +1344,7 @@ conn: nc, writeBuffer: new(bytes.Buffer), } c.peerImpl = c - c.logger = cl.logger.WithValues(c).WithDefaultLevel(log.Debug).WithText(func(m log.Msg) string { - return fmt.Sprintf("%v: %s", c, m.Text()) - }) + c.logger = cl.logger.WithDefaultLevel(log.Debug).WithContextValue(c) c.writerCond.L = cl.locker() c.setRW(connStatsReadWriter{nc, c}) c.r = &rateLimitedReader{ diff --git a/go.mod b/go.mod index d8298624e4d9dc976df91f7d697812685d4120c2..a56d4113d769f3a5832c536a857b2797fa338ec5 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ github.com/alexflint/go-arg v1.3.0 github.com/anacrolix/dht/v2 v2.6.1-0.20200416071723-3850fa1b802a github.com/anacrolix/envpprof v1.1.0 github.com/anacrolix/go-libutp v1.0.2 - github.com/anacrolix/log v0.7.0 + github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d github.com/anacrolix/missinggo v1.2.1 github.com/anacrolix/missinggo/perf v1.0.0 github.com/anacrolix/missinggo/v2 v2.4.1-0.20200419051441-747d9d7544c6 diff --git a/go.sum b/go.sum index c6a1882b5d5af90177fce3f59f8482509c648eac..b74356cd51934392569bbe1b889bf89c3e869f66 100644 --- a/go.sum +++ b/go.sum @@ -76,6 +76,8 @@ github.com/anacrolix/log v0.6.1-0.20200416071330-f58a030e6149 h1:3cEyLU9ObAfTnBHCev8uuWGhbHfol8uTwyMRkLIpZGg= github.com/anacrolix/log v0.6.1-0.20200416071330-f58a030e6149/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= github.com/anacrolix/log v0.7.0 h1:koGkC/K0LjIbrhLhwfpsfMuvu8nhvY7J4TmLVc1mAwE= github.com/anacrolix/log v0.7.0/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= +github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d h1:AkY1QtaxDBPLr1hrxLD8tUL04EPI0asYc2VB8v7Bg2U= +github.com/anacrolix/log v0.7.1-0.20200604014615-c244de44fd2d/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI= github.com/anacrolix/missinggo v0.0.0-20180522035225-b4a5853e62ff/go.mod h1:b0p+7cn+rWMIphK1gDH2hrDuwGOcbB6V4VXeSsEfHVk= github.com/anacrolix/missinggo v0.0.0-20180725070939-60ef2fbf63df/go.mod h1:kwGiTUTZ0+p4vAz3VbAI5a30t2YbvemcmspjKwrAz5s= github.com/anacrolix/missinggo v0.2.1-0.20190310234110-9fbdc9f242a8 h1:E2Xb2SBsVzHJ1tNMW9QcckYEQcyBKz1ee8qVjeVRWys= diff --git a/peer-impl.go b/peer-impl.go index 41540c4704f3cfaa9879a1ff00c8a9553d599c6e..2b65d1ad94c757334d2f7e4d25898d19e4e9c3f2 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -18,4 +18,5 @@ _close() _postCancel(request) onGotInfo(*metainfo.Info) drop() + String() string } diff --git a/torrent.go b/torrent.go index bd4a025e9fa814319414c5907b1cffd0b2cedd24..749a5dae7d48d0238a45e7c5219958c385cb54b4 100644 --- a/torrent.go +++ b/torrent.go @@ -2049,6 +2049,7 @@ Url: url, }, requests: make(map[request]webseed.Request, maxRequests), } + ws.peer.logger = t.logger.WithContextValue(&ws) ws.peer.peerImpl = &ws if t.haveInfo() { ws.onGotInfo(t.info) diff --git a/web_seed.go b/web_seed.go index 72bda5fde0bdf755b0a175ea37ea90f7bf4bfba2..c2bb78e1bddb1a0285d59ee8a39ed147fd4b9224 100644 --- a/web_seed.go +++ b/web_seed.go @@ -1,9 +1,9 @@ package torrent import ( + "fmt" "net/http" - "github.com/anacrolix/log" "github.com/anacrolix/torrent/common" "github.com/anacrolix/torrent/metainfo" pp "github.com/anacrolix/torrent/peer_protocol" @@ -33,6 +33,10 @@ peer peer } var _ peerImpl = (*webSeed)(nil) + +func (ws *webSeed) String() string { + return fmt.Sprintf("webseed peer for %q", ws.client.Url) +} func (ws *webSeed) onGotInfo(info *metainfo.Info) { ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles())) @@ -81,7 +85,7 @@ result := <-webseedRequest.Result ws.peer.t.cl.lock() defer ws.peer.t.cl.unlock() if result.Err != nil { - log.Printf("webseed request rejected: %v", result.Err) + ws.peer.logger.Printf("request %v rejected: %v", r, result.Err) ws.peer.remoteRejectedRequest(r) } else { err := ws.peer.receiveChunk(&pp.Message{