log.go 720 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. if sUserID > 0 {
  19. err = outputFile.Chown(sUserID, sGroupID)
  20. if err != nil {
  21. outputFile.Close()
  22. os.Remove(outputFile.Name())
  23. return err
  24. }
  25. }
  26. }
  27. err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
  28. if err != nil {
  29. outputFile.Close()
  30. os.Remove(outputFile.Name())
  31. return err
  32. }
  33. stderrFile = outputFile
  34. return nil
  35. }