43 lines
710 B
Go
43 lines
710 B
Go
package errs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
var errFS *os.File = nil
|
|
var errFSLock sync.Mutex
|
|
|
|
func init() {
|
|
bn, e := os.Executable()
|
|
if e != nil {
|
|
panic("Can not get executable name")
|
|
}
|
|
ef, e := os.OpenFile(bn+".error", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0665)
|
|
if e != nil {
|
|
panic("Can not open or create error log file")
|
|
}
|
|
err := InitFS(ef)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func InitFS(f *os.File) *Err {
|
|
errFS = f
|
|
errFSLock = sync.Mutex{}
|
|
return nil
|
|
}
|
|
|
|
func storeFS(err *Err) {
|
|
if errFS == nil {
|
|
return
|
|
}
|
|
errFSLock.Lock()
|
|
defer errFSLock.Unlock()
|
|
if _, e := errFS.WriteString(err.getString() + "\n"); e != nil {
|
|
fmt.Fprintln(os.Stderr, "Error write to file: "+e.Error())
|
|
}
|
|
}
|