Updates before synching with new host-repo.
This commit is contained in:
46
logger.go
Normal file
46
logger.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
logpath string
|
||||
logfile []byte
|
||||
}
|
||||
|
||||
// write to our file, if it exists
|
||||
func (l *Logger) Log(str string) {
|
||||
|
||||
CheckLogPath(l.logpath)
|
||||
|
||||
var msg string
|
||||
msg = str
|
||||
os.WriteFile(l.logpath, []byte(msg), os.ModeAppend)
|
||||
}
|
||||
|
||||
// dump the err into a string, write to our log file, then pass off the error to the log package
|
||||
func (l *Logger) Fatal(err error) {
|
||||
l.Log(fmt.Sprintf("%v\n", err))
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
func NewLogger(logpath string) *Logger {
|
||||
CheckLogPath(logpath)
|
||||
|
||||
return &Logger{
|
||||
logpath: logpath,
|
||||
}
|
||||
}
|
||||
|
||||
// checks for existence of given logpath file, creates if it doesn't
|
||||
func CheckLogPath(logpath string) {
|
||||
_, err := os.Stat(logpath)
|
||||
if os.IsNotExist(err) {
|
||||
os.Create(logpath)
|
||||
} else if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user