From 37fc800cb1036a108fd840ba4d8f86b1a43a7cfb Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 6 Jun 2018 00:40:03 +1000 Subject: [PATCH] cmd/torrent: Add -stats flag Requires updated tagflag package. --- cmd/torrent/main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 2421412b..1d9b4944 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -130,6 +130,7 @@ var flags = struct { DownloadRate tagflag.Bytes `help:"max bytes per second down from peers"` Debug bool PackedBlocklist string + Stats *bool tagflag.StartPos Torrent []string `arity:"+" help:"torrent file path or magnet uri"` }{ @@ -143,9 +144,17 @@ func stdoutAndStderrAreSameFile() bool { return os.SameFile(fi1, fi2) } +func statsEnabled() bool { + if flags.Stats == nil { + return flags.Debug + } + return *flags.Stats +} + func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) tagflag.Parse(&flags) + defer envpprof.Stop() clientConfig := torrent.Config{ Debug: flags.Debug, Seed: flags.Seed, @@ -193,10 +202,18 @@ func main() { log.Fatal("y u no complete torrents?!") } if flags.Seed { + outputStats(client) select {} } + outputStats(client) +} + +func outputStats(cl *torrent.Client) { + if !statsEnabled() { + return + } expvar.Do(func(kv expvar.KeyValue) { fmt.Printf("%s: %s\n", kv.Key, kv.Value) }) - envpprof.Stop() + cl.WriteStatus(os.Stdout) } -- 2.48.1