Skip to content

Commit c2c6d96

Browse files
committed
Improved panic log handling
1 parent 796a463 commit c2c6d96

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

handler/builder.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/arduino/arduino-cli/arduino/libraries"
1111
"github.com/arduino/arduino-cli/executils"
1212
"github.com/arduino/go-paths-helper"
13+
"github.com/bcmi-labs/arduino-language-server/streams"
1314
"github.com/pkg/errors"
1415
)
1516

@@ -21,6 +22,8 @@ func (handler *InoHandler) scheduleRebuildEnvironment() {
2122
}
2223

2324
func (handler *InoHandler) rebuildEnvironmentLoop() {
25+
defer streams.CatchAndLogPanic()
26+
2427
grabDeadline := func() *time.Time {
2528
handler.rebuildSketchDeadlineMutex.Lock()
2629
defer handler.rebuildSketchDeadlineMutex.Unlock()

handler/handler.go

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ func (handler *InoHandler) StopClangd() {
109109

110110
// HandleMessageFromIDE handles a message received from the IDE client (via stdio).
111111
func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
112+
defer streams.CatchAndLogPanic()
113+
112114
needsWriteLock := map[string]bool{
113115
"initialize": true,
114116
"textDocument/didOpen": true,
@@ -1131,6 +1133,8 @@ func (handler *InoHandler) cpp2inoDiagnostics(cppDiags *lsp.PublishDiagnosticsPa
11311133

11321134
// FromClangd handles a message received from clangd.
11331135
func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
1136+
defer streams.CatchAndLogPanic()
1137+
11341138
handler.synchronizer.DataMux.RLock()
11351139
defer handler.synchronizer.DataMux.RUnlock()
11361140

@@ -1238,6 +1242,8 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
12381242
}
12391243

12401244
func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageType, message string) {
1245+
defer streams.CatchAndLogPanic()
1246+
12411247
params := lsp.ShowMessageParams{
12421248
Type: msgType,
12431249
Message: message,

main.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"flag"
5+
"io"
56
"log"
67
"os"
7-
"syscall"
88

99
"github.com/arduino/go-paths-helper"
1010
"github.com/bcmi-labs/arduino-language-server/handler"
@@ -38,22 +38,11 @@ func main() {
3838

3939
if enableLogging {
4040
logfile := streams.OpenLogFileAs("inols-err.log")
41-
// log.SetOutput(io.MultiWriter(logfile, os.Stderr))
42-
defer func() {
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-
48-
logfile.Close()
49-
}()
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-
}
54-
// log.SetOutput(logfile)
41+
log.SetOutput(io.MultiWriter(logfile, os.Stderr))
42+
defer streams.CatchAndLogPanic()
43+
} else {
44+
log.SetOutput(os.Stderr)
5545
}
56-
log.SetOutput(os.Stderr)
5746

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

streams/panics.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package streams
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"runtime/debug"
7+
)
8+
9+
// CatchAndLogPanic will recover a panic, log it on standard logger, and rethrow it
10+
// to continue stack unwinding.
11+
func CatchAndLogPanic() {
12+
if r := recover(); r != nil {
13+
reason := fmt.Sprintf("%v", r)
14+
log.Println(fmt.Sprintf("Panic: %s\n\n%s", reason, string(debug.Stack())))
15+
panic(reason)
16+
}
17+
}

0 commit comments

Comments
 (0)