-
Notifications
You must be signed in to change notification settings - Fork 14
/
log.go
142 lines (125 loc) · 2.69 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
package main
import (
"errors"
"fmt"
"io"
"strings"
"github.com/gizak/termui/v3/widgets"
"github.com/sirupsen/logrus"
)
var (
errZeroMeterSize = errors.New("zero meter size")
)
func infof(format string, args ...any) {
if notui {
logrus.Infof(format, args...)
} else {
sc.infof(format, args...)
}
}
func (s *screen) infof(format string, args ...any) {
s.Lock()
defer s.Unlock()
s.logroll.Rows = append(s.logroll.Rows, fmt.Sprintf("[INFO](fg:blue) "+format, args...))
s.logroll.ScrollDown()
}
func warnf(format string, args ...any) {
if notui {
logrus.Warnf(format, args...)
} else {
sc.warnf(format, args...)
}
}
func (s *screen) warnf(format string, args ...any) {
s.Lock()
defer s.Unlock()
s.logroll.Rows = append(s.logroll.Rows, fmt.Sprintf("[WARN](fg:yellow) "+format, args...))
s.logroll.ScrollDown()
}
func errorf(format string, args ...any) {
if notui {
logrus.Errorf(format, args...)
} else {
sc.errorf(format, args...)
}
}
func (s *screen) errorf(format string, args ...any) {
s.Lock()
defer s.Unlock()
s.logroll.Rows = append(s.logroll.Rows, fmt.Sprintf("[ERRO](fg:red) "+format, args...))
s.logroll.ScrollDown()
}
func infoln(args ...any) {
if notui {
logrus.Infoln(args...)
} else {
sc.infoln(args...)
}
}
func (s *screen) infoln(args ...any) {
s.Lock()
defer s.Unlock()
s.logroll.Rows = append(s.logroll.Rows, strings.TrimSuffix(
"[INFO](fg:blue) "+fmt.Sprintln(args...), "\n",
))
s.logroll.ScrollDown()
}
func errorln(args ...any) {
if notui {
logrus.Errorln(args...)
} else {
sc.errorln(args...)
}
}
func (s *screen) errorln(args ...any) {
s.Lock()
defer s.Unlock()
s.logroll.Rows = append(s.logroll.Rows, strings.TrimSuffix(
"[ERRO](fg:red) "+fmt.Sprintln(args...), "\n",
))
s.logroll.ScrollDown()
}
type progressmeter struct {
prefix string
name string
size int
prgs int
lstp int
fptr *widgets.Gauge
io.Writer
}
func newmeter(prefix, name string, size int) (pm progressmeter) {
pm.prefix = prefix
pm.name = name
pm.size = size
if !notui {
pm.fptr = sc.addfile(prefix+" "+name, size)
}
return
}
func (pm *progressmeter) Write(p []byte) (n int, err error) {
if pm.size == 0 {
return 0, errZeroMeterSize
}
pm.prgs += len(p)
if !notui {
sc.logwrite(len(p))
}
percent := pm.prgs * 100 / pm.size
if percent == pm.lstp {
return len(p), nil
}
if notui {
logrus.Infof("%s [%2d%%] %s\t(%dMB/%dMB)", pm.prefix, percent, pm.name, pm.prgs/1024/1024, pm.size/1024/1024)
} else {
pm.fptr.Percent = percent
pm.fptr.Label = fmt.Sprintf("%d%% (%dMB/%dMB)", percent, pm.prgs/1024/1024, pm.size/1024/1024)
}
pm.lstp = percent
return len(p), nil
}
func (pm *progressmeter) finish() {
if !notui {
sc.removefile(pm.fptr)
}
}