2023-05-19 04:12:21 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ColorReset = "\033[0m"
|
|
|
|
ColorRed = "\033[31m"
|
|
|
|
ColorGreen = "\033[32m"
|
|
|
|
ColorYellow = "\033[33m"
|
|
|
|
ColorCyan = "\033[36m"
|
|
|
|
ColorGray = "\033[2;37m"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogError = 1 << iota
|
|
|
|
LogWarn
|
|
|
|
LogEvent
|
|
|
|
LogInfo
|
|
|
|
LogHighlight
|
|
|
|
LogPrint
|
|
|
|
LogDebug
|
|
|
|
|
|
|
|
LogAll = LogError | LogWarn | LogEvent | LogInfo | LogHighlight | LogPrint | LogDebug
|
|
|
|
)
|
|
|
|
|
|
|
|
type Log struct {
|
2024-04-16 17:09:15 +00:00
|
|
|
fmtTime string
|
|
|
|
level uint32
|
|
|
|
component string
|
|
|
|
printer func(level uint32, component string, msg string)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Printf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogPrint, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Panicf(s string, v ...interface{}) {
|
|
|
|
panic(fmt.Sprintf(s, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Fatalf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogError, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Infof(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogInfo, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Eventf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogEvent, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Warnf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogWarn, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Debugf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogDebug, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Highlightf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogHighlight, s, v...)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Errorf(s string, v ...interface{}) {
|
2024-04-16 17:09:15 +00:00
|
|
|
l.emit(LogError, s, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) emit(level uint32, s string, v ...interface{}) {
|
|
|
|
if l.level&level == level {
|
|
|
|
l.printer(level, l.component, fmt.Sprintf(s, v...))
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) ColorPrint(color string, s string) {
|
|
|
|
now := time.Now() // get this early.
|
2024-04-16 17:09:15 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "%s | %4s | %s%s%s", now.Format(l.fmtTime), color, l.component, s, ColorReset)
|
2023-05-19 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) SetLevel(level uint32) {
|
|
|
|
l.level = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) SetTimeFormat(fmt string) {
|
|
|
|
l.fmtTime = fmt
|
|
|
|
}
|
2024-04-16 17:09:15 +00:00
|
|
|
|
|
|
|
func (l *Log) SetPrinter(printer func(level uint32, component string, msg string)) {
|
|
|
|
l.printer = printer
|
|
|
|
}
|
|
|
|
|
2024-04-16 17:47:14 +00:00
|
|
|
func (l *Log) SetName(name string) {
|
|
|
|
l.component = name
|
|
|
|
}
|
|
|
|
|
2024-04-16 17:09:15 +00:00
|
|
|
func (l *Log) WithName(name string) *Log {
|
|
|
|
return &Log{
|
|
|
|
component: name,
|
|
|
|
level: l.level,
|
|
|
|
fmtTime: l.fmtTime,
|
2024-04-16 17:47:14 +00:00
|
|
|
printer: l.printer,
|
2024-04-16 17:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(name string) *Log {
|
|
|
|
return &Log{
|
|
|
|
component: name,
|
|
|
|
level: LogAll,
|
|
|
|
fmtTime: Default.fmtTime,
|
|
|
|
printer: Default.printer,
|
|
|
|
}
|
|
|
|
}
|