Lots of improvements to logging

This commit is contained in:
Matt Joiner 2019-08-22 13:59:04 +10:00
parent 2a5488f723
commit eb4dad73ae
5 changed files with 62 additions and 37 deletions

View File

@ -324,7 +324,7 @@ func (cl *Client) newDhtServer(conn net.PacketConn) (s *dht.Server, err error) {
if err != nil {
cl.logger.Printf("error bootstrapping dht: %s", err)
}
log.Str("completed bootstrap").AddValues(s, ts).Log(cl.logger)
log.Fstr("%v: completed bootstrap", s).AddValues(s, ts).Log(cl.logger)
}()
}
return

View File

@ -4,7 +4,6 @@ package main
import (
"expvar"
"fmt"
"log"
"net"
"net/http"
"os"
@ -13,6 +12,10 @@ import (
"syscall"
"time"
"golang.org/x/xerrors"
"github.com/anacrolix/log"
"github.com/anacrolix/envpprof"
"github.com/anacrolix/tagflag"
humanize "github.com/dustin/go-humanize"
@ -63,48 +66,47 @@ func torrentBar(t *torrent.Torrent) {
}()
}
func addTorrents(client *torrent.Client) {
func addTorrents(client *torrent.Client) error {
for _, arg := range flags.Torrent {
t := func() *torrent.Torrent {
t, err := func() (*torrent.Torrent, error) {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
return nil, xerrors.Errorf("error adding magnet: %w", err)
}
return t
return t, nil
} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
response, err := http.Get(arg)
if err != nil {
log.Fatalf("Error downloading torrent file: %s", err)
return nil, xerrors.Errorf("Error downloading torrent file: %s", err)
}
metaInfo, err := metainfo.Load(response.Body)
defer response.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
return nil, xerrors.Errorf("adding torrent: %w", err)
}
return t
return t, nil
} else if strings.HasPrefix(arg, "infohash:") {
t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
return t
return t, nil
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
return nil, xerrors.Errorf("adding torrent: %w", err)
return t, nil
}
}()
if err != nil {
return xerrors.Errorf("adding torrent for %q: %w", arg, err)
}
torrentBar(t)
t.AddPeers(func() (ret []torrent.Peer) {
for _, ta := range flags.TestPeer {
@ -120,6 +122,7 @@ func addTorrents(client *torrent.Client) {
t.DownloadAll()
}()
}
return nil
}
var flags = struct {
@ -134,6 +137,7 @@ var flags = struct {
Stats *bool
PublicIP net.IP
Progress bool
Quiet bool `help:"discard client logging"`
tagflag.StartPos
Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
}{
@ -165,7 +169,13 @@ func exitSignalHandlers(client *torrent.Client) {
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
if err := mainErr(); err != nil {
log.Printf("error in main: %v", err)
os.Exit(1)
}
}
func mainErr() error {
tagflag.Parse(&flags)
defer envpprof.Stop()
clientConfig := torrent.NewDefaultClientConfig()
@ -176,7 +186,7 @@ func main() {
if flags.PackedBlocklist != "" {
blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
if err != nil {
log.Fatalf("error loading blocklist: %s", err)
return xerrors.Errorf("loading blocklist: %v", err)
}
defer blocklist.Close()
clientConfig.IPBlocklist = blocklist
@ -193,10 +203,13 @@ func main() {
if flags.DownloadRate != -1 {
clientConfig.DownloadRateLimiter = rate.NewLimiter(rate.Limit(flags.DownloadRate), 1<<20)
}
if flags.Quiet {
clientConfig.Logger = log.Discard
}
client, err := torrent.NewClient(clientConfig)
if err != nil {
log.Fatalf("error creating client: %s", err)
return xerrors.Errorf("creating client: %v", err)
}
defer client.Close()
go exitSignalHandlers(client)
@ -207,7 +220,7 @@ func main() {
client.WriteStatus(w)
})
if stdoutAndStderrAreSameFile() {
log.SetOutput(progress.Bypass())
log.Default = log.Logger{log.StreamLogger{W: progress.Bypass(), Fmt: log.LineFormatter}}
}
if flags.Progress {
progress.Start()
@ -216,13 +229,14 @@ func main() {
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
log.Fatal("y u no complete torrents?!")
return xerrors.New("y u no complete torrents?!")
}
if flags.Seed {
outputStats(client)
select {}
}
outputStats(client)
return nil
}
func outputStats(cl *torrent.Client) {

10
go.mod
View File

@ -5,21 +5,21 @@ require (
github.com/RoaringBitmap/roaring v0.4.18 // indirect
github.com/alexflint/go-arg v1.1.0
github.com/anacrolix/dht/v2 v2.0.1
github.com/anacrolix/envpprof v1.0.0
github.com/anacrolix/envpprof v1.0.1
github.com/anacrolix/go-libutp v1.0.2
github.com/anacrolix/log v0.2.2-0.20190821105323-e3efcaf1a0fd
github.com/anacrolix/log v0.3.0
github.com/anacrolix/missinggo v1.2.1
github.com/anacrolix/missinggo/perf v1.0.0
github.com/anacrolix/mmsg v1.0.0 // indirect
github.com/anacrolix/sync v0.0.0-20180808010631-44578de4e778
github.com/anacrolix/tagflag v0.0.0-20180803105420-3a8ff5428f76
github.com/anacrolix/upnp v0.1.1
github.com/anacrolix/utp v0.0.0-20180219060659-9e0e1d1d0572
github.com/boltdb/bolt v1.3.1
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.0
github.com/edsrzf/mmap-go v1.0.0
github.com/elgatito/upnp v0.0.0-20180711183757-2f244d205f9a
github.com/fsnotify/fsnotify v1.4.7
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e // indirect
github.com/google/btree v1.0.0
@ -32,7 +32,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
@ -40,5 +40,3 @@ require (
)
go 1.13
replace github.com/elgatito/upnp => github.com/anacrolix/upnp v0.0.0-20190717072655-8249d7a81c03

25
go.sum
View File

@ -16,13 +16,15 @@ github.com/anacrolix/envpprof v0.0.0-20180404065416-323002cec2fa h1:xCaATLKmn39Q
github.com/anacrolix/envpprof v0.0.0-20180404065416-323002cec2fa/go.mod h1:KgHhUaQMc8cC0+cEflSgCFNFbKwi5h54gqtVn8yhP7c=
github.com/anacrolix/envpprof v1.0.0 h1:AwZ+mBP4rQ5f7JSsrsN3h7M2xDW/xSE66IPVOqlnuUc=
github.com/anacrolix/envpprof v1.0.0/go.mod h1:KgHhUaQMc8cC0+cEflSgCFNFbKwi5h54gqtVn8yhP7c=
github.com/anacrolix/envpprof v1.0.1 h1:lShFeOuHFuzLAfyP6WplWvIfHKmbKu1u9/rDOtcFGX4=
github.com/anacrolix/envpprof v1.0.1/go.mod h1:My7T5oSqVfEn4MD4Meczkw/f5lSIndGAKu/0SM/rkf4=
github.com/anacrolix/go-libutp v0.0.0-20180522111405-6baeb806518d/go.mod h1:beQSaSxwH2d9Eeu5ijrEnHei5Qhk+J6cDm1QkWFru4E=
github.com/anacrolix/go-libutp v1.0.2 h1:cL2SfTCO418V+DQRdMEW+RNfO2InLqW6PsSLqHwmGR4=
github.com/anacrolix/go-libutp v1.0.2/go.mod h1:uIH0A72V++j0D1nnmTjjZUiH/ujPkFxYWkxQ02+7S0U=
github.com/anacrolix/log v0.0.0-20180412014343-2323884b361d h1:G8ITVMWuQL4adKRC3A6aBOo0YFJYcmpS3JFQd+rZrn0=
github.com/anacrolix/log v0.0.0-20180412014343-2323884b361d/go.mod h1:sf/7c2aTldL6sRQj/4UKyjgVZBu2+M2z9wf7MmwPiew=
github.com/anacrolix/log v0.2.2-0.20190821103111-726085c46ea1 h1:aaaFTLBK8GqKjl/3NAxmK3YPTl5zN65uiJzmf1bQT2g=
github.com/anacrolix/log v0.2.2-0.20190821103111-726085c46ea1/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgwU9jwU=
github.com/anacrolix/log v0.3.0 h1:Btxh7GkT4JYWvWJ1uKOwgobf+7q/1eFQaDdCUXCtssw=
github.com/anacrolix/log v0.3.0/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgwU9jwU=
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=
@ -47,8 +49,8 @@ github.com/anacrolix/tagflag v0.0.0-20180605133421-f477c8c2f14c/go.mod h1:1m2U/K
github.com/anacrolix/tagflag v0.0.0-20180803105420-3a8ff5428f76 h1:No2E3UEtiEcvy5juw8FNCrtjWUm+Rq9KB0cMcwMDNnc=
github.com/anacrolix/tagflag v0.0.0-20180803105420-3a8ff5428f76/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw=
github.com/anacrolix/torrent v0.0.0-20180622074351-fefeef4ee9eb/go.mod h1:3vcFVxgOASslNXHdivT8spyMRBanMCenHRpe0u5vpBs=
github.com/anacrolix/upnp v0.0.0-20190717072655-8249d7a81c03 h1:moiTMOh4ZQ8yELlyjgVY1dQF2PlrP2NQr2/qFD1eB1w=
github.com/anacrolix/upnp v0.0.0-20190717072655-8249d7a81c03/go.mod h1:ufVftEeVgFwZQy/aZMVfogCLY/2z19vR7oI+dA3XBhg=
github.com/anacrolix/upnp v0.1.1 h1:v5C+wBiku2zmwFR5B+pUfdNBL5TfPtyO+sWuw+/VEDg=
github.com/anacrolix/upnp v0.1.1/go.mod h1:LXsbsp5h+WGN7YR+0A7iVXm5BL1LYryDev1zuJMWYQo=
github.com/anacrolix/utp v0.0.0-20180219060659-9e0e1d1d0572 h1:kpt6TQTVi6gognY+svubHfxxpq0DLU9AfTQyZVc3UOc=
github.com/anacrolix/utp v0.0.0-20180219060659-9e0e1d1d0572/go.mod h1:MDwc+vsGEq7RMw6lr2GKOEqjWny5hO5OZXRVNaBJ2Dk=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
@ -110,6 +112,11 @@ github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpR
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-sqlite3 v1.7.0 h1:CiYZ8slwBLIMkDbDJCF+Zd2M8bZ1Gz02TMsm1V33Lk0=
@ -118,8 +125,6 @@ github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK86
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae h1:VeRdUYdCw49yizlSbMEn2SZ+gT+3IUKx8BqxyQdz+BY=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
@ -146,6 +151,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/syncthing/syncthing v0.14.48-rc.4/go.mod h1:nw3siZwHPA6M8iSfjDCWQ402eqvEIasMQOE8nFOxy7M=
github.com/tinylib/msgp v1.0.2 h1:DfdQrzQa7Yh2es9SuLkixqxuXS2SxsdYn0KbdrOGWD8=
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
@ -161,6 +168,7 @@ github.com/willf/bloom v0.0.0-20170505221640-54e3b963ee16 h1:hDGatoumfYOxzIZGsSy
github.com/willf/bloom v0.0.0-20170505221640-54e3b963ee16/go.mod h1:MmAltL9pDMNTrvUkxdg0k0q5I0suxmuwp3KbyrZLOZ8=
github.com/willf/bloom v2.0.3+incompatible h1:QDacWdqcAUI1MPOwIQZRy9kOR7yxfyEmxX8Wdm2/JPA=
github.com/willf/bloom v2.0.3+incompatible/go.mod h1:MmAltL9pDMNTrvUkxdg0k0q5I0suxmuwp3KbyrZLOZ8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180524181706-dfa909b99c79/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190318221613-d196dffd7c2b h1:ZWpVMTsK0ey5WJCu+vVdfMldWq7/ezaOcjnKWIHWVkE=
@ -178,3 +186,8 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZe
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -4,7 +4,7 @@ import (
"time"
"github.com/anacrolix/log"
"github.com/elgatito/upnp"
"github.com/anacrolix/upnp"
)
func addPortMapping(d upnp.Device, proto upnp.Protocol, internalPort int, debug bool) {
@ -25,7 +25,7 @@ func (cl *Client) forwardPort() {
return
}
cl.unlock()
ds := upnp.Discover(0, 2*time.Second)
ds := upnp.Discover(0, 2*time.Second, cl.logger)
cl.lock()
cl.logger.Printf("discovered %d upnp devices", len(ds))
port := cl.incomingPeerPort()