gottdad/log.go

63 lines
1.3 KiB
Go
Raw Permalink Normal View History

2020-06-14 16:27:57 +02:00
package main
import (
"fmt"
"log"
"path/filepath"
"runtime"
)
const (
logDebug uint = iota
logWarn
logAlert
)
func failError(err error, format string, a ...interface{}) {
if err != nil {
_, fn, line, _ := runtime.Caller(1)
out := fmt.Sprintf(format, a...)
log.Fatalf("[%s:%d] %s : %s", filepath.Base(fn), line, out, err)
}
}
func logError(err error, lvl uint, format string, a ...interface{}) {
2020-06-14 16:30:29 +02:00
if err != nil && lvl >= 0 {
2020-06-14 16:27:57 +02:00
_, fn, line, _ := runtime.Caller(2)
out := fmt.Sprintf(format, a...)
log.Printf("[%s:%d] %s : %s", filepath.Base(fn), line, out, err)
}
}
func logInfo(lvl uint, format string, a ...interface{}) {
2020-06-14 16:30:29 +02:00
if lvl >= 0 {
2020-06-14 16:27:57 +02:00
_, fn, line, _ := runtime.Caller(2)
out := fmt.Sprintf(format, a...)
log.Printf("[%s:%d] %s", filepath.Base(fn), line, out)
}
}
func logInfoDebug(format string, a ...interface{}) {
logInfo(logDebug, format, a...)
}
func logInfoWarn(format string, a ...interface{}) {
logInfo(logWarn, format, a...)
}
func logInfoAlert(format string, a ...interface{}) {
logInfo(logAlert, format, a...)
}
func logErrorDebug(err error, format string, a ...interface{}) {
logError(err, logDebug, format, a...)
}
func logErrorWarn(err error, format string, a ...interface{}) {
logError(err, logWarn, format, a...)
}
func logErrorAlert(err error, format string, a ...interface{}) {
logError(err, logAlert, format, a...)
}