From: Matt Joiner Date: Thu, 25 Jan 2018 02:12:46 +0000 (+1100) Subject: cmd/torrent: Fix logging when progress bar and stderr are the same file X-Git-Tag: v1.0.0~273 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=358c6c8649670366904736fbbe55eef084eb83eb;p=btrtrc.git cmd/torrent: Fix logging when progress bar and stderr are the same file --- diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 0434c2ab..30490132 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -23,8 +23,10 @@ import ( "github.com/anacrolix/torrent/storage" ) +var progress = uiprogress.New() + func torrentBar(t *torrent.Torrent) { - bar := uiprogress.AddBar(1) + bar := progress.AddBar(1) bar.AppendCompleted() bar.AppendFunc(func(*uiprogress.Bar) (ret string) { select { @@ -133,6 +135,12 @@ var flags = struct { DownloadRate: -1, } +func stdoutAndStderrAreSameFile() bool { + fi1, _ := os.Stdout.Stat() + fi2, _ := os.Stderr.Stat() + return os.SameFile(fi1, fi2) +} + func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) tagflag.Parse(&flags) @@ -167,7 +175,10 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { client.WriteStatus(w) }) - uiprogress.Start() + if stdoutAndStderrAreSameFile() { + log.SetOutput(progress.Bypass()) + } + progress.Start() addTorrents(client) if client.WaitAll() { log.Print("downloaded ALL the torrents")