glob/internal/debug/debug_enabled.go

65 lines
906 B
Go
Raw Normal View History

2018-10-02 22:52:57 +03:00
// +build globdebug
package debug
import (
"fmt"
"os"
"strings"
)
const Enabled = true
2019-02-06 23:43:38 +03:00
var (
i = 0
prefix = map[int]string{}
)
2018-10-02 22:52:57 +03:00
func Logf(f string, args ...interface{}) {
2019-02-06 23:43:38 +03:00
if f != "" && prefix[i] != "" {
f = ": " + f
}
2018-10-02 22:52:57 +03:00
fmt.Fprint(os.Stderr,
2019-02-06 23:43:38 +03:00
strings.Repeat(" ", i),
fmt.Sprintf("(%d) ", i),
prefix[i],
2018-10-02 22:52:57 +03:00
fmt.Sprintf(f, args...),
"\n",
)
}
2019-02-06 23:43:38 +03:00
func Indexing(name, s string) func(int, []int) {
EnterPrefix("%s: index: %q", name, s)
return func(index int, segments []int) {
Logf("-> %d, %v", index, segments)
LeavePrefix()
}
}
func Matching(name, s string) func(bool) {
EnterPrefix("%s: match %q", name, s)
return func(ok bool) {
Logf("-> %t", ok)
LeavePrefix()
}
}
func EnterPrefix(s string, args ...interface{}) {
Enter()
prefix[i] = fmt.Sprintf(s, args...)
Logf("")
}
func LeavePrefix() {
prefix[i] = ""
Leave()
}
2018-10-02 22:52:57 +03:00
func Enter() {
2019-02-06 23:43:38 +03:00
i++
2018-10-02 22:52:57 +03:00
}
func Leave() {
2019-02-06 23:43:38 +03:00
i--
2018-10-02 22:52:57 +03:00
}