Skip to content

Commit

Permalink
fix(log): Fix log lines being overwritten by progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 24, 2023
1 parent f0a1c1a commit 7965d8b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
5 changes: 1 addition & 4 deletions internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ func (action Dump) Run(ctx context.Context) (err error) {

startTime := time.Now()

bar := progressbar.New(-1, "downloading", action.Spinner)
bar, plogger := progressbar.New(os.Stderr, -1, "downloading", action.Spinner)
defer bar.Close()
plogger := progressbar.NewBarSafeLogger(os.Stderr, bar)
log.SetOutput(plogger)

errGroup, ctx := errgroup.WithContext(ctx)

Expand Down Expand Up @@ -142,7 +140,6 @@ func (action Dump) Run(ctx context.Context) (err error) {
}

_ = bar.Finish()
log.SetOutput(os.Stderr)

// Close file
err = f.Close()
Expand Down
10 changes: 3 additions & 7 deletions internal/actions/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ func (action Restore) Run(ctx context.Context) (err error) {

startTime := time.Now()

bar := progressbar.New(-1, "uploading", action.Spinner)
bar, errLog := progressbar.New(os.Stderr, -1, "uploading", action.Spinner)
defer bar.Close()
errLog := progressbar.NewBarSafeLogger(os.Stderr, bar)
outLog := progressbar.NewBarSafeLogger(os.Stdout, bar)
log.SetOutput(errLog)

errGroup, ctx := errgroup.WithContext(ctx)

Expand All @@ -59,7 +56,7 @@ func (action Restore) Run(ctx context.Context) (err error) {
defer func(pr io.ReadCloser) {
_ = pr.Close()
}(pr)
return action.runInDatabasePod(ctx, pr, outLog, errLog, action.Format)
return action.runInDatabasePod(ctx, pr, errLog, errLog, action.Format)
})

errGroup.Go(func() error {
Expand Down Expand Up @@ -131,7 +128,7 @@ func (action Restore) Run(ctx context.Context) (err error) {
defer func(pr io.ReadCloser) {
_ = pr.Close()
}(pr)
return action.runInDatabasePod(ctx, pr, outLog, errLog, sqlformat.Gzip)
return action.runInDatabasePod(ctx, pr, errLog, errLog, sqlformat.Gzip)
})
}

Expand All @@ -158,7 +155,6 @@ func (action Restore) Run(ctx context.Context) (err error) {
}

_ = bar.Finish()
log.SetOutput(os.Stderr)

log.WithFields(log.Fields{
"file": action.Filename,
Expand Down
20 changes: 14 additions & 6 deletions internal/progressbar/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func NewBarSafeLogger(w io.Writer, bar *ProgressBar) *BarSafeLogger {
}

type BarSafeLogger struct {
out io.Writer
bar *ProgressBar
out io.Writer
bar *ProgressBar
atStart bool
}

func (l *BarSafeLogger) Write(p []byte) (int, error) {
Expand All @@ -24,17 +25,24 @@ func (l *BarSafeLogger) Write(p []byte) (int, error) {
l.bar.mu.Lock()
defer l.bar.mu.Unlock()

if _, err := l.out.Write([]byte("\r\x1B[K")); err != nil {
return 0, err
if !l.atStart {
if _, err := l.out.Write([]byte("\r\x1B[K")); err != nil {
return 0, err
}
}

n, err := l.out.Write(p)
if err != nil {
return n, err
}

if _, err := l.out.Write([]byte(l.bar.String())); err != nil {
return n, err
if p[len(p)-1] == '\n' {
if _, err := l.out.Write([]byte(l.bar.String())); err != nil {
return n, err
}
l.atStart = false
} else {
l.atStart = true
}

return n, nil
Expand Down
14 changes: 10 additions & 4 deletions internal/progressbar/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
log "github.com/sirupsen/logrus"
)

func New(max int64, label string, spinnerKey string) *ProgressBar {
func New(w io.Writer, max int64, label string, spinnerKey string) (*ProgressBar, *BarSafeLogger) {
s, ok := spinner.Map[spinnerKey]
if !ok {
log.WithField("spinner", spinnerKey).Warn("invalid spinner")
Expand Down Expand Up @@ -61,7 +61,7 @@ func New(max int64, label string, spinnerKey string) *ProgressBar {
if bar.IsFinished() {
return
}
if bar.mu.TryLock() {
if !bar.logger.atStart && bar.mu.TryLock() {
_ = bar.RenderBlank()
_, _ = os.Stderr.Write([]byte(bar.String()))
bar.mu.Unlock()
Expand All @@ -70,18 +70,24 @@ func New(max int64, label string, spinnerKey string) *ProgressBar {
}
}()

return bar
logger := NewBarSafeLogger(w, bar)
log.SetOutput(logger)
return bar, logger
}

type ProgressBar struct {
*progressbar.ProgressBar
mu sync.Mutex
cancelChan chan struct{}
cancelOnce sync.Once
logger BarSafeLogger
}

func (p *ProgressBar) Finish() error {
p.Close()
defer func() {
p.Close()
log.SetOutput(os.Stderr)
}()
return p.ProgressBar.Finish()
}

Expand Down

0 comments on commit 7965d8b

Please sign in to comment.