fix crash report

This commit is contained in:
Alireza Ahmadi
2024-11-25 22:50:50 +01:00
parent f2329c20df
commit 3a7c00fc5f
2 changed files with 17 additions and 6 deletions

View File

@@ -16,13 +16,24 @@ type LogWriter struct {
} }
func (lw *LogWriter) Write(m []byte) (n int, err error) { func (lw *LogWriter) Write(m []byte) (n int, err error) {
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[([^\]]+)\] (.+)$`) crashRegex := regexp.MustCompile(`(?i)(panic|exception|stack trace|fatal error)`)
// Convert the data to a string // Convert the data to a string
message := strings.TrimSpace(string(m)) message := strings.TrimSpace(string(m))
// Check if the message contains a crash
if crashRegex.MatchString(message) {
logger.Debug("Core crash detected:\n", message)
lw.lastLine = message
err1 := writeCrachReport(m)
if err1 != nil {
logger.Error("Unable to write crash report:", err1)
}
return len(m), nil
}
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[([^\]]+)\] (.+)$`)
messages := strings.Split(message, "\n") messages := strings.Split(message, "\n")
lw.lastLine = messages[len(messages)-1]
for _, msg := range messages { for _, msg := range messages {
matches := regex.FindStringSubmatch(msg) matches := regex.FindStringSubmatch(msg)
@@ -44,9 +55,10 @@ func (lw *LogWriter) Write(m []byte) (n int, err error) {
default: default:
logger.Debug("XRAY: " + msg) logger.Debug("XRAY: " + msg)
} }
lw.lastLine = ""
} else if msg != "" { } else if msg != "" {
logger.Debug("XRAY: " + msg) logger.Debug("XRAY: " + msg)
return len(m), nil lw.lastLine = msg
} }
} }

View File

@@ -176,7 +176,6 @@ func (p *process) Start() (err error) {
if err != nil { if err != nil {
logger.Error("Failure in running xray-core: ", err) logger.Error("Failure in running xray-core: ", err)
p.exitErr = err p.exitErr = err
p.witeCrachReport(err)
} }
}() }()
@@ -193,7 +192,7 @@ func (p *process) Stop() error {
return p.cmd.Process.Signal(syscall.SIGTERM) return p.cmd.Process.Signal(syscall.SIGTERM)
} }
func (p *process) witeCrachReport(err error) error { func writeCrachReport(m []byte) error {
crashReportPath := config.GetBinFolderPath() + "/core_crash_" + time.Now().Format("20060102_150405") + ".log" crashReportPath := config.GetBinFolderPath() + "/core_crash_" + time.Now().Format("20060102_150405") + ".log"
return os.WriteFile(crashReportPath, []byte(err.Error()), os.ModePerm) return os.WriteFile(crashReportPath, m, os.ModePerm)
} }