Skip to content

Commit

Permalink
Direct log output to stderr and hide progress bar when redirected (#74)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
silkeh authored Feb 28, 2024
1 parent 21f1bd7 commit 51a0ee5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
33 changes: 25 additions & 8 deletions builder/source/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))
}
Expand Down

0 comments on commit 51a0ee5

Please sign in to comment.