paniclog_windows.go 869 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Log the panic under windows to the log file
  2. //
  3. // Code from minix, via
  4. //
  5. // http://play.golang.org/p/kLtct7lSUg
  6. //+build windows
  7. package tools
  8. import (
  9. "log"
  10. "os"
  11. "syscall"
  12. )
  13. var (
  14. kernel32 = syscall.MustLoadDLL("kernel32.dll")
  15. procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
  16. )
  17. func setStdHandle(stdhandle int32, handle syscall.Handle) error {
  18. r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
  19. if r0 == 0 {
  20. if e1 != 0 {
  21. return error(e1)
  22. }
  23. return syscall.EINVAL
  24. }
  25. return nil
  26. }
  27. // redirectStderr to the file passed in
  28. func RedirectStderr(f *os.File) {
  29. err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
  30. if err != nil {
  31. log.Printf("Failed to redirect stderr to file: %v", err)
  32. }
  33. // SetStdHandle does not affect prior references to stderr
  34. os.Stderr = f
  35. }