github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/node_panic.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  //go:build !arm64
     3  // +build !arm64
     4  
     5  package nodes
     6  
     7  import (
     8  	"bytes"
     9  	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
    10  	"github.com/iwind/TeaGo/Tea"
    11  	"github.com/iwind/TeaGo/logs"
    12  	"os"
    13  	"syscall"
    14  )
    15  
    16  // 处理异常
    17  func (this *Node) handlePanic() {
    18  	// 如果是在前台运行就直接返回
    19  	backgroundEnv, _ := os.LookupEnv("EdgeBackground")
    20  	if backgroundEnv != "on" {
    21  		return
    22  	}
    23  
    24  	var panicFile = Tea.Root + "/logs/panic.log"
    25  
    26  	// 分析panic
    27  	data, err := os.ReadFile(panicFile)
    28  	if err == nil {
    29  		var index = bytes.Index(data, []byte("panic:"))
    30  		if index >= 0 {
    31  			remotelogs.Error("NODE", "系统错误,请上报给开发者: "+string(data[index:]))
    32  		}
    33  	}
    34  
    35  	fp, err := os.OpenFile(panicFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_APPEND, 0777)
    36  	if err != nil {
    37  		logs.Println("NODE", "open 'panic.log' failed: "+err.Error())
    38  		return
    39  	}
    40  	err = syscall.Dup2(int(fp.Fd()), int(os.Stderr.Fd()))
    41  	if err != nil {
    42  		logs.Println("NODE", "write to 'panic.log' failed: "+err.Error())
    43  	}
    44  }
    45