-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.go
112 lines (89 loc) · 2.48 KB
/
feedback.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
package feedback
import (
"fmt"
"github.com/code-game-project/cli-utils/cli"
)
const FeedbackPkg = Package("feedback")
type Package string
type Severity int
const (
SeverityDebug Severity = iota
SeverityInfo
SeverityWarn
SeverityError
SeverityFatal
SeverityNone
)
type ProgressCallback func(pkg Package, key, message string, current, total int64, unit cli.Unit)
type FeedbackReceiver interface {
Log(pkg Package, severity Severity, message string)
Progress(pkg Package, process, message string, current, total int64, unit cli.Unit)
}
var (
feedbackReceiver FeedbackReceiver
enabled bool
disabledLogPackages map[Package]int = make(map[Package]int)
interceptProgress map[Package]ProgressCallback = make(map[Package]ProgressCallback)
)
func Progress(pkg Package, process, message string, current, total int64, unit cli.Unit) {
if !enabled {
return
}
if pr, ok := interceptProgress[pkg]; ok {
if pr != nil {
pr(pkg, process, message, current, total, unit)
}
} else {
feedbackReceiver.Progress(pkg, process, message, current, total, unit)
}
}
func Debug(pkg Package, msgFormat string, msgArgs ...any) {
Log(pkg, SeverityDebug, msgFormat, msgArgs...)
}
func Info(pkg Package, msgFormat string, msgArgs ...any) {
Log(pkg, SeverityInfo, msgFormat, msgArgs...)
}
func Warn(pkg Package, msgFormat string, msgArgs ...any) {
Log(pkg, SeverityWarn, msgFormat, msgArgs...)
}
func Error(pkg Package, msgFormat string, msgArgs ...any) {
Log(pkg, SeverityError, msgFormat, msgArgs...)
}
func Fatal(pkg Package, msgFormat string, msgArgs ...any) {
Log(pkg, SeverityFatal, msgFormat, msgArgs...)
}
func Log(pkg Package, severity Severity, msgFormat string, msgArgs ...any) {
if !enabled || (severity != SeverityDebug && disabledLogPackages[pkg] > 0) {
return
}
feedbackReceiver.Log(pkg, severity, fmt.Sprintf(msgFormat, msgArgs...))
}
func Enable(receiver FeedbackReceiver) {
if receiver != nil {
enabled = true
}
feedbackReceiver = receiver
}
func Disable() {
enabled = false
}
func Reenable() {
if feedbackReceiver != nil {
enabled = true
}
}
func DisableLog(pkg Package) {
disabledLogPackages[pkg] += 1
}
func ReenableLog(pkg Package) {
disabledLogPackages[pkg] -= 1
if disabledLogPackages[pkg] == 0 {
delete(disabledLogPackages, pkg)
}
}
func InterceptProgress(target Package, progressCallback ProgressCallback) {
interceptProgress[target] = progressCallback
}
func UninterceptProgress(target Package) {
delete(interceptProgress, target)
}