forked from deepfence/YaraHunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
100 lines (83 loc) · 2.1 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
89
90
91
92
93
94
95
96
97
98
99
100
package core
import (
"fmt"
"github.com/fatih/color"
"os"
"regexp"
"strings"
"sync"
)
const (
FATAL = 5
ERROR = 4
IMPORTANT = 3
WARN = 2
INFO = 1
DEBUG = 0
)
var LogColors = map[int]*color.Color{
FATAL: color.New(color.FgRed).Add(color.Bold),
ERROR: color.New(color.FgRed),
IMPORTANT: color.New(color.FgMagenta),
WARN: color.New(color.FgYellow),
INFO: color.New(),
DEBUG: color.New(color.Faint),
}
type Logger struct {
sync.Mutex
debugLevel int
}
func (l *Logger) SetLogLevel(d string) {
l.debugLevel = ERROR
if strings.EqualFold(d, "FATAL") {
l.debugLevel = FATAL
} else if strings.EqualFold(d, "ERROR") {
l.debugLevel = ERROR
} else if strings.EqualFold(d, "IMPORTANT") {
l.debugLevel = IMPORTANT
} else if strings.EqualFold(d, "WARN") {
l.debugLevel = WARN
} else if strings.EqualFold(d, "INFO") {
l.debugLevel = INFO
} else if strings.EqualFold(d, "DEBUG") {
l.debugLevel = DEBUG
}
}
func (l *Logger) Log(level int, format string, args ...interface{}) {
l.Lock()
defer l.Unlock()
if level < l.debugLevel {
return
}
if c, ok := LogColors[level]; ok {
c.Fprintf(os.Stderr, "\r"+format+"\n", args...)
} else {
fmt.Fprintf(os.Stderr, "\r"+format+"\n", args...)
}
if level == FATAL {
panic("Fatal error....")
}
}
func (l *Logger) Fatal(format string, args ...interface{}) {
l.Log(FATAL, format, args...)
}
func (l *Logger) Error(format string, args ...interface{}) {
l.Log(ERROR, format, args...)
}
func (l *Logger) Warn(format string, args ...interface{}) {
l.Log(WARN, format, args...)
}
func (l *Logger) Important(format string, args ...interface{}) {
l.Log(IMPORTANT, format, args...)
}
func (l *Logger) Info(format string, args ...interface{}) {
l.Log(INFO, format, args...)
}
func (l *Logger) Debug(format string, args ...interface{}) {
l.Log(DEBUG, format, args...)
}
func colorStrip(str string) string {
ansi := "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
re := regexp.MustCompile(ansi)
return re.ReplaceAllString(str, "")
}