Skip to content

Commit 102ea13

Browse files
Clean up log output
1 parent cbf8dcf commit 102ea13

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

pkg/monitor/display.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"slices"
1010
"strings"
11+
"sync"
1112
"sync/atomic"
1213
"time"
1314

@@ -33,7 +34,10 @@ type Console struct {
3334
displayProgress bool
3435
}
3536

36-
var runID int64
37+
var (
38+
runID int64
39+
prettyIDCounter int64
40+
)
3741

3842
func (c *Console) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
3943
id := atomic.AddInt64(&runID, 1)
@@ -50,6 +54,8 @@ type display struct {
5054
dump dump
5155
livePrinter *livePrinter
5256
dumpState string
57+
callIDMap map[string]string
58+
callLock sync.Mutex
5359
}
5460

5561
type livePrinter struct {
@@ -83,6 +89,9 @@ func (l *livePrinter) print(event runner.Event, c call) {
8389
}
8490

8591
func (d *display) Event(event runner.Event) {
92+
d.callLock.Lock()
93+
defer d.callLock.Unlock()
94+
8695
var (
8796
currentIndex = -1
8897
currentCall call
@@ -111,23 +120,30 @@ func (d *display) Event(event runner.Event) {
111120
"parentID", currentCall.ParentID,
112121
"toolID", currentCall.ToolID)
113122

123+
prettyID, ok := d.callIDMap[currentCall.ID]
124+
if !ok {
125+
prettyID = fmt.Sprint(atomic.AddInt64(&prettyIDCounter, 1))
126+
d.callIDMap[currentCall.ID] = prettyID
127+
}
128+
114129
callName := callName{
115-
call: &currentCall,
116-
prg: d.dump.Program,
117-
calls: d.dump.Calls,
130+
prettyID: prettyID,
131+
call: &currentCall,
132+
prg: d.dump.Program,
133+
calls: d.dump.Calls,
118134
}
119135

120136
switch event.Type {
121137
case runner.EventTypeCallStart:
122138
d.livePrinter.end()
123139
currentCall.Start = event.Time
124140
currentCall.Input = event.Content
125-
log.Fields("input", event.Content).Infof("started [%s]", callName)
141+
log.Fields("input", event.Content).Infof("started [%s]", callName)
126142
case runner.EventTypeCallProgress:
127143
d.livePrinter.print(event, currentCall)
128144
case runner.EventTypeCallContinue:
129145
d.livePrinter.end()
130-
log.Fields("toolResults", event.ToolResults).Infof("continue [%s]", callName)
146+
log.Fields("toolResults", event.ToolResults).Infof("continue [%s]", callName)
131147
case runner.EventTypeChat:
132148
d.livePrinter.end()
133149
if event.ChatRequest == nil {
@@ -137,7 +153,7 @@ func (d *display) Event(event runner.Event) {
137153
"cached", event.ChatResponseCached,
138154
)
139155
} else {
140-
log.Infof("openai request sent [%s]", callName)
156+
log.Infof("sent [%s]", callName)
141157
log = log.Fields(
142158
"completionID", event.ChatCompletionID,
143159
"request", toJSON(event.ChatRequest),
@@ -154,7 +170,7 @@ func (d *display) Event(event runner.Event) {
154170
d.livePrinter.end()
155171
currentCall.End = event.Time
156172
currentCall.Output = event.Content
157-
log.Fields("output", event.Content).Infof("ended [%s]", callName)
173+
log.Fields("output", event.Content).Infof("ended [%s]", callName)
158174
}
159175

160176
d.dump.Calls[currentIndex] = currentCall
@@ -184,6 +200,7 @@ func NewConsole(opts ...Options) *Console {
184200
func newDisplay(dumpState string, progress bool) *display {
185201
display := &display{
186202
dumpState: dumpState,
203+
callIDMap: make(map[string]string),
187204
}
188205
if progress {
189206
display.livePrinter = &livePrinter{
@@ -220,9 +237,10 @@ func (j jsonDump) String() string {
220237
}
221238

222239
type callName struct {
223-
call *call
224-
prg *types.Program
225-
calls []call
240+
prettyID string
241+
call *call
242+
prg *types.Program
243+
calls []call
226244
}
227245

228246
func (c callName) String() string {
@@ -238,7 +256,7 @@ func (c callName) String() string {
238256
name = tool.Source.File
239257
}
240258
if currentCall.ID != "1" {
241-
name += "(" + currentCall.ID + ")"
259+
name += "(" + c.prettyID + ")"
242260
}
243261
msg = append(msg, name)
244262
found := false
@@ -255,7 +273,11 @@ func (c callName) String() string {
255273
}
256274

257275
slices.Reverse(msg)
258-
return strings.Join(msg, "->")
276+
result := strings.Join(msg[1:], "->")
277+
if result == "" {
278+
return "main"
279+
}
280+
return result
259281
}
260282

261283
type dump struct {

pkg/mvl/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
2929
msg += fmt.Sprintf(" [input=%s]", i)
3030
}
3131
return []byte(fmt.Sprintf("%s %s\n",
32-
entry.Time.Format(time.RFC3339),
32+
entry.Time.Format(time.TimeOnly),
3333
msg)), nil
3434
}
3535

0 commit comments

Comments
 (0)