forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
162 lines (134 loc) · 3.34 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package log
import (
"fmt"
"io"
"io/ioutil"
"log"
"sync"
"time"
"github.com/chzyer/readline"
"github.com/fatih/color"
)
var stdout io.Writer = color.Output
var g_rl *readline.Instance = nil
var debug_output = true
var mtx_log *sync.Mutex = &sync.Mutex{}
const (
DEBUG = iota
INFO
IMPORTANT
WARNING
ERROR
FATAL
SUCCESS
)
var LogLabels = map[int]string{
DEBUG: "dbg",
INFO: "inf",
IMPORTANT: "imp",
WARNING: "war",
ERROR: "err",
FATAL: "!!!",
SUCCESS: "+++",
}
func DebugEnable(enable bool) {
debug_output = enable
}
func SetOutput(o io.Writer) {
stdout = o
}
func SetReadline(rl *readline.Instance) {
g_rl = rl
}
func GetOutput() io.Writer {
return stdout
}
func NullLogger() *log.Logger {
return log.New(ioutil.Discard, "", 0)
}
func refreshReadline() {
if g_rl != nil {
g_rl.Refresh()
}
}
func Debug(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
if debug_output {
fmt.Fprint(stdout, format_msg(DEBUG, format+"\n", args...))
refreshReadline()
}
}
func Info(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(INFO, format+"\n", args...))
refreshReadline()
}
func Important(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(IMPORTANT, format+"\n", args...))
refreshReadline()
}
func Warning(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(WARNING, format+"\n", args...))
refreshReadline()
}
func Error(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(ERROR, format+"\n", args...))
refreshReadline()
}
func Fatal(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(FATAL, format+"\n", args...))
refreshReadline()
}
func Success(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprint(stdout, format_msg(SUCCESS, format+"\n", args...))
refreshReadline()
}
func Printf(format string, args ...interface{}) {
mtx_log.Lock()
defer mtx_log.Unlock()
fmt.Fprintf(stdout, format, args...)
refreshReadline()
}
func format_msg(lvl int, format string, args ...interface{}) string {
t := time.Now()
var sign, msg *color.Color
switch lvl {
case DEBUG:
sign = color.New(color.FgBlack, color.BgHiBlack)
msg = color.New(color.Reset, color.FgHiBlack)
case INFO:
sign = color.New(color.FgGreen, color.BgBlack)
msg = color.New(color.Reset)
case IMPORTANT:
sign = color.New(color.FgWhite, color.BgHiBlue)
//msg = color.New(color.Reset, color.FgHiBlue)
msg = color.New(color.Reset)
case WARNING:
sign = color.New(color.FgBlack, color.BgYellow)
//msg = color.New(color.Reset, color.FgYellow)
msg = color.New(color.Reset)
case ERROR:
sign = color.New(color.FgWhite, color.BgRed)
msg = color.New(color.Reset, color.FgRed)
case FATAL:
sign = color.New(color.FgBlack, color.BgRed)
msg = color.New(color.Reset, color.FgRed, color.Bold)
case SUCCESS:
sign = color.New(color.FgWhite, color.BgGreen)
msg = color.New(color.Reset, color.FgGreen)
}
time_clr := color.New(color.Reset)
return "\r[" + time_clr.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second()) + "] [" + sign.Sprintf("%s", LogLabels[lvl]) + "] " + msg.Sprintf(format, args...)
}