From 73202e148372103cf1e357be82c42e64a883bbea Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 12 Jun 2023 15:48:07 +0100 Subject: [PATCH] crash: render ANSI colors on crash page Could not for the life of me get any regex to properly strip the ANSI escape sequences, so the text log will have to suffer with them, but the ansihtml library is now used to render them into HTML. --- exit.go | 8 ++++++++ go.mod | 2 ++ go.sum | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/exit.go b/exit.go index 5154e3d..892e69b 100644 --- a/exit.go +++ b/exit.go @@ -11,6 +11,8 @@ import ( "runtime/debug" "strings" "time" + + "github.com/robert-nix/ansihtml" ) // https://gist.github.com/swdunlop/9629168 @@ -84,6 +86,12 @@ func Exit(err interface{}) { log.Fatalf("Failed to write crash dump file: %v", err2) } log.Printf("\n------\nA crash report has been saved to \"%s\".\n------", fpath+".txt") + + // Render ANSI colors to HTML + data["Log"] = template.HTML(string(ansihtml.ConvertToHTML([]byte(data["Log"].(string))))) + data["SanitizedLog"] = template.HTML(string(ansihtml.ConvertToHTML([]byte(data["SanitizedLog"].(string))))) + data["Err"] = template.HTML(string(ansihtml.ConvertToHTML([]byte(data["Err"].(string))))) + f, err2 := os.OpenFile(fpath+".html", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) if err2 != nil { log.Fatalf("Failed to open crash dump file: %v", err2) diff --git a/go.mod b/go.mod index b2ff30e..6f13285 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,9 @@ require ( github.com/mailgun/mailgun-go/v4 v4.9.0 github.com/mattn/go-isatty v0.0.19 // indirect github.com/otiai10/copy v1.7.0 // indirect + github.com/pborman/ansi v1.0.0 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/robert-nix/ansihtml v1.0.1 // indirect github.com/steambap/captcha v1.4.1 github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.0 diff --git a/go.sum b/go.sum index 0099e46..f609f2a 100644 --- a/go.sum +++ b/go.sum @@ -269,6 +269,8 @@ github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT9 github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= +github.com/pborman/ansi v1.0.0 h1:OqjHMhvlSuCCV5JT07yqPuJPQzQl+WXsiZ14gZsqOrQ= +github.com/pborman/ansi v1.0.0/go.mod h1:SgWzwMAx1X/Ez7i90VqF8LRiQtx52pWDiQP+x3iGnzw= github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= @@ -280,6 +282,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robert-nix/ansihtml v1.0.1 h1:VTiyQ6/+AxSJoSSLsMecnkh8i0ZqOEdiRl/odOc64fc= +github.com/robert-nix/ansihtml v1.0.1/go.mod h1:CJwclxYaTPc2RfcxtanEACsYuTksh4yDXcNeHHKZINE= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=