From 49c7d838408fe9057e8d985b9111ae9c2a60bea5 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 2 Feb 2023 13:42:15 +0000 Subject: [PATCH] fix logging and crash reports on Windows "-H=windowsgui" disables stdout on Windows, and the io.Multiwriter used for logging had stdout as it's first entry, which failed and caused logging and line caching to be skipped. stdout is now removed from the multiwriter in this situation. Other portion of the issue was because crash reports had colons in their names, which Windows doesn't like. Fixes #168. --- exit.go | 26 ++++++++++++++++++++++---- log.go | 6 +++++- tray.go | 5 +++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/exit.go b/exit.go index ddf5709..5154e3d 100644 --- a/exit.go +++ b/exit.go @@ -5,13 +5,12 @@ import ( "html/template" "log" "os" + "os/exec" "path/filepath" "runtime" "runtime/debug" "strings" "time" - - "github.com/pkg/browser" ) // https://gist.github.com/swdunlop/9629168 @@ -43,6 +42,21 @@ func identifyPanic() string { return fmt.Sprintf("pc:%x", pc) } +// OpenFile attempts to open a given file in the appropriate GUI application. +func OpenFile(fpath string) (err error) { + switch PLATFORM { + case "linux": + err = exec.Command("xdg-open", fpath).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", fpath).Start() + case "darwin": + err = exec.Command("open", fpath).Start() + default: + err = fmt.Errorf("unknown os") + } + return +} + // Exit dumps the last 100 lines of output to a crash file in /tmp (or equivalent), and generates a prettier HTML file containing it that is opened in the browser if possible. func Exit(err interface{}) { tmpl, err2 := template.ParseFS(localFS, "html/crash.html", "html/header.html") @@ -63,7 +77,8 @@ func Exit(err interface{}) { if err != nil { data["Err"] = fmt.Sprintf("%s %v", identifyPanic(), err) } - fpath := filepath.Join(temp, "jfa-go-crash-"+time.Now().Local().Format("2006-01-02T15:04:05")) + // Use dashes for time rather than colons for Windows + fpath := filepath.Join(temp, "jfa-go-crash-"+time.Now().Local().Format("2006-01-02T15-04-05")) err2 = os.WriteFile(fpath+".txt", []byte(logCache), 0666) if err2 != nil { log.Fatalf("Failed to write crash dump file: %v", err2) @@ -78,7 +93,10 @@ func Exit(err interface{}) { if err2 != nil { log.Fatalf("Failed to execute template: %v", err2) } - browser.OpenFile(fpath + ".html") + if err := OpenFile(fpath + ".html"); err != nil { + log.Printf("Failed to open browser, trying text file...") + OpenFile(fpath + ".txt") + } if TRAY { QuitTray() } else { diff --git a/log.go b/log.go index e008ded..2d80a38 100644 --- a/log.go +++ b/log.go @@ -27,7 +27,11 @@ func logOutput() (closeFunc func(), err error) { closeFunc = func() {} return } - writers = append(writers, colorStripper{f}) + if PLATFORM == "windows" { + writers = []io.Writer{colorStripper{lineCache}, colorStripper{f}} + } else { + writers = append(writers, colorStripper{f}) + } closeFunc = func() { w.Close() <-wExit diff --git a/tray.go b/tray.go index 117105b..a89f108 100644 --- a/tray.go +++ b/tray.go @@ -1,3 +1,4 @@ +//go:build tray // +build tray package main @@ -9,7 +10,6 @@ import ( "syscall" "github.com/getlantern/systray" - "github.com/skratchdot/open-golang/open" ) var TRAY = true @@ -101,7 +101,8 @@ func onReady() { case <-mRestart.ClickedCh: trayRestart() case <-mOpenLogs.ClickedCh: - open.Start(logPath) + log.Printf("Opening %s\n", logPath) + OpenFile(logPath) case <-mQuit.ClickedCh: systray.Quit() }