Skip to content

Commit b675233

Browse files
committed
Log by redirecting stderr, this helps to log panics in goroutines
https://stackoverflow.com/a/34773942/1655275
1 parent bf332d7 commit b675233

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: main.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ func main() {
3838

3939
if enableLogging {
4040
logfile := streams.OpenLogFileAs("inols-err.log")
41+
// log.SetOutput(io.MultiWriter(logfile, os.Stderr))
4142
defer func() {
42-
// In case of panic output the stack trace in the log file before exiting
43-
if r := recover(); r != nil {
44-
log.Println(string(debug.Stack()))
45-
}
43+
// // In case of panic output the stack trace in the log file before exiting
44+
// if r := recover(); r != nil {
45+
// log.Println(string(debug.Stack()))
46+
// }
47+
4648
logfile.Close()
4749
}()
48-
log.SetOutput(io.MultiWriter(logfile, os.Stderr))
50+
err := syscall.Dup2(int(logfile.Fd()), int(os.Stderr.Fd()))
51+
if err != nil {
52+
log.Fatalf("Failed to redirect stderr to file: %v", err)
53+
}
4954
// log.SetOutput(logfile)
50-
} else {
51-
log.SetOutput(os.Stderr)
5255
}
56+
log.SetOutput(os.Stderr)
5357

5458
handler.Setup(cliPath, clangdPath, enableLogging, true)
5559
initialBoard := lsp.Board{Fqbn: initialFqbn, Name: initialBoardName}

0 commit comments

Comments
 (0)