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) } }