forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
313 lines (270 loc) · 8.02 KB
/
debug.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package depinject
import (
"fmt"
"os"
"path/filepath"
"reflect"
"cosmossdk.io/depinject/internal/graphviz"
)
// DebugOption is a functional option for running a container that controls
// debug logging and visualization output.
type DebugOption interface {
applyConfig(*debugConfig) error
}
// StdoutLogger is a debug option which routes logging output to stdout.
func StdoutLogger() DebugOption {
return Logger(func(s string) {
_, _ = fmt.Fprintln(os.Stdout, s)
})
}
// StderrLogger is a debug option which routes logging output to stderr.
func StderrLogger() DebugOption {
return Logger(func(s string) {
_, _ = fmt.Fprintln(os.Stderr, s)
})
}
// Visualizer creates an option which provides a visualizer function which
// will receive a rendering of the container in the Graphiz DOT format
// whenever the container finishes building or fails due to an error. The
// graph is color-coded to aid debugging with black representing success,
// red representing an error, and gray representing unused types or functions.
// Graph rendering should be deterministic for a given version of the container
// module and container options so that graphs can be used in tests.
func Visualizer(visualizer func(dotGraph string)) DebugOption {
return debugOption(func(c *debugConfig) error {
c.addFuncVisualizer(visualizer)
return nil
})
}
// LogVisualizer is a debug option which dumps a graphviz DOT rendering of
// the container to the log.
func LogVisualizer() DebugOption {
return debugOption(func(c *debugConfig) error {
c.enableLogVisualizer()
return nil
})
}
// FileVisualizer is a debug option which dumps a graphviz DOT rendering of
// the container to the specified file.
func FileVisualizer(filename string) DebugOption {
return debugOption(func(c *debugConfig) error {
c.addFileVisualizer(filename)
return nil
})
}
// Logger creates an option which provides a logger function which will
// receive all log messages from the container.
func Logger(logger func(string)) DebugOption {
return debugOption(func(c *debugConfig) error {
logger("Initializing logger")
c.loggers = append(c.loggers, logger)
// send conditional log messages batched for onError/onSuccess cases
if c.logBuf != nil {
for _, s := range *c.logBuf {
logger(s)
}
}
return nil
})
}
const debugContainerDot = "debug_container.dot"
// Debug is a default debug option which sends log output to stderr, dumps
// the container in the graphviz DOT and SVG formats to debug_container.dot
// and debug_container.svg respectively.
func Debug() DebugOption {
return DebugOptions(
StderrLogger(),
FileVisualizer(debugContainerDot),
)
}
func (d *debugConfig) initLogBuf() {
if d.logBuf == nil {
d.logBuf = &[]string{}
d.loggers = append(d.loggers, func(s string) {
*d.logBuf = append(*d.logBuf, s)
})
}
}
// OnError is a debug option that allows setting debug options that are
// conditional on an error happening. Any loggers added error will
// receive the full dump of logs since the start of container processing.
func OnError(option DebugOption) DebugOption {
return debugOption(func(config *debugConfig) error {
config.initLogBuf()
config.onError = option
return nil
})
}
// OnSuccess is a debug option that allows setting debug options that are
// conditional on successful container resolution. Any loggers added on success
// will receive the full dump of logs since the start of container processing.
func OnSuccess(option DebugOption) DebugOption {
return debugOption(func(config *debugConfig) error {
config.initLogBuf()
config.onSuccess = option
return nil
})
}
// DebugCleanup specifies a clean-up function to be called at the end of
// processing to clean up any resources that may be used during debugging.
func DebugCleanup(cleanup func()) DebugOption {
return debugOption(func(config *debugConfig) error {
config.cleanup = append(config.cleanup, cleanup)
return nil
})
}
// AutoDebug does the same thing as Debug when there is an error and deletes
// the debug_container.dot if it exists when there is no error. This is the
// default debug mode of Run.
func AutoDebug() DebugOption {
return DebugOptions(
OnError(Debug()),
OnSuccess(DebugCleanup(func() {
deleteIfExists(debugContainerDot)
})),
)
}
func deleteIfExists(filename string) {
if _, err := os.Stat(filename); err == nil {
_ = os.Remove(filename)
}
}
// DebugOptions creates a debug option which bundles together other debug options.
func DebugOptions(options ...DebugOption) DebugOption {
return debugOption(func(c *debugConfig) error {
for _, opt := range options {
if err := opt.applyConfig(c); err != nil {
return err
}
}
return nil
})
}
type debugConfig struct {
// logging
loggers []func(string)
indentStr string
logBuf *[]string // a log buffer for onError/onSuccess processing
// graphing
graph *graphviz.Graph
visualizers []func(string)
logVisualizer bool
// extra processing
onError DebugOption
onSuccess DebugOption
cleanup []func()
}
type debugOption func(*debugConfig) error
func (c debugOption) applyConfig(ctr *debugConfig) error {
return c(ctr)
}
var _ DebugOption = (*debugOption)(nil)
func newDebugConfig() (*debugConfig, error) {
return &debugConfig{
graph: graphviz.NewGraph(),
}, nil
}
func (c *debugConfig) indentLogger() {
c.indentStr = c.indentStr + " "
}
func (c *debugConfig) dedentLogger() {
if len(c.indentStr) > 0 {
c.indentStr = c.indentStr[1:]
}
}
func (c debugConfig) logf(format string, args ...interface{}) {
s := fmt.Sprintf(c.indentStr+format, args...)
for _, logger := range c.loggers {
logger(s)
}
}
func (c *debugConfig) generateGraph() {
dotStr := c.graph.String()
if c.logVisualizer {
c.logf("DOT Graph: %s", dotStr)
}
for _, v := range c.visualizers {
v(dotStr)
}
}
func (c *debugConfig) addFuncVisualizer(f func(string)) {
c.visualizers = append(c.visualizers, func(dot string) {
f(dot)
})
}
func (c *debugConfig) enableLogVisualizer() {
c.logVisualizer = true
}
func (c *debugConfig) addFileVisualizer(filename string) {
c.visualizers = append(c.visualizers, func(_ string) {
dotStr := c.graph.String()
err := os.WriteFile(filename, []byte(dotStr), 0o644)
if err != nil {
c.logf("Error saving graphviz file %s: %+v", filename, err)
} else {
path, err := filepath.Abs(filename)
if err == nil {
c.logf("Saved graph of container to %s", path)
}
}
})
}
func (c *debugConfig) locationGraphNode(location Location, key *moduleKey) *graphviz.Node {
graph := c.moduleSubGraph(key)
name := location.Name()
node, found := graph.FindOrCreateNode(name)
if found {
return node
}
node.SetShape("box")
setUnusedStyle(node.Attributes)
return node
}
func (c *debugConfig) typeGraphNode(typ reflect.Type) *graphviz.Node {
name := moreUsefulTypeString(typ)
node, found := c.graph.FindOrCreateNode(name)
if found {
return node
}
setUnusedStyle(node.Attributes)
return node
}
func setUnusedStyle(attr *graphviz.Attributes) {
attr.SetColor("lightgrey")
attr.SetPenWidth("0.5")
attr.SetFontColor("dimgrey")
}
// moreUsefulTypeString is more useful than reflect.Type.String()
func moreUsefulTypeString(ty reflect.Type) string {
switch ty.Kind() {
case reflect.Struct, reflect.Interface:
return fmt.Sprintf("%s.%s", ty.PkgPath(), ty.Name())
case reflect.Pointer:
return fmt.Sprintf("*%s", moreUsefulTypeString(ty.Elem()))
case reflect.Map:
return fmt.Sprintf("map[%s]%s", moreUsefulTypeString(ty.Key()), moreUsefulTypeString(ty.Elem()))
case reflect.Slice:
return fmt.Sprintf("[]%s", moreUsefulTypeString(ty.Elem()))
default:
return ty.String()
}
}
func (c *debugConfig) moduleSubGraph(key *moduleKey) *graphviz.Graph {
if key == nil {
// return the root graph
return c.graph
} else {
gname := fmt.Sprintf("cluster_%s", key.name)
graph, found := c.graph.FindOrCreateSubGraph(gname)
if !found {
graph.SetLabel(fmt.Sprintf("Module: %s", key.name))
graph.SetPenWidth("0.5")
graph.SetFontSize("12.0")
graph.SetStyle("rounded")
}
return graph
}
}
func (c *debugConfig) addGraphEdge(from, to *graphviz.Node) {
_ = c.graph.CreateEdge(from, to)
}