debug mechanics

This commit is contained in:
Sergey Kamardin 2018-10-02 22:52:57 +03:00
parent 9cd1b6671f
commit 2b9d056d0d
4 changed files with 84 additions and 44 deletions

View File

@ -5,10 +5,8 @@ package compiler
import (
"fmt"
"os"
"strings"
"sync/atomic"
"github.com/gobwas/glob/internal/debug"
"github.com/gobwas/glob/match"
"github.com/gobwas/glob/syntax/ast"
)
@ -35,23 +33,30 @@ func compileNodes(ns []*ast.Node, sep []rune) ([]match.Matcher, error) {
}
func compile(tree *ast.Node, sep []rune) (m match.Matcher, err error) {
enter()
logf("compiling %s", tree)
defer func() {
logf("result %s", m)
leave()
}()
if debug.Enabled {
debug.Enter()
debug.Logf("compiler: compiling %s", tree)
defer func() {
debug.Logf("compiler: result %s", m)
debug.Leave()
}()
}
// todo this could be faster on pattern_alternatives_combine_lite (see glob_test.go)
if n := ast.Minimize(tree); n != nil {
logf("minimized tree")
logf("\t%s", tree)
logf("\t%s", n)
r, err := compile(n, sep)
if debug.Enabled {
if err != nil {
debug.Logf("compiler: compile minimized tree failed: %v", err)
} else {
debug.Logf("compiler: minimized tree")
debug.Logf("compiler: \t%s", tree)
debug.Logf("compiler: \t%s", n)
}
}
if err == nil {
return r, nil
}
logf("compile minimized tree failed: %v", err)
}
switch tree.Kind {
@ -105,23 +110,3 @@ func compile(tree *ast.Node, sep []rune) (m match.Matcher, err error) {
return match.Optimize(m), nil
}
var i = new(int32)
func logf(f string, args ...interface{}) {
n := int(atomic.LoadInt32(i))
fmt.Fprint(os.Stderr,
strings.Repeat(" ", n),
fmt.Sprintf("(%d) ", n),
fmt.Sprintf(f, args...),
"\n",
)
}
func enter() {
atomic.AddInt32(i, 1)
}
func leave() {
atomic.AddInt32(i, -1)
}

View File

@ -0,0 +1,9 @@
// +build !globdebug
package debug
const Enabled = false
func Logf(_ string, _ ...interface{}) {}
func Enter() {}
func Leave() {}

View File

@ -0,0 +1,32 @@
// +build globdebug
package debug
import (
"fmt"
"os"
"strings"
"sync/atomic"
)
const Enabled = true
var i = new(int32)
func Logf(f string, args ...interface{}) {
n := int(atomic.LoadInt32(i))
fmt.Fprint(os.Stderr,
strings.Repeat(" ", n),
fmt.Sprintf("(%d) ", n),
fmt.Sprintf(f, args...),
"\n",
)
}
func Enter() {
atomic.AddInt32(i, 1)
}
func Leave() {
atomic.AddInt32(i, -1)
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"unicode/utf8"
"github.com/gobwas/glob/internal/debug"
"github.com/gobwas/glob/util/runes"
)
@ -74,21 +75,28 @@ func (t Tree) MinLen() int {
}
func (t Tree) Match(s string) (ok bool) {
enter()
logf("matching %q: %v", s, t)
defer func(s string) {
logf("result: %q -> %v", s, ok)
leave()
}(s)
if debug.Enabled {
debug.Enter()
debug.Logf("tree: matching %q: %v", s, t)
defer func(s string) {
debug.Logf("tree: result: %q -> %v", s, ok)
debug.Leave()
}(s)
}
offset, limit := t.offsetLimit(s)
q := s[offset : len(s)-limit]
logf("OFFSET/LIMIT: %d/%d %q of %q", offset, limit, q, s)
if debug.Enabled {
debug.Logf("tree: offset/limit: %d/%d %q of %q", offset, limit, q, s)
}
for len(q) >= t.vrunes {
// search for matching part in substring
index, segments := t.value.Index(q)
logf("INDEX #%d %q (%v)", index, q, t.value)
if debug.Enabled {
debug.Logf("tree: index #%d %q (%v)", index, q, t.value)
}
if index == -1 {
releaseSegments(segments)
return false
@ -101,7 +109,9 @@ func (t Tree) Match(s string) (ok bool) {
} else {
left = l == ""
}
logf("LEFT %q %v", l, left)
if debug.Enabled {
debug.Logf("tree: left %q %v", l, left)
}
if left {
for _, seg := range segments {
var (
@ -113,7 +123,9 @@ func (t Tree) Match(s string) (ok bool) {
} else {
right = r == ""
}
logf("RIGHT %q %v", r, right)
if debug.Enabled {
debug.Logf("tree: right %q %v", r, right)
}
if right {
releaseSegments(segments)
return true
@ -125,7 +137,9 @@ func (t Tree) Match(s string) (ok bool) {
releaseSegments(segments)
q = q[x:]
offset += x
logf("SLICED TO %q", q)
if debug.Enabled {
debug.Logf("tree: sliced to %q", q)
}
}
return false