2019-11-05 07:57:09 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package crash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kernel32 = syscall.MustLoadDLL("kernel32.dll")
|
|
|
|
procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
|
|
|
|
)
|
|
|
|
|
|
|
|
func setStdHandle(stdhandle int32, handle syscall.Handle) error {
|
|
|
|
r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
|
|
|
|
if r0 == 0 {
|
|
|
|
if e1 != 0 {
|
|
|
|
return error(e1)
|
|
|
|
}
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:11:12 +00:00
|
|
|
// NewCrashLog set crash log
|
|
|
|
func NewCrashLog(file string) {
|
2019-11-05 07:57:09 +00:00
|
|
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
} else {
|
|
|
|
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
|
|
|
|
if err != nil {
|
2019-11-08 09:45:09 +00:00
|
|
|
log.Fatalf("open crash log file error. %v", err)
|
2019-11-05 07:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|