forked from Vencord/Installer
-
Notifications
You must be signed in to change notification settings - Fork 7
/
log.go
88 lines (70 loc) · 1.43 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"github.com/fatih/color"
"os"
"strings"
)
type Level = int
const (
LevelDebug Level = iota
LevelInfo
LevelWarn
LevelError
LevelFatal
)
var levelNames = map[Level]string{
LevelDebug: "DEBUG",
LevelInfo: "INFO",
LevelWarn: "WARN",
LevelError: "ERROR",
LevelFatal: "FATAL",
}
var levelColors = map[Level]*color.Color{
LevelDebug: color.New(color.FgCyan),
LevelInfo: color.New(color.FgBlue),
LevelWarn: color.New(color.FgYellow),
LevelError: color.New(color.FgRed),
LevelFatal: color.New(color.FgHiRed),
}
var Log Handler
var LogLevel = LevelInfo
func init() {
debug := SliceContainsFunc(os.Args, func(s string) bool {
return s == "-debug" || s == "--debug"
})
if debug {
LogLevel = LevelDebug
}
}
type Handler struct {
}
func (h Handler) Log(level Level, a ...any) {
if level < LogLevel {
return
}
levelName := levelNames[level]
var prefix any = levelColors[level].Sprintf(levelName + strings.Repeat(" ", len("error")-len(levelName)))
_, _ = fmt.Fprintln(os.Stderr, Prepend(a, prefix)...)
}
func (h Handler) Debug(a ...any) {
h.Log(LevelDebug, a...)
}
func (h Handler) Info(a ...any) {
h.Log(LevelInfo, a...)
}
func (h Handler) Warn(a ...any) {
h.Log(LevelWarn, a...)
}
func (h Handler) Error(a ...any) {
h.Log(LevelError, a...)
}
func (h Handler) Fatal(a ...any) {
h.Log(LevelFatal, a...)
os.Exit(1)
}
func (h Handler) FatalIfErr(err error) {
if err != nil {
h.Fatal(err)
}
}