Skip to content

Commit 62b3005

Browse files
committed
internal/core/adt: several logging improvements
Replace the file prefix with a logging sequence number: the file name was not very useful and mostly messed with indentation. The sequence number, OTOH, allows for more easily pinpointing the position in the log and is quite handy for setting conditional break points. (Developer can set a ctx.logID == n condition on a breakpoint after the log to stop at that particular point in evaluation). The Logf function now uses a strings.Builder. This should be faster, but also results in nicer code. Change the nesting around the Unify and Start Process headers. This improves legibility of logs Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: Ie6d67902e0c13831d390b9cd1ac097c9d007fda9 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207550 Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent c8170aa commit 62b3005

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

internal/core/adt/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ type OpContext struct {
182182
IsValidator bool
183183

184184
// ==== Debugging ====
185+
logID int // sequence number for log messages
185186

186187
// ErrorGraphs contains an analysis, represented as a Mermaid graph, for
187188
// each node that has an error.

internal/core/adt/log.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *OpContext) Assertf(pos token.Pos, b bool, format string, args ...interf
4848
}
4949

5050
func init() {
51-
log.SetFlags(log.Lshortfile)
51+
log.SetFlags(0)
5252
}
5353

5454
var pMap = map[*Vertex]int{}
@@ -57,41 +57,43 @@ func (c *OpContext) Logf(v *Vertex, format string, args ...interface{}) {
5757
if c.LogEval == 0 {
5858
return
5959
}
60-
var prefix string
60+
w := &strings.Builder{}
61+
62+
c.logID++
63+
fmt.Fprintf(w, "%3d ", c.logID)
64+
6165
if c.nest > 0 {
62-
prefix = strings.Repeat("... ", c.nest)
63-
prefix = prefix[:len(prefix)-1]
66+
for i := 0; i < c.nest; i++ {
67+
w.WriteString("... ")
68+
}
6469
}
6570

6671
if v == nil {
67-
s := fmt.Sprintf(prefix+format, args...)
68-
_ = log.Output(2, s)
72+
fmt.Fprintf(w, format, args...)
73+
_ = log.Output(2, w.String())
6974
return
7075
}
76+
7177
p := pMap[v]
7278
if p == 0 {
7379
p = len(pMap) + 1
7480
pMap[v] = p
7581
}
7682
disjunctInfo := c.disjunctInfo()
83+
fmt.Fprintf(w, "[n:%d/%v %s%s] ",
84+
p, v.Path(), c.PathToString(v.Path()), disjunctInfo)
7785

78-
a := append([]interface{}{
79-
prefix,
80-
p,
81-
v.Path(),
82-
c.PathToString(v.Path()),
83-
disjunctInfo,
84-
}, args...)
85-
for i := 2; i < len(a); i++ {
86-
switch x := a[i].(type) {
86+
for i, a := range args {
87+
switch x := a.(type) {
8788
case Node:
88-
a[i] = c.Str(x)
89+
args[i] = c.Str(x)
8990
case Feature:
90-
a[i] = x.SelectorString(c)
91+
args[i] = x.SelectorString(c)
9192
}
9293
}
93-
s := fmt.Sprintf("%s [n:%d %v%s%s] "+format, a...)
94-
_ = log.Output(2, s)
94+
fmt.Fprintf(w, format, args...)
95+
96+
_ = log.Output(2, w.String())
9597
}
9698

9799
// PathToString creates a pretty-printed path of the given list of features.

internal/core/adt/sched.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ func (s *scheduler) process(needs condition, mode runMode) bool {
369369

370370
if s.ctx.LogEval > 0 && len(s.tasks) > 0 {
371371
if v := s.tasks[0].node.node; v != nil {
372-
c.nest++
373372
c.Logf(v, "START Process %v -- mode: %v", v.Label, mode)
373+
c.nest++
374374
defer func() {
375-
c.Logf(v, "END Process")
376375
c.nest--
376+
c.Logf(v, "END Process")
377377
}()
378378
}
379379
}

internal/core/adt/unify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func (n *nodeContext) scheduleConjuncts() {
122122
// TODO(evalv3): consider not returning a result at all.
123123
func (v *Vertex) unify(c *OpContext, needs condition, mode runMode) bool {
124124
if c.LogEval > 0 {
125-
c.nest++
126125
c.Logf(v, "Unify %v", fmt.Sprintf("%p", v))
126+
c.nest++
127127
defer func() {
128-
c.Logf(v, "END Unify")
129128
c.nest--
129+
c.Logf(v, "END Unify")
130130
}()
131131
}
132132

0 commit comments

Comments
 (0)