78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
|
|
package logx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
colorReset = "\033[0m"
|
||
|
|
colorBlue = "\033[38;5;39m"
|
||
|
|
colorCyan = "\033[38;5;45m"
|
||
|
|
colorGreen = "\033[38;5;42m"
|
||
|
|
colorYellow = "\033[38;5;220m"
|
||
|
|
colorRed = "\033[38;5;196m"
|
||
|
|
colorGray = "\033[38;5;244m"
|
||
|
|
colorPink = "\033[38;5;213m"
|
||
|
|
)
|
||
|
|
|
||
|
|
type style struct {
|
||
|
|
emoji string
|
||
|
|
label string
|
||
|
|
color string
|
||
|
|
}
|
||
|
|
|
||
|
|
var (
|
||
|
|
mu = sync.Mutex{}
|
||
|
|
|
||
|
|
styles = map[string]style{
|
||
|
|
"start": {emoji: "🚀", label: "START", color: colorBlue},
|
||
|
|
"queue": {emoji: "📥", label: "QUEUE", color: colorCyan},
|
||
|
|
"visit": {emoji: "🌐", label: "VISIT", color: colorBlue},
|
||
|
|
"recv": {emoji: "📦", label: "RECV", color: colorCyan},
|
||
|
|
"parsed": {emoji: "🧠", label: "PARSED", color: colorPink},
|
||
|
|
"status": {emoji: "🌀", label: "STATUS", color: colorYellow},
|
||
|
|
"done": {emoji: "✅", label: "DONE", color: colorGreen},
|
||
|
|
"write": {emoji: "💾", label: "WRITE", color: colorBlue},
|
||
|
|
"skip": {emoji: "⏭️", label: "SKIP", color: colorGray},
|
||
|
|
"warn": {emoji: "⚠️", label: "WARN", color: colorYellow},
|
||
|
|
"error": {emoji: "💥", label: "ERROR", color: colorRed},
|
||
|
|
"retry": {emoji: "🔁", label: "RETRY", color: colorYellow},
|
||
|
|
"giveup": {emoji: "🛑", label: "GIVEUP", color: colorRed},
|
||
|
|
"success": {emoji: "🎉", label: "SUCCESS", color: colorGreen},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func Eventf(kind, format string, args ...any) {
|
||
|
|
st, ok := styles[kind]
|
||
|
|
if !ok {
|
||
|
|
st = style{emoji: "•", label: strings.ToUpper(kind), color: colorGray}
|
||
|
|
}
|
||
|
|
|
||
|
|
write(st, fmt.Sprintf(format, args...))
|
||
|
|
}
|
||
|
|
|
||
|
|
func Statusf(frame, format string, args ...any) {
|
||
|
|
st := styles["status"]
|
||
|
|
st.emoji = frame
|
||
|
|
write(st, fmt.Sprintf(format, args...))
|
||
|
|
}
|
||
|
|
|
||
|
|
func write(st style, message string) {
|
||
|
|
mu.Lock()
|
||
|
|
defer mu.Unlock()
|
||
|
|
|
||
|
|
timestamp := time.Now().Format("15:04:05")
|
||
|
|
fmt.Printf(
|
||
|
|
"%s[%s] %s %-7s %s%s\n",
|
||
|
|
st.color,
|
||
|
|
timestamp,
|
||
|
|
st.emoji,
|
||
|
|
st.label,
|
||
|
|
message,
|
||
|
|
colorReset,
|
||
|
|
)
|
||
|
|
}
|