sing-box/experimental/libbox/log.go

41 lines
720 B
Go
Raw Permalink Normal View History

2023-04-09 14:32:15 +00:00
//go:build darwin || linux
package libbox
import (
"os"
2023-09-06 11:50:15 +00:00
"runtime"
2023-04-09 14:32:15 +00:00
"golang.org/x/sys/unix"
)
var stderrFile *os.File
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 11:50:15 +00:00
if runtime.GOOS != "android" {
if sUserID > 0 {
err = outputFile.Chown(sUserID, sGroupID)
if err != nil {
outputFile.Close()
os.Remove(outputFile.Name())
return err
}
2023-07-24 07:44:11 +00:00
}
2023-04-21 09:29:00 +00:00
}
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
if err != nil {
outputFile.Close()
os.Remove(outputFile.Name())
return err
}
stderrFile = outputFile
return nil
}