1
0
Fork 0

Add -dump flag to help troubleshooting

This commit is contained in:
Maurizio Porrato 2022-09-28 08:43:37 +01:00
parent 1996daf321
commit 6dd5412956
2 changed files with 32 additions and 0 deletions

12
main.go
View File

@ -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
}

View File

@ -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