This commit is contained in:
Yum-King 2026-05-06 09:18:57 +08:00 committed by GitHub
commit e76d8e7322
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View file

@ -941,7 +941,19 @@ func (h *Handler) startGatewayLocked(initialStatus string, existingPid int) (int
// Wait for exit in background and clean up
go func() {
if err := cmd.Wait(); err != nil {
logger.ErrorC("gateway", fmt.Sprintf("Gateway process exited: %v", err))
tail := gateway.logs.Tail(50)
if len(tail) > 0 {
logger.ErrorC(
"gateway",
fmt.Sprintf(
"Gateway process exited: %v\nLast gateway logs:\n%s",
err,
strings.Join(tail, "\n"),
),
)
} else {
logger.ErrorC("gateway", fmt.Sprintf("Gateway process exited: %v", err))
}
} else {
logger.InfoC("gateway", "Gateway process exited normally")
}

View file

@ -95,3 +95,35 @@ func (b *LogBuffer) RunID() int {
return b.runID
}
// Tail returns up to the last n buffered lines (newest last).
// If n <= 0, it returns nil.
func (b *LogBuffer) Tail(n int) []string {
if n <= 0 {
return nil
}
b.mu.RLock()
defer b.mu.RUnlock()
if b.total == 0 || len(b.lines) == 0 {
return nil
}
if n > len(b.lines) {
n = len(b.lines)
}
result := make([]string, n)
if b.total <= b.cap {
copy(result, b.lines[len(b.lines)-n:])
return result
}
start := (b.total - n) % b.cap
for i := 0; i < n; i++ {
result[i] = b.lines[(start+i)%b.cap]
}
return result
}