diff --git a/libcore/core.go b/libcore/core.go index 2f6db83b..6d78afac 100644 --- a/libcore/core.go +++ b/libcore/core.go @@ -16,15 +16,11 @@ func Unsetenv(key string) error { return os.Unsetenv(key) } -func initCoreDefer() { - device.AllDefer("InitCore", ForceLog) -} - func InitCore(internalAssets string, externalAssets string, prefix string, useOfficial BoolFunc, // extractV2RayAssets cachePath string, process string, //InitCore enableLog bool, maxKB int32, //SetEnableLog ) { - defer initCoreDefer() + defer device.DeferPanicToError("InitCore", func(err error) { ForceLog(fmt.Sprintln(err)) }) isBgProcess := strings.HasSuffix(process, ":bg") @@ -41,12 +37,12 @@ func InitCore(internalAssets string, externalAssets string, prefix string, useOf go ForceLog(s) } else { // not fatal - ForceLog(fmt.Sprintln("Log not inited:", s, err.Error())) + // ForceLog(fmt.Sprintln("Log not inited:", s, err.Error())) } // Set up some component go func() { - defer initCoreDefer() + defer device.DeferPanicToError("InitCore-go", func(err error) { ForceLog(fmt.Sprintln(err)) }) device.GoDebug(process) externalAssetsPath = externalAssets diff --git a/libcore/device/debug.go b/libcore/device/debug.go index 889442c3..016c17fe 100644 --- a/libcore/device/debug.go +++ b/libcore/device/debug.go @@ -13,9 +13,9 @@ func GoDebug(any interface{}) { } } -func AllDefer(name string, log func(string)) { +func DeferPanicToError(name string, err func(error)) { if r := recover(); r != nil { - s := fmt.Sprintln(name+" panic", r, string(debug.Stack())) - log(s) + s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack())) + err(s) } } diff --git a/libcore/speedtest.go b/libcore/speedtest.go index 4bbb38f2..7f9c1c28 100644 --- a/libcore/speedtest.go +++ b/libcore/speedtest.go @@ -3,6 +3,8 @@ package libcore import ( "context" "fmt" + "libcore/device" + "log" "math/rand" "net/http" "strings" @@ -13,6 +15,8 @@ import ( ) func UrlTestV2ray(instance *V2RayInstance, inbound string, link string, timeout int32) (int32, error) { + defer device.DeferPanicToError("UrlTestV2ray", func(err error) { log.Println(err) }) + transport := &http.Transport{ TLSHandshakeTimeout: time.Duration(timeout) * time.Millisecond, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { diff --git a/libcore/v2ray.go b/libcore/v2ray.go index 498099a5..2b54ad0c 100644 --- a/libcore/v2ray.go +++ b/libcore/v2ray.go @@ -3,6 +3,7 @@ package libcore import ( "context" "fmt" + "libcore/device" "log" "strings" "sync" @@ -39,6 +40,8 @@ func NewV2rayInstance() *V2RayInstance { } func (instance *V2RayInstance) LoadConfig(content string) error { + defer device.DeferPanicToError("LoadConfig", func(err error) { log.Println(err) }) + instance.access.Lock() defer instance.access.Unlock() @@ -77,6 +80,8 @@ func (instance *V2RayInstance) LoadConfig(content string) error { } func (instance *V2RayInstance) Start() error { + defer device.DeferPanicToError("Start", func(err error) { log.Println(err) }) + instance.access.Lock() defer instance.access.Unlock() if instance.started { @@ -105,6 +110,8 @@ func (instance *V2RayInstance) QueryStats(tag string, direct string) int64 { } func (instance *V2RayInstance) Close() error { + defer device.DeferPanicToError("Close", func(err error) { log.Println(err) }) + instance.access.Lock() defer instance.access.Unlock() if instance.started {