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 ( import (
"fmt" "fmt"
"os"
"strings"
"sync/atomic"
"github.com/gobwas/glob/internal/debug"
"github.com/gobwas/glob/match" "github.com/gobwas/glob/match"
"github.com/gobwas/glob/syntax/ast" "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) { func compile(tree *ast.Node, sep []rune) (m match.Matcher, err error) {
enter() if debug.Enabled {
logf("compiling %s", tree) debug.Enter()
debug.Logf("compiler: compiling %s", tree)
defer func() { defer func() {
logf("result %s", m) debug.Logf("compiler: result %s", m)
leave() debug.Leave()
}() }()
}
// todo this could be faster on pattern_alternatives_combine_lite (see glob_test.go) // todo this could be faster on pattern_alternatives_combine_lite (see glob_test.go)
if n := ast.Minimize(tree); n != nil { if n := ast.Minimize(tree); n != nil {
logf("minimized tree")
logf("\t%s", tree)
logf("\t%s", n)
r, err := compile(n, sep) 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 { if err == nil {
return r, nil return r, nil
} }
logf("compile minimized tree failed: %v", err)
} }
switch tree.Kind { switch tree.Kind {
@ -105,23 +110,3 @@ func compile(tree *ast.Node, sep []rune) (m match.Matcher, err error) {
return match.Optimize(m), nil 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" "fmt"
"unicode/utf8" "unicode/utf8"
"github.com/gobwas/glob/internal/debug"
"github.com/gobwas/glob/util/runes" "github.com/gobwas/glob/util/runes"
) )
@ -74,21 +75,28 @@ func (t Tree) MinLen() int {
} }
func (t Tree) Match(s string) (ok bool) { func (t Tree) Match(s string) (ok bool) {
enter() if debug.Enabled {
logf("matching %q: %v", s, t) debug.Enter()
debug.Logf("tree: matching %q: %v", s, t)
defer func(s string) { defer func(s string) {
logf("result: %q -> %v", s, ok) debug.Logf("tree: result: %q -> %v", s, ok)
leave() debug.Leave()
}(s) }(s)
}
offset, limit := t.offsetLimit(s) offset, limit := t.offsetLimit(s)
q := s[offset : len(s)-limit] 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 { for len(q) >= t.vrunes {
// search for matching part in substring // search for matching part in substring
index, segments := t.value.Index(q) 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 { if index == -1 {
releaseSegments(segments) releaseSegments(segments)
return false return false
@ -101,7 +109,9 @@ func (t Tree) Match(s string) (ok bool) {
} else { } else {
left = l == "" left = l == ""
} }
logf("LEFT %q %v", l, left) if debug.Enabled {
debug.Logf("tree: left %q %v", l, left)
}
if left { if left {
for _, seg := range segments { for _, seg := range segments {
var ( var (
@ -113,7 +123,9 @@ func (t Tree) Match(s string) (ok bool) {
} else { } else {
right = r == "" right = r == ""
} }
logf("RIGHT %q %v", r, right) if debug.Enabled {
debug.Logf("tree: right %q %v", r, right)
}
if right { if right {
releaseSegments(segments) releaseSegments(segments)
return true return true
@ -125,7 +137,9 @@ func (t Tree) Match(s string) (ok bool) {
releaseSegments(segments) releaseSegments(segments)
q = q[x:] q = q[x:]
offset += x offset += x
logf("SLICED TO %q", q) if debug.Enabled {
debug.Logf("tree: sliced to %q", q)
}
} }
return false return false