wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
24.52k stars 1.18k forks source link

How to Automatically Write Report to File When App Crashed? #2892

Open KiddoV opened 1 year ago

KiddoV commented 1 year ago

Discussed in https://github.com/wailsapp/wails/discussions/2682

Originally posted by **KiddoV** May 23, 2023 > How do I write a report to, says text (.txt) file, if my complied app (.exe) crash? > > In other words, simply write all logs to a file, when app crashed. Thanks,
agui2200 commented 1 year ago

There is a temporary plan here:

package main

import (
    "log"
    "os"
    "syscall"
)

const (
    kernel32dll = "kernel32.dll"
)

const panicFile = "C:/panic.log"

var globalFile *os.File

func InitPanicFile() error {
    log.Println("init panic file in windows mode")
    file, err := os.OpenFile(panicFile, os.O_CREATE|os.O_APPEND, 0666)
    globalFile = file
    if err != nil {
        return err
    }
    kernel32 := syscall.NewLazyDLL(kernel32dll)
    setStdHandle := kernel32.NewProc("SetStdHandle")
    sh := syscall.STD_ERROR_HANDLE
    v, _, err := setStdHandle.Call(uintptr(sh), uintptr(file.Fd()))
    if v == 0 {
        return err
    }
    return nil
}

func init() {
    err := pc.InitPanicFile()
    if err != nil {
        println(err)
    }
}

func testPanic() {
    panic("test panic")
}

func main() {
    testPanic()
}

The principle is to redirect standard output

KiddoV commented 1 year ago

Will try that, thanks!

stffabi commented 1 year ago

We have used https://github.com/mitchellh/panicwrap in our projects, works quite well but at the expense of spawning a second process.

KiddoV commented 1 year ago

@agui2200 How do I make it only log when ERROR occurs? Right now it would log INF, DEB logs as well.

@stffabi Will give it a try too, thanks!

KiddoV commented 1 year ago

@stffabi can you give me an example of how to use https://github.com/mitchellh/panicwrap with Wails? My app is not running after implemented it:

> wails dev
...
Development mode exited
agui2200 commented 11 months ago

@agui2200 How do I make it only log when ERROR occurs? Right now it would log INF, DEB logs as well.

all panic output can be rewire to C:/panic.log , this code only support win32 platform