mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +00:00
TrimSuffix(s, "0s") was meant to turn "4h0m0s" into "4h", but it strips any trailing "0s"/"0m" -- so 10s logged as "1", 20s as "2", 1m30s as "1m3", 2h30m as "2h3", and a zero duration as the empty string. Every elapsed/duration field in the app was affected. The suffix now has to include the preceding unit, so only a whole zero-valued component is dropped. The existing table only covered values that dodge the bug (4m, 4h, 4m3s); added the ones that don't.
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"iter"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
)
|
|
|
|
func ShortDur(d time.Duration) string {
|
|
var s string
|
|
switch {
|
|
case d > time.Hour:
|
|
s = d.Round(time.Minute).String()
|
|
case d > time.Minute:
|
|
s = d.Round(time.Second).String()
|
|
case d > time.Second:
|
|
s = d.Round(10 * time.Millisecond).String()
|
|
case d > time.Millisecond:
|
|
s = d.Round(100 * time.Microsecond).String()
|
|
default:
|
|
s = d.String()
|
|
}
|
|
// Drop whole zero-valued trailing components ("4h0m0s" -> "4h"). The suffix has to include
|
|
// the preceding unit, or a value that merely ends in a zero digit loses it: "10s" -> "1".
|
|
if strings.HasSuffix(s, "m0s") {
|
|
s = strings.TrimSuffix(s, "0s")
|
|
}
|
|
if strings.HasSuffix(s, "h0m") {
|
|
s = strings.TrimSuffix(s, "0m")
|
|
}
|
|
return s
|
|
}
|
|
|
|
func StringerValue(s fmt.Stringer) string {
|
|
v := reflect.ValueOf(s)
|
|
if v.Kind() == reflect.Pointer && v.IsNil() {
|
|
return "nil"
|
|
}
|
|
return s.String()
|
|
}
|
|
|
|
func formatSeq[T any](v iter.Seq[T]) string {
|
|
return formatSlice(slices.Collect(v))
|
|
}
|
|
|
|
func formatSlice[T any](v []T) string {
|
|
s := slice.Map(v, func(x T) string { return fmt.Sprintf("%v", x) })
|
|
return fmt.Sprintf("[`%s`]", strings.Join(s, "`,`"))
|
|
}
|
|
|
|
func CRLFWriter(w io.Writer) io.Writer {
|
|
return &crlfWriter{w: w}
|
|
}
|
|
|
|
type crlfWriter struct {
|
|
w io.Writer
|
|
lastByte byte
|
|
}
|
|
|
|
func (cw *crlfWriter) Write(p []byte) (int, error) {
|
|
var written int
|
|
for _, b := range p {
|
|
if b == '\n' && cw.lastByte != '\r' {
|
|
if _, err := cw.w.Write([]byte{'\r'}); err != nil {
|
|
return written, err
|
|
}
|
|
}
|
|
if _, err := cw.w.Write([]byte{b}); err != nil {
|
|
return written, err
|
|
}
|
|
written++
|
|
cw.lastByte = b
|
|
}
|
|
return written, nil
|
|
}
|