增加底层动态链接库异常捕获

This commit is contained in:
杨红岩 2023-11-01 16:54:02 +08:00
parent bf9bb1d034
commit f59c18dfed
8 changed files with 129 additions and 19 deletions

View File

@ -0,0 +1,21 @@
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// Package exception
// Underlying dynamic link library exception capture
// Supports: Windows, MacOS.
package exception
import "github.com/energye/energy/v2/cef/internal/exception"
// SetOnException 设置 liblcl -> CEF 低层异常捕获回调函数
func SetOnException(fn exception.Callback) {
exception.HandlerInit(fn)
}

View File

@ -17,7 +17,6 @@ import (
. "github.com/energye/energy/v2/cef/process"
. "github.com/energye/energy/v2/common"
"github.com/energye/energy/v2/common/imports/tempdll"
"github.com/energye/energy/v2/logger"
"github.com/energye/golcl/energy/inits"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/api"
@ -27,16 +26,6 @@ import (
"runtime"
)
// ExceptionCallback 异常回调函数
type ExceptionCallback func(sender lcl.IObject, e *lcl.Exception)
var exceptionCallback ExceptionCallback
// SetOnException 设置 lib-lcl -> CEF 低层异常捕获回调函数
func SetOnException(exception ExceptionCallback) {
exceptionCallback = exception
}
// GlobalInit 全局初始化
// 需要手动调用的函数,在main函数中调用
// 参数:
@ -90,14 +79,6 @@ func GlobalInit(libs *embed.FS, resources *embed.FS) {
}
// main thread run call
applicationQueueAsyncCallInit()
//应用低层出错异常捕获
lcl.Application.SetOnException(func(sender lcl.IObject, e *lcl.Exception) {
if exceptionCallback != nil {
exceptionCallback(sender, e)
} else {
logger.Error("ResultString:", e.Message())
}
})
}
// v8init v8初始化

View File

@ -23,6 +23,8 @@ package def
const (
//null nil
null_nil = iota
// Exception
SetExceptionHandlerCallback
// TForm
Form_SetOnMessagesEvent
// CEF

View File

@ -23,6 +23,8 @@ func init() {
var energyImportDefs = []*dllimports.ImportTable{
//null nil
dllimports.NewEnergyImport("", 0),
// Exception
dllimports.NewEnergyImport("SetExceptionHandlerCallback", 0),
// TForm
dllimports.NewEnergyImport("Form_SetOnMessagesEvent", 0),
//CEF

View File

@ -0,0 +1,45 @@
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// Package exception
// Underlying dynamic link library exception capture
// Supports: Windows, MacOS.
package exception
import (
"github.com/energye/energy/v2/cef/internal/def"
"github.com/energye/energy/v2/common/imports"
"github.com/energye/golcl/lcl/api"
"github.com/energye/golcl/lcl/api/dllimports"
)
type Callback func(message string)
var (
exceptionHandler dllimports.ProcAddr
exceptionHandlerCallback Callback
)
// HandlerInit
// 底层库异常处理器初始化
func HandlerInit(fn Callback) {
if exceptionHandlerCallback == nil {
exceptionHandler = imports.Proc(def.SetExceptionHandlerCallback)
exceptionHandler.Call(exceptionHandlerProcEventAddr)
exceptionHandlerCallback = fn
}
}
func exceptionHandlerProc(message uintptr) uintptr {
if exceptionHandlerCallback != nil {
exceptionHandlerCallback(api.GoStr(message))
}
return 0
}

View File

@ -0,0 +1,38 @@
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build !windows && cgo
// +build !windows,cgo
package exception
//// #cgo darwin CFLAGS: -mmacosx-version-min=10.8 -DMACOSX_DEPLOYMENT_TARGET=10.8
// #cgo darwin CFLAGS: -mmacosx-version-min=10.10
// #cgo darwin LDFLAGS: -mmacosx-version-min=10.10
//
// extern void* doExceptionHandlerProc(void* f);
// static void* doExceptionHandlerProcEventAddr() {
// return &doExceptionHandlerProc;
// }
import "C"
import (
"unsafe"
)
//export doExceptionHandlerProc
func doExceptionHandlerProc(f unsafe.Pointer) unsafe.Pointer {
exceptionHandlerProc(uintptr(f))
return nil
}
var (
exceptionHandlerProcEventAddr = uintptr(C.doExceptionHandlerProcEventAddr())
)

View File

@ -0,0 +1,17 @@
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package exception
import "syscall"
var (
exceptionHandlerProcEventAddr = syscall.NewCallback(exceptionHandlerProc)
)

View File

@ -5,6 +5,7 @@ import (
"embed"
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/cef/exception"
"github.com/energye/energy/v2/cef/ipc"
"github.com/energye/energy/v2/cef/ipc/context"
"github.com/energye/energy/v2/pkgs/assetserve"
@ -17,6 +18,9 @@ var resources embed.FS
func main() {
//全局初始化 每个应用都必须调用的
cef.GlobalInit(nil, &resources)
exception.SetOnException(func(message string) {
fmt.Println("message", message)
})
//创建应用
cefApp := cef.NewApplication()
//指定一个URL地址或本地html文件目录