From 53e32ca9ddfca37c5f25928302088ba92e713186 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 28 Jan 2018 16:07:11 +1100 Subject: [PATCH] Start using new log package --- client.go | 4 +++- client_test.go | 15 ++++++--------- torrent.go | 6 ++++-- torrent_test.go | 9 +++------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/client.go b/client.go index 95d348f8..689386cd 100644 --- a/client.go +++ b/client.go @@ -8,13 +8,14 @@ import ( "expvar" "fmt" "io" - "log" "net" "net/url" "strconv" "strings" "time" + "github.com/anacrolix/log" + "github.com/anacrolix/dht" "github.com/anacrolix/dht/krpc" "github.com/anacrolix/missinggo" @@ -1040,6 +1041,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) ( L: &cl.mu, }, } + t.logger = log.Default.Clone().AddValue(t) t.setChunkSize(defaultChunkSize) return } diff --git a/client_test.go b/client_test.go index 56390b3d..a16fdbf0 100644 --- a/client_test.go +++ b/client_test.go @@ -18,7 +18,6 @@ import ( _ "github.com/anacrolix/envpprof" "github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo/filecache" - "github.com/anacrolix/missinggo/pubsub" "github.com/bradfitz/iter" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -100,14 +99,12 @@ func TestPieceHashSize(t *testing.T) { func TestTorrentInitialState(t *testing.T) { dir, mi := testutil.GreetingTestTorrent() defer os.RemoveAll(dir) - tor := &Torrent{ - infoHash: mi.HashInfoBytes(), - pieceStateChanges: pubsub.NewPubSub(), - } - tor.chunkSize = 2 - tor.storageOpener = storage.NewClient(storage.NewFileWithCompletion(tempDir(), storage.NewMapPieceCompletion())) - // Needed to lock for asynchronous piece verification. - tor.cl = new(Client) + cl := &Client{} + tor := cl.newTorrent( + mi.HashInfoBytes(), + storage.NewFileWithCompletion(tempDir(), storage.NewMapPieceCompletion()), + ) + tor.setChunkSize(2) tor.cl.mu.Lock() err := tor.setInfoBytes(mi.InfoBytes) tor.cl.mu.Unlock() diff --git a/torrent.go b/torrent.go index f342de22..a215859d 100644 --- a/torrent.go +++ b/torrent.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "log" "math" "math/rand" "net" @@ -16,6 +15,8 @@ import ( "text/tabwriter" "time" + "github.com/anacrolix/log" + "github.com/davecgh/go-spew/spew" "github.com/anacrolix/dht" @@ -45,7 +46,8 @@ type peersKey struct { // Maintains state of torrent within a Client. type Torrent struct { - cl *Client + cl *Client + logger *log.Logger networkingEnabled bool // Determines what chunks to request from peers. 1: Favour higher priority diff --git a/torrent_test.go b/torrent_test.go index 3889aff2..c407af0b 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -146,12 +146,9 @@ func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) { func TestPieceHashFailed(t *testing.T) { mi := testutil.GreetingMetaInfo() - tt := Torrent{ - cl: new(Client), - infoHash: mi.HashInfoBytes(), - storageOpener: storage.NewClient(badStorage{}), - chunkSize: 2, - } + cl := new(Client) + tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{}) + tt.setChunkSize(2) require.NoError(t, tt.setInfoBytes(mi.InfoBytes)) tt.cl.mu.Lock() tt.pieces[1].dirtyChunks.AddRange(0, 3)