Skip to content

Commit 4c4cc36

Browse files
MHSanaeialireza0
andcommitted
fix crash report
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
1 parent 4e0aca1 commit 4c4cc36

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

xray/log_writer.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@ type LogWriter struct {
1616
}
1717

1818
func (lw *LogWriter) Write(m []byte) (n int, err error) {
19-
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[([^\]]+)\] (.+)$`)
19+
crashRegex := regexp.MustCompile(`(?i)(panic|exception|stack trace|fatal error)`)
20+
2021
// Convert the data to a string
2122
message := strings.TrimSpace(string(m))
23+
24+
// Check if the message contains a crash
25+
if crashRegex.MatchString(message) {
26+
logger.Debug("Core crash detected:\n", message)
27+
lw.lastLine = message
28+
err1 := writeCrachReport(m)
29+
if err1 != nil {
30+
logger.Error("Unable to write crash report:", err1)
31+
}
32+
return len(m), nil
33+
}
34+
35+
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[([^\]]+)\] (.+)$`)
2236
messages := strings.Split(message, "\n")
23-
lw.lastLine = messages[len(messages)-1]
2437

2538
for _, msg := range messages {
2639
matches := regex.FindStringSubmatch(msg)
@@ -42,9 +55,10 @@ func (lw *LogWriter) Write(m []byte) (n int, err error) {
4255
default:
4356
logger.Debug("XRAY: " + msg)
4457
}
58+
lw.lastLine = ""
4559
} else if msg != "" {
4660
logger.Debug("XRAY: " + msg)
47-
return len(m), nil
61+
lw.lastLine = msg
4862
}
4963
}
5064

xray/process.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ func (p *process) Start() (err error) {
226226
if err != nil {
227227
logger.Error("Failure in running xray-core:", err)
228228
p.exitErr = err
229-
p.witeCrachReport(err)
230229
}
231230
}()
232231

@@ -243,7 +242,7 @@ func (p *process) Stop() error {
243242
return p.cmd.Process.Signal(syscall.SIGTERM)
244243
}
245244

246-
func (p *process) witeCrachReport(err error) error {
245+
func writeCrachReport(m []byte) error {
247246
crashReportPath := config.GetBinFolderPath() + "/core_crash_" + time.Now().Format("20060102_150405") + ".log"
248-
return os.WriteFile(crashReportPath, []byte(err.Error()), os.ModePerm)
247+
return os.WriteFile(crashReportPath, m, os.ModePerm)
249248
}

0 commit comments

Comments
 (0)