log.go 691 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //go:build darwin || linux
  2. package libbox
  3. import (
  4. "os"
  5. "runtime"
  6. "golang.org/x/sys/unix"
  7. )
  8. var stderrFile *os.File
  9. func RedirectStderr(path string) error {
  10. if stats, err := os.Stat(path); err == nil && stats.Size() > 0 {
  11. _ = os.Rename(path, path+".old")
  12. }
  13. outputFile, err := os.Create(path)
  14. if err != nil {
  15. return err
  16. }
  17. if runtime.GOOS != "android" {
  18. err = outputFile.Chown(sUserID, sGroupID)
  19. if err != nil {
  20. outputFile.Close()
  21. os.Remove(outputFile.Name())
  22. return err
  23. }
  24. }
  25. err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
  26. if err != nil {
  27. outputFile.Close()
  28. os.Remove(outputFile.Name())
  29. return err
  30. }
  31. stderrFile = outputFile
  32. return nil
  33. }