github.com/iDigitalFlame/xmt@v0.5.4/util/bugtrack/bugtrack.go (about) 1 //go:build bugs 2 // +build bugs 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 // Package bugtrack enables the bug tracking system, which is comprised of a 21 // global logger that will write to Standard Error and on the filesystem in a 22 // temporary directory, "$TEMP" in *nix and "%TEMP%" on Windows, that is named 23 // "bugtrack-<PID>.log". 24 // 25 // To enable bug tracking, use the "bugs" build tag. 26 package bugtrack 27 28 import ( 29 "os" 30 "path/filepath" 31 "runtime/debug" 32 "time" 33 34 "github.com/PurpleSec/logx" 35 "github.com/iDigitalFlame/xmt/util" 36 ) 37 38 // Enabled is the stats of the bugtrack package. 39 // 40 // This is true if bug tracking is enabled. 41 const Enabled = true 42 43 var log = bugInit() 44 45 // Recover is a "guard" function to be used to gracefully shut down a program 46 // when a panic is detected. 47 // 48 // Can be en enabled by using: 49 // 50 // if bugtrack.Enabled { 51 // defer bugtrack.Recover("thread-name") 52 // } 53 // 54 // The specified name will be entered into the bugtrack log and a stack trace 55 // will be generated before gracefully returning execution to the program. 56 func Recover(v string) { 57 if r := recover(); r != nil { 58 log.Error("Recovered %s: [%s]", v, r) 59 log.Error("Trace: %s", debug.Stack()) 60 time.Sleep(time.Minute) 61 } 62 } 63 func bugInit() logx.Log { 64 var ( 65 p = os.TempDir() 66 err = os.MkdirAll(p, 0755) 67 ) 68 if err != nil { 69 panic("bugtrack: init failed with error: " + err.Error()) 70 } 71 var ( 72 f = filepath.Join(p, "bugtrack-"+util.Uitoa(uint64(os.Getpid()))+".log") 73 l logx.Log 74 ) 75 if l, err = logx.File(f, logx.Append, logx.Trace); err != nil { 76 panic("bugtrack: creating file log failed with error: " + err.Error()) 77 } 78 r := logx.Multiple(l, logx.Writer(os.Stderr, logx.Trace)) 79 r.SetPrefix("BUGTRACK") 80 r.Info(`Bugtrack log init complete! Log file located at "%s".`, f) 81 return r 82 } 83 84 // Track is a simple logging function that takes the same arguments as a 85 // 'fmt.Sprintf' function. This can be used to track bugs or output values. 86 // 87 // Not recommended to be used in production environments. 88 // 89 // The "-tags bugs" option is required in order for this function to be used. 90 func Track(s string, m ...interface{}) { 91 log.Trace(s, m...) 92 }