From 51a0ee5e9ecdaf56688bb661544bbbc72137552a Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Wed, 28 Feb 2024 18:02:36 +0100 Subject: [PATCH] Direct log output to stderr and hide progress bar when redirected (#74) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After this change it is possible to differentiate between the log output from solbuild itself, and the output from commands executed by solbuild. This allows one to check the build result on the terminal and keep the redirected stdout for checking the reason for the build result. This is a follow-up of #54/#63. Additionally, the progress bar is replaced by a log statement when redirected. For example: ``` $ sudo solbuild build package.yml -p unstable-x86_64 > /tmp/output.log ✓ > Downloading source uri=https://www.nano-editor.org/dist/v7/nano-7.2.tar.xz ✓ > Now starting build package=nano ✓ > Building succeeded ``` --- builder/source/simple.go | 33 +++++++++++++++++++++++++-------- cli/log/log.go | 6 +++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/builder/source/simple.go b/builder/source/simple.go index 0789a31..181eb47 100644 --- a/builder/source/simple.go +++ b/builder/source/simple.go @@ -190,13 +190,35 @@ func (s *SimpleSource) download(destination string) error { }, }, } - resp := client.Do(req) - // Setup our progress bar + // Show our progress bar + s.showProgress(resp) + + if err := resp.Err(); err != nil { + slog.Error("Error downloading", "uri", s.URI, "err", err) + + return err + } + + return nil +} + +func onTTY() bool { + s, _ := os.Stdout.Stat() + + return s.Mode()&os.ModeCharDevice > 0 +} + +func (s *SimpleSource) showProgress(resp *grab.Response) { + if !onTTY() { + slog.Info("Downloading source", "uri", s.URI) + } + pbar := pb.Start64(resp.Size()) pbar.Set(pb.Bytes, true) pbar.SetTemplateString(progressBarTemplate) + pbar.SetWriter(os.Stdout) defer pbar.Finish() @@ -212,12 +234,7 @@ func (s *SimpleSource) download(destination string) error { // Ensure progressbar completes to 100% pbar.SetCurrent(resp.BytesComplete()) - if err := resp.Err(); err != nil { - slog.Error("Error downloading", "uri", s.URI, "err", err) - return err - } - - return nil + return } } } diff --git a/cli/log/log.go b/cli/log/log.go index 955e64b..6a41755 100644 --- a/cli/log/log.go +++ b/cli/log/log.go @@ -38,20 +38,20 @@ func setLogger(h slog.Handler) { } func onTTY() bool { - s, _ := os.Stdout.Stat() + s, _ := os.Stderr.Stat() return s.Mode()&os.ModeCharDevice > 0 } func SetColoredLogger() { - setLogger(powerline.NewHandler(os.Stdout, &powerline.HandlerOptions{ + setLogger(powerline.NewHandler(os.Stderr, &powerline.HandlerOptions{ Level: &Level, Colors: colors, })) } func SetUncoloredLogger() { - setLogger(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + setLogger(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: &Level, })) }