init
This commit is contained in:
21
crash/crash_darwin.go
Normal file
21
crash/crash_darwin.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// +build darwin
|
||||
|
||||
package crash
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// CrashLog set crash log
|
||||
func CrashLog(file string) {
|
||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||
} else {
|
||||
syscall.Dup2(int(f.Fd()), 2)
|
||||
}
|
||||
}
|
21
crash/crash_unix.go
Normal file
21
crash/crash_unix.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// +build freebsd openbsd netbsd dragonfly linux
|
||||
|
||||
package crash
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// CrashLog set crash log
|
||||
func CrashLog(file string) {
|
||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||
} else {
|
||||
syscall.Dup3(int(f.Fd()), 2, 0)
|
||||
}
|
||||
}
|
40
crash/crash_win.go
Normal file
40
crash/crash_win.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// +build windows
|
||||
|
||||
package crash
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// CrashLog set crash log
|
||||
func CrashLog(file string) {
|
||||
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 {
|
||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user