Commit fc257005 by feidy

新增panic后打印日志,待测试

	new file:   panic_linux.go
	new file:   panic_windows.go
parent 94cc8089
//go:build linux
// +build linux
/*
* @Author: zhoufei
* @Date: 2022-07-13 10:35:50
* @LastEditors: zhoufei
* @LastEditTime: 2022-07-13 10:41:12
* @FilePath: /utils/panic_linux.go
* @Description:
*
* Copyright (c) 2022 by zhoufei, All Rights Reserved.
*/
package utils
import (
"os"
"syscall"
)
// RedirectStderr to the file passed in
func RedirectStderr() (err error) {
logFile, err := os.OpenFile("./test-error.log", os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
if err != nil {
return
}
err = syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0)
if err != nil {
return
}
return
}
//go:build windows
// +build windows
package utils
import (
"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
}
// RedirectStderr to the file passed in
func RedirectStderr() (err error) {
logFile, err := os.OpenFile("./error.log", os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
if err != nil {
return
}
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(logFile.Fd()))
if err != nil {
return
}
// SetStdHandle does not affect prior references to stderr
os.Stderr = logFile
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment