Files
kato 2318684e93 refactor(core): add headless run and auto-restart
- Run without console by default
- Log to file once console hidden
- Recover panics in background goroutines
- Restart agent after unexpected errors
- Use shared instance launch helper
2026-09-01 20:12:12 +03:00

51 lines
935 B
Go

package helpers
import (
"fmt"
"io"
"log"
"os"
"sync"
"tea.chunkbyte.com/kato/go-worm/lib/crashlog"
)
var (
logMu sync.Mutex
logW io.Writer = os.Stdout
)
var Log = log.New(&logWriter{}, "", log.Ldate|log.Ltime|log.Lmicroseconds)
type logWriter struct{}
func (logWriter) Write(p []byte) (int, error) {
logMu.Lock()
defer logMu.Unlock()
return logW.Write(p)
}
// SetLogOutput redirects application logging (e.g. after hiding the console).
func SetLogOutput(w io.Writer) {
logMu.Lock()
defer logMu.Unlock()
logW = w
}
// Go runs fn in a goroutine with panic recovery.
func Go(component string, fn func()) {
go func() {
defer RecoverLog(component)
fn()
}()
}
// RecoverLog logs a recovered panic from a background component.
func RecoverLog(component string) {
if r := recover(); r != nil {
detail := fmt.Sprintf("%s: %v", component, r)
Log.Printf("panic %s", detail)
crashlog.RecordPanic(detail)
}
}