-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_print.go
116 lines (103 loc) · 2.95 KB
/
tree_print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020 GoFast Author(http://chende.ren). All rights reserved.
// Use of this source code is governed by a MIT license
package fst
import (
"fmt"
"github.com/qinchende/gofast/logx"
"github.com/qinchende/gofast/skill/lang"
"strings"
)
// 打印 路由树
func (gft *GoFast) printRouteTrees() {
strTree := new(strings.Builder)
strTree.WriteString("\n+++++++++++++++The route tree:\n")
for _, tree := range gft.routerTrees {
printTree(tree, strTree)
}
strTree.WriteString("++++++++++++++++++++++++++++++\n")
// 打印到控制台
debugPrintRouteTree(gft, strTree)
}
func printTree(tree *methodTree, strTree *strings.Builder) {
if tree.root == nil {
return
}
strTree.WriteString("(")
strTree.WriteString(tree.method)
strTree.WriteString(")\n")
tree.root.prettyPrint(strTree, "", true)
}
func (n *radixNode) prettyPrint(str *strings.Builder, prefix string, isTail bool) {
str.WriteString(prefix)
nextPrefix := prefix
if isTail {
str.WriteString("└── ")
nextPrefix += " "
} else {
str.WriteString("├── ")
nextPrefix += "│ "
}
// 要显示的节点内容
str.WriteString(n.match)
curLen := len([]rune(prefix)) + len([]rune(n.match))
// 缩进最大是160字符占位符
retract := 60 - curLen
for i := 1; i <= 5; i++ {
if retract >= 0 {
break
}
retract += 20
}
if retract < 0 {
retract = 0
}
strFmt := "%-" + fmt.Sprintf("%ds", retract)
str.WriteString(fmt.Sprintf(strFmt, ""))
// [优先级,动态匹配参数数量,handler数量,所有子节点首字符]
// [4-0-0-im]
//genPrintNode(str, []string{fmt.Sprint(n.priority), fmt.Sprint(n.maxParams), fmt.Sprint(len(n.handlers)), n.indices})
//genPrintNode(str, []string{fmt.Sprint(len(n.hdsItem)), n.indices})
genPrintNode(str, []string{fmt.Sprint(n.leafItem != nil), n.indices})
//genPrintNode(str, []string{n.indices})
chLen := len(n.children)
for i := 0; i < chLen-1; i++ {
n.children[i].prettyPrint(str, nextPrefix, false)
}
if chLen > 0 {
n.children[chLen-1].prettyPrint(str, nextPrefix, true)
}
}
// 打印节点对应的相关值
func genPrintNode(str *strings.Builder, arr []string) {
str.WriteString(" [")
if arr != nil && len(arr) > 0 {
if arr[0] == "true" {
str.WriteString("1")
} else {
str.WriteString("0")
}
}
for i := 1; i < len(arr); i++ {
if arr[i] != "" {
str.WriteString("-")
str.WriteString(arr[i])
}
}
str.WriteString("]\n")
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// debug print
func debugPrintRoute(gft *GoFast, ri *RouteItem) {
if !gft.IsDebugging() {
return
}
nuHandlers := len(ri.eHds)
lastHdsIdx := ri.eHds[nuHandlers-1]
fun := ri.group.myApp.fstMem.allCtxHandlers[lastHdsIdx]
logx.DebugF("%-6s %-25s %s (%d hds)", ri.method, ri.fullPath, lang.FuncFullName(fun), nuHandlers)
}
func debugPrintRouteTree(gft *GoFast, strTree *strings.Builder) {
if gft.IsDebugging() {
logx.DebugDirect(strTree.String())
}
}