summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/btrfs-rec/main.go4
-rw-r--r--lib/textui/log.go44
-rw-r--r--lib/textui/log_test.go25
3 files changed, 41 insertions, 32 deletions
diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go
index d4165bf..dc00dab 100644
--- a/cmd/btrfs-rec/main.go
+++ b/cmd/btrfs-rec/main.go
@@ -103,7 +103,9 @@ func main() {
ctx := cmd.Context()
logger := textui.NewLogger(os.Stderr, logLevelFlag.Level)
ctx = dlog.WithLogger(ctx, logger)
- ctx = dlog.WithField(ctx, "mem", new(textui.LiveMemUse))
+ if logLevelFlag.Level >= dlog.LogLevelDebug {
+ ctx = dlog.WithField(ctx, "mem", new(textui.LiveMemUse))
+ }
dlog.SetFallbackLogger(logger.WithField("btrfs-progs.THIS_IS_A_BUG", true))
grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{
diff --git a/lib/textui/log.go b/lib/textui/log.go
index b4dcea4..2a6fdd4 100644
--- a/lib/textui/log.go
+++ b/lib/textui/log.go
@@ -237,28 +237,30 @@ func (l *logger) log(lvl dlog.LogLevel, writeMsg func(io.Writer)) {
}
// caller //////////////////////////////////////////////////////////////
- const (
- thisModule = "git.lukeshu.com/btrfs-progs-ng"
- thisPackage = "git.lukeshu.com/btrfs-progs-ng/lib/textui"
- maximumCallerDepth int = 25
- minimumCallerDepth int = 3 // runtime.Callers + .log + .Log
- )
- var pcs [maximumCallerDepth]uintptr
- depth := runtime.Callers(minimumCallerDepth, pcs[:])
- frames := runtime.CallersFrames(pcs[:depth])
- for f, again := frames.Next(); again; f, again = frames.Next() {
- if !strings.HasPrefix(f.Function, thisModule+"/") {
- continue
- }
- if strings.HasPrefix(f.Function, thisPackage+".") {
- continue
- }
- if nextField == len(fieldKeys) {
- logBuf.WriteString(" :")
+ if lvl >= dlog.LogLevelDebug {
+ const (
+ thisModule = "git.lukeshu.com/btrfs-progs-ng"
+ thisPackage = "git.lukeshu.com/btrfs-progs-ng/lib/textui"
+ maximumCallerDepth int = 25
+ minimumCallerDepth int = 3 // runtime.Callers + .log + .Log
+ )
+ var pcs [maximumCallerDepth]uintptr
+ depth := runtime.Callers(minimumCallerDepth, pcs[:])
+ frames := runtime.CallersFrames(pcs[:depth])
+ for f, again := frames.Next(); again; f, again = frames.Next() {
+ if !strings.HasPrefix(f.Function, thisModule+"/") {
+ continue
+ }
+ if strings.HasPrefix(f.Function, thisPackage+".") {
+ continue
+ }
+ if nextField == len(fieldKeys) {
+ logBuf.WriteString(" :")
+ }
+ file := f.File[strings.LastIndex(f.File, thisModDir+"/")+len(thisModDir+"/"):]
+ fmt.Fprintf(logBuf, " (from %s:%d)", file, f.Line)
+ break
}
- file := f.File[strings.LastIndex(f.File, thisModDir+"/")+len(thisModDir+"/"):]
- fmt.Fprintf(logBuf, " (from %s:%d)", file, f.Line)
- break
}
// boilerplate /////////////////////////////////////////////////////////
diff --git a/lib/textui/log_test.go b/lib/textui/log_test.go
index bcd9c39..44958cb 100644
--- a/lib/textui/log_test.go
+++ b/lib/textui/log_test.go
@@ -15,8 +15,13 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)
-func logLineRegexp(inner string) string {
- return `[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{4} ` + inner + ` \(from lib/textui/log_test\.go:[0-9]+\)\n`
+func logLineRegexp(includePos bool, inner string) string {
+ ret := `[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{4} ` + inner
+ if includePos {
+ ret += ` : \(from lib/textui/log_test\.go:[0-9]+\)`
+ }
+ ret += `\n`
+ return ret
}
func TestLogFormat(t *testing.T) {
@@ -25,7 +30,7 @@ func TestLogFormat(t *testing.T) {
ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelTrace))
dlog.Debugf(ctx, "foo %d", 12345)
assert.Regexp(t,
- `^`+logLineRegexp(`DBG : foo 12,345 :`)+`$`,
+ `^`+logLineRegexp(true, `DBG : foo 12,345`)+`$`,
out.String())
}
@@ -45,12 +50,12 @@ func TestLogLevel(t *testing.T) {
dlog.Error(ctx, "Error")
assert.Regexp(t,
`^`+
- logLineRegexp(`ERR : Error :`)+
- logLineRegexp(`WRN : Warn :`)+
- logLineRegexp(`INF : Info :`)+
- logLineRegexp(`INF : Info :`)+
- logLineRegexp(`WRN : Warn :`)+
- logLineRegexp(`ERR : Error :`)+
+ logLineRegexp(false, `ERR : Error`)+
+ logLineRegexp(false, `WRN : Warn`)+
+ logLineRegexp(false, `INF : Info`)+
+ logLineRegexp(false, `INF : Info`)+
+ logLineRegexp(false, `WRN : Warn`)+
+ logLineRegexp(false, `ERR : Error`)+
`$`,
out.String())
}
@@ -62,6 +67,6 @@ func TestLogField(t *testing.T) {
ctx = dlog.WithField(ctx, "foo", 12345)
dlog.Info(ctx, "msg")
assert.Regexp(t,
- `^`+logLineRegexp(`INF : msg : foo=12,345`)+`$`,
+ `^`+logLineRegexp(false, `INF : msg : foo=12,345`)+`$`,
out.String())
}