lib: Print nicely rounded durations (#6756)

This commit is contained in:
Simon Frei 2020-06-18 10:55:41 +02:00 committed by GitHub
parent 4812fd3ec1
commit 8cf9d91ed4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -827,7 +827,7 @@ func (db *Lowlevel) loadMetadataTracker(folder string) *metadataTracker {
}
if age := time.Since(meta.Created()); age > db.recheckInterval {
l.Infof("Stored folder metadata for %q is %v old; recalculating", folder, age)
l.Infof("Stored folder metadata for %q is %v old; recalculating", folder, util.NiceDurationString(age))
return db.getMetaAndCheck(folder)
}

View File

@ -28,6 +28,7 @@ import (
"github.com/syncthing/syncthing/lib/scanner"
"github.com/syncthing/syncthing/lib/stats"
"github.com/syncthing/syncthing/lib/sync"
"github.com/syncthing/syncthing/lib/util"
"github.com/syncthing/syncthing/lib/watchaggregator"
"github.com/thejerf/suture"
@ -330,7 +331,7 @@ func (f *folder) pull() (success bool) {
// Pulling failed, try again later.
delay := f.pullPause + time.Since(startTime)
l.Infof("Folder %v isn't making sync progress - retrying in %v.", f.Description(), delay.Truncate(time.Second))
l.Infof("Folder %v isn't making sync progress - retrying in %v.", f.Description(), util.NiceDurationString(delay))
f.pullFailTimer.Reset(delay)
return false
}

View File

@ -14,6 +14,7 @@ import (
"reflect"
"strconv"
"strings"
"time"
"github.com/syncthing/syncthing/lib/sync"
@ -314,3 +315,19 @@ func CallWithContext(ctx context.Context, fn func() error) error {
return ctx.Err()
}
}
func NiceDurationString(d time.Duration) string {
switch {
case d > 24*time.Hour:
d = d.Round(time.Hour)
case d > time.Hour:
d = d.Round(time.Minute)
case d > time.Minute:
d = d.Round(time.Second)
case d > time.Second:
d = d.Round(time.Millisecond)
case d > time.Millisecond:
d = d.Round(time.Microsecond)
}
return d.String()
}