You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
731 B

  1. // +build windows
  2. package log
  3. import (
  4. "log"
  5. "os"
  6. "syscall"
  7. )
  8. var (
  9. kernel32 = syscall.MustLoadDLL("kernel32.dll")
  10. procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
  11. )
  12. func setStdHandle(stdhandle int32, handle syscall.Handle) error {
  13. r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
  14. if r0 == 0 {
  15. if e1 != 0 {
  16. return error(e1)
  17. }
  18. return syscall.EINVAL
  19. }
  20. return nil
  21. }
  22. func CrashLog(file string) {
  23. f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
  24. if err != nil {
  25. log.Println(err.Error())
  26. } else {
  27. err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
  28. if err != nil {
  29. log.Println(err.Error())
  30. }
  31. }
  32. }