Skip to content

Commit 51a0ee5

Browse files
authored
Direct log output to stderr and hide progress bar when redirected (#74)
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 ```
1 parent 21f1bd7 commit 51a0ee5

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

builder/source/simple.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,35 @@ func (s *SimpleSource) download(destination string) error {
190190
},
191191
},
192192
}
193-
194193
resp := client.Do(req)
195194

196-
// Setup our progress bar
195+
// Show our progress bar
196+
s.showProgress(resp)
197+
198+
if err := resp.Err(); err != nil {
199+
slog.Error("Error downloading", "uri", s.URI, "err", err)
200+
201+
return err
202+
}
203+
204+
return nil
205+
}
206+
207+
func onTTY() bool {
208+
s, _ := os.Stdout.Stat()
209+
210+
return s.Mode()&os.ModeCharDevice > 0
211+
}
212+
213+
func (s *SimpleSource) showProgress(resp *grab.Response) {
214+
if !onTTY() {
215+
slog.Info("Downloading source", "uri", s.URI)
216+
}
217+
197218
pbar := pb.Start64(resp.Size())
198219
pbar.Set(pb.Bytes, true)
199220
pbar.SetTemplateString(progressBarTemplate)
221+
pbar.SetWriter(os.Stdout)
200222

201223
defer pbar.Finish()
202224

@@ -212,12 +234,7 @@ func (s *SimpleSource) download(destination string) error {
212234
// Ensure progressbar completes to 100%
213235
pbar.SetCurrent(resp.BytesComplete())
214236

215-
if err := resp.Err(); err != nil {
216-
slog.Error("Error downloading", "uri", s.URI, "err", err)
217-
return err
218-
}
219-
220-
return nil
237+
return
221238
}
222239
}
223240
}

cli/log/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func setLogger(h slog.Handler) {
3838
}
3939

4040
func onTTY() bool {
41-
s, _ := os.Stdout.Stat()
41+
s, _ := os.Stderr.Stat()
4242

4343
return s.Mode()&os.ModeCharDevice > 0
4444
}
4545

4646
func SetColoredLogger() {
47-
setLogger(powerline.NewHandler(os.Stdout, &powerline.HandlerOptions{
47+
setLogger(powerline.NewHandler(os.Stderr, &powerline.HandlerOptions{
4848
Level: &Level,
4949
Colors: colors,
5050
}))
5151
}
5252

5353
func SetUncoloredLogger() {
54-
setLogger(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
54+
setLogger(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
5555
Level: &Level,
5656
}))
5757
}

0 commit comments

Comments
 (0)