Add -dump flag to help troubleshooting
This commit is contained in:
parent
1996daf321
commit
6dd5412956
12
main.go
12
main.go
|
@ -18,6 +18,7 @@ import (
|
|||
var Flags struct {
|
||||
ConfigFile string
|
||||
LogFile string
|
||||
DumpFile string
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
|
@ -164,6 +165,7 @@ func Rewrite(t *trie.Trie) {
|
|||
func init() {
|
||||
flag.StringVar(&Flags.ConfigFile, "c", "", "Path to rewrite config file")
|
||||
flag.StringVar(&Flags.LogFile, "l", "", "Path to log file")
|
||||
flag.StringVar(&Flags.DumpFile, "dump", "", "Path to dump file")
|
||||
flag.BoolVar(&Flags.Verbose, "v", false, "Verbose logging")
|
||||
}
|
||||
|
||||
|
@ -196,6 +198,16 @@ func LoadRewrites() *trie.Trie {
|
|||
elapsed := time.Since(start)
|
||||
L.Printf("Loaded %d unique rewrites in %s", t.Count(), elapsed)
|
||||
|
||||
if Flags.DumpFile != "" {
|
||||
file, err := os.OpenFile(Flags.DumpFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
L.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
t.Dump(file)
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
|
|
20
trie/trie.go
20
trie/trie.go
|
@ -1,5 +1,10 @@
|
|||
package trie
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func commonPrefixLength(s1 string, s2 string) int {
|
||||
var i, l int
|
||||
|
||||
|
@ -39,6 +44,21 @@ func (t *Trie) Count() int {
|
|||
return r
|
||||
}
|
||||
|
||||
func (t *Trie) dump(prefix string, w io.Writer) {
|
||||
for chunk, subTrie := range t.children {
|
||||
newPrefix := prefix + chunk
|
||||
if subTrie.value != nil {
|
||||
line := fmt.Sprintf("%s %s\n", newPrefix, subTrie.value)
|
||||
w.Write([]byte(line))
|
||||
}
|
||||
subTrie.dump(newPrefix, w)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Trie) Dump(w io.Writer) {
|
||||
t.dump("", w)
|
||||
}
|
||||
|
||||
func (t *Trie) Put(key string, value interface{}) {
|
||||
currentKey := key
|
||||
currentTrie := t
|
||||
|
|
Loading…
Reference in New Issue