core/log/log.go

98 lines
1.9 KiB
Go
Raw Normal View History

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 {
fmtTime string
level uint32
}
func (l *Log) Printf(s string, v ...interface{}) {
if l.level&LogPrint == LogPrint {
l.ColorPrint(ColorGray, fmt.Sprintf(s, v...))
}
}
func (l *Log) Panicf(s string, v ...interface{}) {
panic(fmt.Sprintf(s, v...))
}
func (l *Log) Fatalf(s string, v ...interface{}) {
l.ColorPrint(ColorRed, fmt.Sprintf(s, v...))
os.Exit(1)
}
func (l *Log) Infof(s string, v ...interface{}) {
if l.level&LogInfo == LogInfo {
l.ColorPrint(ColorReset, fmt.Sprintf(s, v...))
}
}
func (l *Log) Eventf(s string, v ...interface{}) {
if l.level&LogEvent == LogEvent {
l.ColorPrint(ColorCyan, fmt.Sprintf(s, v...))
}
}
func (l *Log) Warnf(s string, v ...interface{}) {
if l.level&LogWarn == LogWarn {
l.ColorPrint(ColorYellow, fmt.Sprintf(s, v...))
}
}
func (l *Log) Debugf(s string, v ...interface{}) {
if l.level&LogDebug == LogDebug {
l.ColorPrint(ColorGray, fmt.Sprintf(s, v...))
}
}
func (l *Log) Highlightf(s string, v ...interface{}) {
if l.level&LogHighlight == LogHighlight {
l.ColorPrint(ColorGreen, fmt.Sprintf(s, v...))
}
}
func (l *Log) Errorf(s string, v ...interface{}) {
if l.level&LogHighlight == LogHighlight {
l.ColorPrint(ColorRed, fmt.Sprintf(s, v...))
}
}
func (l *Log) ColorPrint(color string, s string) {
now := time.Now() // get this early.
fmt.Fprintf(os.Stdout, "%s | %s%s%s", now.Format(l.fmtTime), color, s, ColorReset)
}
func (l *Log) SetLevel(level uint32) {
l.level = level
}
func (l *Log) SetTimeFormat(fmt string) {
l.fmtTime = fmt
}