2022-07-29 06:29:21 +02:00
|
|
|
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2022-01-27 10:35:26 +01:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-02-08 11:04:21 +01:00
|
|
|
"log"
|
2022-01-27 10:35:26 +01:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-07-29 06:29:21 +02:00
|
|
|
// Provides a simple way of logging with different levels.
|
2023-01-23 18:48:06 +01:00
|
|
|
// Time/Date are not logged because systemd adds
|
|
|
|
// them for us (Default, can be changed by flag '--logdate true').
|
2022-07-29 06:29:21 +02:00
|
|
|
//
|
|
|
|
// Uses these prefixes: https://www.freedesktop.org/software/systemd/man/sd-daemon.html
|
|
|
|
|
2022-01-31 15:14:37 +01:00
|
|
|
var (
|
|
|
|
DebugWriter io.Writer = os.Stderr
|
|
|
|
InfoWriter io.Writer = os.Stderr
|
|
|
|
WarnWriter io.Writer = os.Stderr
|
2022-02-08 11:04:21 +01:00
|
|
|
ErrWriter io.Writer = os.Stderr
|
2023-01-23 18:48:06 +01:00
|
|
|
CritWriter io.Writer = os.Stderr
|
2022-01-31 15:14:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-23 18:48:06 +01:00
|
|
|
DebugPrefix string = "<7>[DEBUG] "
|
|
|
|
InfoPrefix string = "<6>[INFO] "
|
|
|
|
WarnPrefix string = "<4>[WARNING] "
|
|
|
|
ErrPrefix string = "<3>[ERROR] "
|
|
|
|
CritPrefix string = "<2>[CRITICAL] "
|
2022-02-08 11:04:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-05-09 07:47:54 +02:00
|
|
|
DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
|
|
|
|
InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, log.LstdFlags|log.Lshortfile)
|
|
|
|
WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile)
|
|
|
|
ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile)
|
|
|
|
CritLog *log.Logger = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile)
|
2022-01-31 15:14:37 +01:00
|
|
|
)
|
2022-01-27 10:35:26 +01:00
|
|
|
|
2023-01-23 18:48:06 +01:00
|
|
|
/* CONFIG */
|
|
|
|
|
2023-02-09 14:14:58 +01:00
|
|
|
func Init(lvl string, logdate bool) {
|
2023-06-01 10:12:45 +02:00
|
|
|
|
2023-01-23 18:48:06 +01:00
|
|
|
switch lvl {
|
2023-02-09 14:14:58 +01:00
|
|
|
case "crit":
|
|
|
|
ErrWriter = io.Discard
|
|
|
|
fallthrough
|
|
|
|
case "err", "fatal":
|
|
|
|
WarnWriter = io.Discard
|
|
|
|
fallthrough
|
|
|
|
case "warn":
|
|
|
|
InfoWriter = io.Discard
|
|
|
|
fallthrough
|
|
|
|
case "info":
|
|
|
|
DebugWriter = io.Discard
|
|
|
|
case "debug":
|
|
|
|
// Nothing to do...
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Printf("pkg/log: Flag 'loglevel' has invalid value %#v\npkg/log: Will use default loglevel 'debug'\n", lvl)
|
|
|
|
//SetLogLevel("debug")
|
|
|
|
}
|
|
|
|
|
2023-02-09 15:43:38 +01:00
|
|
|
if !logdate {
|
2023-02-09 14:14:58 +01:00
|
|
|
DebugLog = log.New(DebugWriter, DebugPrefix, 0)
|
2023-02-15 11:50:51 +01:00
|
|
|
InfoLog = log.New(InfoWriter, InfoPrefix, log.Lshortfile)
|
2023-02-09 14:14:58 +01:00
|
|
|
WarnLog = log.New(WarnWriter, WarnPrefix, log.Lshortfile)
|
|
|
|
ErrLog = log.New(ErrWriter, ErrPrefix, log.Llongfile)
|
|
|
|
CritLog = log.New(CritWriter, CritPrefix, log.Llongfile)
|
2023-06-01 10:12:45 +02:00
|
|
|
} else {
|
|
|
|
DebugLog = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
|
|
|
|
InfoLog = log.New(InfoWriter, InfoPrefix, log.LstdFlags|log.Lshortfile)
|
|
|
|
WarnLog = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile)
|
|
|
|
ErrLog = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile)
|
|
|
|
CritLog = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile)
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 18:48:06 +01:00
|
|
|
/* PRINT */
|
|
|
|
|
|
|
|
// Private helper
|
|
|
|
func printStr(v ...interface{}) string {
|
|
|
|
return fmt.Sprint(v...)
|
|
|
|
}
|
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Uses Info() -> If errorpath required at some point:
|
|
|
|
// Will need own writer with 'Output(2, out)' to correctly render path
|
2023-01-23 18:48:06 +01:00
|
|
|
func Print(v ...interface{}) {
|
|
|
|
Info(v...)
|
|
|
|
}
|
|
|
|
|
2022-01-27 10:35:26 +01:00
|
|
|
func Debug(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
DebugLog.Output(2, printStr(v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Info(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
InfoLog.Output(2, printStr(v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
WarnLog.Output(2, printStr(v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Error(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
ErrLog.Output(2, printStr(v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Writes panic stacktrace, but keeps application alive
|
2023-01-23 18:48:06 +01:00
|
|
|
func Panic(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
ErrLog.Output(2, printStr(v...))
|
2023-01-23 18:48:06 +01:00
|
|
|
panic("Panic triggered ...")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Crit(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
CritLog.Output(2, printStr(v...))
|
2023-01-23 18:48:06 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Writes critical log, stops application
|
|
|
|
func Fatal(v ...interface{}) {
|
2023-02-09 14:14:58 +01:00
|
|
|
CritLog.Output(2, printStr(v...))
|
2023-01-24 12:02:29 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-01-23 18:48:06 +01:00
|
|
|
/* PRINT FORMAT*/
|
|
|
|
|
|
|
|
// Private helper
|
2023-02-09 16:24:29 +01:00
|
|
|
func printfStr(format string, v ...interface{}) string {
|
|
|
|
return fmt.Sprintf(format, v...)
|
|
|
|
}
|
2023-01-23 18:48:06 +01:00
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Uses Infof() -> If errorpath required at some point:
|
|
|
|
// Will need own writer with 'Output(2, out)' to correctly render path
|
2023-01-23 18:48:06 +01:00
|
|
|
func Printf(format string, v ...interface{}) {
|
|
|
|
Infof(format, v...)
|
|
|
|
}
|
|
|
|
|
2022-01-27 10:35:26 +01:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
DebugLog.Output(2, printfStr(format, v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
InfoLog.Output(2, printfStr(format, v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warnf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
WarnLog.Output(2, printfStr(format, v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
ErrLog.Output(2, printfStr(format, v...))
|
2022-01-27 10:35:26 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Writes panic stacktrace, but keeps application alive
|
2023-01-23 18:48:06 +01:00
|
|
|
func Panicf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
ErrLog.Output(2, printfStr(format, v...))
|
2023-01-23 18:48:06 +01:00
|
|
|
panic("Panic triggered ...")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Critf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
CritLog.Output(2, printfStr(format, v...))
|
2023-01-23 18:48:06 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 12:02:29 +01:00
|
|
|
// Writes crit log, stops application
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
2023-02-09 16:24:29 +01:00
|
|
|
CritLog.Output(2, printfStr(format, v...))
|
2023-01-24 12:02:29 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-01-23 18:48:06 +01:00
|
|
|
/* SPECIAL */
|
|
|
|
|
2023-02-09 14:14:58 +01:00
|
|
|
// func Finfof(w io.Writer, format string, v ...interface{}) {
|
|
|
|
// if w != io.Discard {
|
|
|
|
// if logDateTime {
|
|
|
|
// currentTime := time.Now()
|
|
|
|
// fmt.Fprintf(InfoWriter, currentTime.String()+InfoPrefix+format+"\n", v...)
|
|
|
|
// } else {
|
|
|
|
// fmt.Fprintf(InfoWriter, InfoPrefix+format+"\n", v...)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|