2023-04-09 22:32:15 +08:00
|
|
|
//go:build darwin || linux
|
|
|
|
|
|
|
|
|
|
package libbox
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2023-09-06 19:50:15 +08:00
|
|
|
"runtime"
|
2025-12-18 23:12:25 +08:00
|
|
|
"runtime/debug"
|
2023-04-09 22:32:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func RedirectStderr(path string) error {
|
|
|
|
|
if stats, err := os.Stat(path); err == nil && stats.Size() > 0 {
|
|
|
|
|
_ = os.Rename(path, path+".old")
|
|
|
|
|
}
|
|
|
|
|
outputFile, err := os.Create(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-09-06 19:50:15 +08:00
|
|
|
if runtime.GOOS != "android" {
|
2024-01-22 18:27:06 +08:00
|
|
|
err = outputFile.Chown(sUserID, sGroupID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
outputFile.Close()
|
|
|
|
|
os.Remove(outputFile.Name())
|
|
|
|
|
return err
|
2023-07-24 15:44:11 +08:00
|
|
|
}
|
2023-04-21 17:29:00 +08:00
|
|
|
}
|
2025-12-18 23:12:25 +08:00
|
|
|
err = debug.SetCrashOutput(outputFile, debug.CrashOptions{})
|
2023-04-21 17:29:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
outputFile.Close()
|
|
|
|
|
os.Remove(outputFile.Name())
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-05-13 22:37:05 +08:00
|
|
|
return outputFile.Close()
|
2023-04-21 17:29:00 +08:00
|
|
|
}
|