This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
/
logger.go
193 lines (160 loc) Β· 3.83 KB
/
logger.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cmd
import (
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
)
var prefix, progressDown, progressDownFinish string
func initPrefixBecauseWindowsSucksBallsHard() {
// dear windows user, please change to a good OS, linux in the best case.
// MICROSHIT DOES NOT GET IT DONE TO SHOW THE SYMBOLS IN THE ELSE CLAUSE
// CORRECTLY. NOT IN THE CMD NOR POWERSHELL. WHY TF, IT IS ONE OF THE MOST
// PROFITABLE COMPANIES ON THIS PLANET AND CANNOT SHOW A PROPER UTF-8 SYMBOL
// IN THEIR OWN PRODUCT WHICH GETS USED MILLION TIMES A DAY
if runtime.GOOS == "windows" {
prefix = "=>"
progressDown = "|"
progressDownFinish = "->"
} else {
prefix = "β"
progressDown = "β"
progressDownFinish = "β³"
}
}
type progress struct {
message string
stop bool
}
type logger struct {
DebugLog *log.Logger
InfoLog *log.Logger
ErrLog *log.Logger
devView bool
progress chan progress
done chan interface{}
lock sync.Mutex
}
func newLogger(debug, info, err bool) *logger {
initPrefixBecauseWindowsSucksBallsHard()
debugLog, infoLog, errLog := log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0)
if debug {
debugLog.SetOutput(os.Stdout)
}
if info {
infoLog.SetOutput(os.Stdout)
}
if err {
errLog.SetOutput(os.Stderr)
}
if debug {
debugLog = log.New(debugLog.Writer(), "[debug] ", 0)
infoLog = log.New(infoLog.Writer(), "[info] ", 0)
errLog = log.New(errLog.Writer(), "[err] ", 0)
}
return &logger{
DebugLog: debugLog,
InfoLog: infoLog,
ErrLog: errLog,
devView: debug,
}
}
func (l *logger) IsDev() bool {
return l.devView
}
func (l *logger) IsQuiet() bool {
return l.DebugLog.Writer() == io.Discard && l.InfoLog.Writer() == io.Discard && l.ErrLog.Writer() == io.Discard
}
func (l *logger) Debug(format string, v ...interface{}) {
l.DebugLog.Printf(format, v...)
}
func (l *logger) Info(format string, v ...interface{}) {
l.InfoLog.Printf(format, v...)
}
func (l *logger) Err(format string, v ...interface{}) {
l.ErrLog.Printf(format, v...)
}
func (l *logger) Exit(format string, v ...interface{}) {
fmt.Fprintln(l.ErrLog.Writer(), fmt.Sprintf(format, v...))
}
func (l *logger) Empty() {
if !l.devView && l.InfoLog.Writer() != io.Discard {
fmt.Println("")
}
}
func (l *logger) SetProgress(format string, v ...interface{}) {
if out.InfoLog.Writer() == io.Discard {
return
} else if l.devView {
l.Debug(format, v...)
return
}
initialMessage := fmt.Sprintf(format, v...)
p := progress{
message: initialMessage,
}
l.lock.Lock()
if l.done != nil {
l.progress <- p
return
} else {
l.progress = make(chan progress, 1)
l.progress <- p
l.done = make(chan interface{})
}
go func() {
states := []string{"-", "\\", "|", "/"}
var count int
for i := 0; ; i++ {
select {
case p := <-l.progress:
if p.stop {
fmt.Printf("\r" + strings.Repeat(" ", len(prefix)+len(initialMessage)))
if count > 1 {
fmt.Printf("\r%s %s\n", progressDownFinish, p.message)
} else {
fmt.Printf("\r%s %s\n", prefix, p.message)
}
if l.done != nil {
l.done <- nil
}
l.progress = nil
l.lock.Unlock()
return
} else {
if count > 0 {
fmt.Printf("\r%s %s\n", progressDown, p.message)
}
l.progress = make(chan progress, 1)
count++
fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
l.lock.Unlock()
}
default:
if i%10 == 0 {
fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
}
time.Sleep(35 * time.Millisecond)
}
}
}()
}
func (l *logger) StopProgress(format string, v ...interface{}) {
if out.InfoLog.Writer() == io.Discard {
return
} else if l.devView {
l.Debug(format, v...)
return
}
l.lock.Lock()
l.progress <- progress{
message: fmt.Sprintf(format, v...),
stop: true,
}
<-l.done
l.done = nil
}