| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package debug |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "io/ioutil" |
| 24 | "os" |
| 25 | "os/user" |
| 26 | "path/filepath" |
| 27 | "runtime" |
| 28 | "runtime/debug" |
| 29 | "time" |
| 30 | |
| 31 | "github.com/sasha-s/go-deadlock" |
| 32 | ) |
| 33 | |
| 34 | var writer io.Writer |
| 35 | var RecoverPrettyPanic bool = true |
| 36 | var DeadlockDetection bool |
| 37 | var WriteLogs bool |
| 38 | var OnRecover func() |
| 39 | var LogDirectory = GetUserDebugDir() |
| 40 | |
| 41 | func GetUserDebugDir() string { |
| 42 | if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { |
| 43 | return filepath.Join(os.TempDir(), "gomuks-"+getUname()) |
| 44 | } |
| 45 | // See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 46 | if xdgStateHome := os.Getenv("XDG_STATE_HOME"); xdgStateHome != "" { |
| 47 | return filepath.Join(xdgStateHome, "gomuks") |
| 48 | } |
| 49 | home := os.Getenv("HOME") |
| 50 | if home == "" { |
| 51 | fmt.Println("XDG_STATE_HOME and HOME are both unset") |
| 52 | os.Exit(1) |
| 53 | } |
| 54 | return filepath.Join(home, ".local", "state", "gomuks") |
| 55 | } |
| 56 | |
| 57 | func getUname() string { |
| 58 | currUser, err := user.Current() |
| 59 | if err != nil { |
| 60 | fmt.Println(err) |
| 61 | os.Exit(1) |
| 62 | } |
| 63 | |
| 64 | return currUser.Username |
| 65 | } |
| 66 | |
| 67 | func Initialize() { |
| 68 | err := os.MkdirAll(LogDirectory, 0750) |
| 69 | if err != nil { |
| 70 | RecoverPrettyPanic = false |
| 71 | DeadlockDetection = false |
| 72 | WriteLogs = false |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | if WriteLogs { |
| 77 | writer, err = os.OpenFile(filepath.Join(LogDirectory, "debug.log"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640) |
| 78 | if err != nil { |
| 79 | panic(err) |
| 80 | } |
| 81 | _, _ = fmt.Fprintf(writer, "======================= Debug init @ %s =======================\n", time.Now().Format("2006-01-02 15:04:05")) |
| 82 | } |
| 83 | |
| 84 | if DeadlockDetection { |
| 85 | deadlocks, err := os.OpenFile(filepath.Join(LogDirectory, "deadlock.log"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640) |
| 86 | if err != nil { |
| 87 | panic(err) |
| 88 | } |
| 89 | deadlock.Opts.LogBuf = deadlocks |
| 90 | deadlock.Opts.OnPotentialDeadlock = func() { |
| 91 | if OnRecover != nil { |
| 92 | OnRecover() |
| 93 | } |
| 94 | _, _ = fmt.Fprintf(os.Stderr, "Potential deadlock detected. See %s/deadlock.log for more information.", LogDirectory) |
| 95 | os.Exit(88) |
| 96 | } |
| 97 | _, err = fmt.Fprintf(deadlocks, "======================= Debug init @ %s =======================\n", time.Now().Format("2006-01-02 15:04:05")) |
| 98 | if err != nil { |
| 99 | panic(err) |
| 100 | } |
| 101 | } else { |
| 102 | deadlock.Opts.Disable = true |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func Printf(text string, args ...interface{}) { |
| 107 | if writer != nil { |
| 108 | _, _ = fmt.Fprintf(writer, time.Now().Format("[2006-01-02 15:04:05] ")) |
| 109 | _, _ = fmt.Fprintf(writer, text+"\n", args...) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func Print(text ...interface{}) { |
| 114 | if writer != nil { |
| 115 | _, _ = fmt.Fprintf(writer, time.Now().Format("[2006-01-02 15:04:05] ")) |
| 116 | _, _ = fmt.Fprintln(writer, text...) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func PrintStack() { |
| 121 | if writer != nil { |
| 122 | _, _ = writer.Write(debug.Stack()) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Recover recovers a panic, runs the OnRecover handler and either re-panics or |
| 127 | // shows an user-friendly message about the panic depending on whether or not |
| 128 | // the pretty panic mode is enabled. |
| 129 | func Recover() { |
| 130 | if p := recover(); p != nil { |
| 131 | if OnRecover != nil { |
| 132 | OnRecover() |
| 133 | } |
| 134 | if RecoverPrettyPanic { |
| 135 | PrettyPanic(p) |
| 136 | } else { |
| 137 | panic(p) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | const Oops = ` __________ |
| 143 | < Oh noes! > |
| 144 | ‾‾‾\‾‾‾‾‾‾ |
| 145 | \ ^__^ |
| 146 | \ (XX)\_______ |
| 147 | (__)\ )\/\ |
| 148 | U ||----W | |
| 149 | || || |
| 150 | |
| 151 | A fatal error has occurred. |
| 152 | |
| 153 | ` |
| 154 | |
| 155 | func PrettyPanic(panic interface{}) { |
| 156 | fmt.Print(Oops) |
| 157 | traceFile := fmt.Sprintf(filepath.Join(LogDirectory, "panic-%s.txt"), time.Now().Format("2006-01-02--15-04-05")) |
| 158 | |
| 159 | var buf bytes.Buffer |
| 160 | _, _ = fmt.Fprintln(&buf, panic) |
| 161 | buf.Write(debug.Stack()) |
| 162 | err := ioutil.WriteFile(traceFile, buf.Bytes(), 0640) |
| 163 | |
| 164 | if err != nil { |
| 165 | fmt.Println("Saving the stack trace to", traceFile, "failed:") |
| 166 | fmt.Println("--------------------------------------------------------------------------------") |
| 167 | fmt.Println(err) |
| 168 | fmt.Println("--------------------------------------------------------------------------------") |
| 169 | fmt.Println("") |
| 170 | fmt.Println("You can file an issue at https://github.com/tulir/gomuks/issues.") |
| 171 | fmt.Println("Please provide the file save error (above) and the stack trace of the original error (below) when filing an issue.") |
| 172 | fmt.Println("") |
| 173 | fmt.Println("--------------------------------------------------------------------------------") |
| 174 | fmt.Println(panic) |
| 175 | debug.PrintStack() |
| 176 | fmt.Println("--------------------------------------------------------------------------------") |
| 177 | } else { |
| 178 | fmt.Println("The stack trace has been saved to", traceFile) |
| 179 | fmt.Println("") |
| 180 | fmt.Println("You can file an issue at https://github.com/tulir/gomuks/issues.") |
| 181 | fmt.Println("Please provide the contents of that file when filing an issue.") |
| 182 | } |
| 183 | os.Exit(1) |
| 184 | } |