Skip to content

Commit b91fde9

Browse files
committed
simulate(tui): fold SDK logs into the run report file
1 parent 889f3ce commit b91fde9

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

cmd/lk/simulate_report.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ func newRunReporter() *runReporter {
160160
return &runReporter{simLog: newSimLog(w, w), f: f}
161161
}
162162

163+
// LogWriter is the destination for SDK/CLI log lines, interleaved into the
164+
// same file as the run record.
165+
func (r *runReporter) LogWriter() io.Writer {
166+
if r.f == nil {
167+
return io.Discard
168+
}
169+
return r.f
170+
}
171+
163172
func (r *runReporter) Finish(run *livekit.SimulationRun, ap *AgentProcess, brokenAgent bool, dashboardURL string) string {
164173
if r.f == nil {
165174
return ""

cmd/lk/simulate_tui.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ import (
4343

4444
func runSimulateTUI(config *simulateConfig) error {
4545
// SDK/protocol log lines go to stderr behind the alt screen and would all
46-
// spill into the terminal when the TUI exits; capture them in a file
47-
// instead, surfaced as a path on exit like the agent log.
48-
cliLogPath := redirectLogsToFile()
46+
// spill into the terminal when the TUI exits; route them into the run
47+
// report instead, interleaved with the same record CI mode prints.
48+
reporter := newRunReporter()
49+
redirectLogs(reporter.LogWriter())
4950

5051
launcher := launchSimulationAgent(config)
5152
m := newSimulateModel(config, launcher)
53+
m.reporter = reporter
5254
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
5355
_, runErr := p.Run()
5456

@@ -81,10 +83,6 @@ func runSimulateTUI(config *simulateConfig) error {
8183
}
8284
}
8385

84-
if cliLogPath != "" {
85-
out.Statusf("CLI logs: %s", cliLogPath)
86-
}
87-
8886
// The summary (which carries the chat transcripts) may have landed after
8987
// the TUI's last poll; refresh once so the report includes it.
9088
if m.run != nil && m.run.Summary == nil && m.runID != "" {
@@ -117,21 +115,13 @@ func runSimulateTUI(config *simulateConfig) error {
117115
return nil
118116
}
119117

120-
// redirectLogsToFile points the protocol/SDK/stdlib loggers at a temp file
121-
// and returns its path; on failure the logs are discarded (they would
122-
// otherwise corrupt the TUI and spill on exit).
123-
func redirectLogsToFile() string {
124-
f, err := os.CreateTemp("", "lk-simulate-cli-*.log")
125-
if err != nil {
126-
discard := logger.LogRLogger(logr.Discard())
127-
logger.SetLogger(discard, "lk")
128-
lksdk.SetLogger(discard)
129-
log.SetOutput(io.Discard)
130-
return ""
131-
}
118+
// redirectLogs points the protocol/SDK/stdlib loggers at w (the run report);
119+
// on failure the logs are discarded (they would otherwise corrupt the TUI and
120+
// spill on exit).
121+
func redirectLogs(w io.Writer) {
132122
core := zapcore.NewCore(
133123
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
134-
zapcore.AddSync(f),
124+
zapcore.AddSync(w),
135125
zapcore.InfoLevel,
136126
)
137127
zl, err := logger.FromZapLogger(zap.New(core), &logger.Config{Level: "info"})
@@ -143,8 +133,7 @@ func redirectLogsToFile() string {
143133
logger.SetLogger(zl, "lk")
144134
lksdk.SetLogger(zl)
145135
}
146-
log.SetOutput(f)
147-
return f.Name()
136+
log.SetOutput(w)
148137
}
149138

150139
// --- Styles ---
@@ -357,7 +346,6 @@ func newSimulateModel(config *simulateConfig, launcher *agentLauncher) *simulate
357346
return &simulateModel{
358347
config: config,
359348
launcher: launcher,
360-
reporter: newRunReporter(),
361349
numSimulations: config.numSimulations,
362350
width: 80,
363351
height: 24,

0 commit comments

Comments
 (0)