This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
logging.go
169 lines (132 loc) · 4.01 KB
/
logging.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/ztrue/tracerr"
)
var logType = map[int]string{
0: RESETCOL + textCol.Green + "[^]" + RESETCOL,
1: RESETCOL + textCol.Cyan + "[.]" + RESETCOL,
2: RESETCOL + textCol.Blue + "[#]" + RESETCOL,
3: RESETCOL + textCol.Yellow + "[*]" + RESETCOL,
4: RESETCOL + textCol.Red + "[!]" + RESETCOL,
5: RESETCOL + textCol.Violet + "[+]" + RESETCOL,
6: RESETCOL + textCol.Cyan + "[?]" + RESETCOL,
}
// Chapter log, used to organize logs into sections.
func chapLog(prefix string, colorInput string, msg string, params ...interface{}) {
var color string
colors := map[int]string{
2: textCol.Violet,
3: textCol.Blue,
4: textCol.Cyan,
5: textCol.Cyan,
}
if colorInput != "" { // Check if color is specified
color = colorInput
} else { // Pick color based on prefix length
color = colors[len(prefix)]
}
rawLogf("\n"+RESETCOL+color+bolden(prefix+RESETCOL+textFx.Bold+" %s\n"), fmt.Sprintf(msg, params...))
}
// Adds a tab to the beginning of each line and prints it.
func indent(text string) {
for _, line := range strings.Split(text, "\n") {
rawLog(" " + line + "\n")
}
}
// Logs a message and formats it.
func rawLogf(msg string, params ...interface{}) {
fmt.Printf(msg, params...) // nolint:forbidigo
}
// Logs a message.
func rawLog(msg string) {
fmt.Print(msg) // nolint:forbidigo
}
// Logs a message with a specified prefix.
func log(logTypeInput int, msg string, params ...interface{}) {
rawLogf(logType[logTypeInput]+(" %s\n"), fmt.Sprintf(msg, params...))
}
// Logs a message without a newline.
func logNoNewline(logTypeInput int, msg string, params ...interface{}) {
rawLogf(logType[logTypeInput]+(" %s"), fmt.Sprintf(msg, params...))
}
// Logs a message with an extra newline before.
func logNewlineBefore(logTypeInput int, msg string, params ...interface{}) {
rawLogf("\n"+logType[logTypeInput]+(" %s\n"), fmt.Sprintf(msg, params...))
}
// Checks if err is not nil and if so suspend the program.
func errorLog(err error, msg string, params ...interface{}) {
if err != nil {
msg := fmt.Sprintf(("%s. Error: %s"), fmt.Sprintf(msg, params...), err.Error())
if force {
log(4, msg)
log(3, "Continuing despite error because force is enabled...")
return
}
chapLog("=>", textCol.Red, "Error")
log(4, msg)
log(4, "Source error log:")
errLog := tracerr.SprintSourceColor(tracerr.Wrap(err), 6)
for _, line := range strings.Split(errLog, "\n\n")[2:] {
rawLog(" " + line + "\n")
}
os.Exit(1)
}
}
// Suspends the program with an error message.
func errorLogRaw(msg string, params ...interface{}) {
errMsg := fmt.Sprintf(("%s."), fmt.Sprintf(msg, params...))
if force {
log(4, errMsg)
log(3, "Continuing despite error because force is enabled...")
return
}
chapLog("=>", textCol.Red, "Error")
log(4, errMsg)
os.Exit(1)
}
// Same as errorLog but adds an extra newline before.
func errorLogNewlineBefore(err error, msg string, params ...interface{}) {
if err != nil {
msg := fmt.Sprintf(("%s. Error: %s"), fmt.Sprintf(msg, params...), err.Error())
if force {
logNewlineBefore(4, msg)
log(3, "Continuing despite error because force is enabled...")
return
}
chapLog("\n=>", textCol.Red, "Error")
logNewlineBefore(4, msg)
log(4, "Source error log:")
errLog := tracerr.SprintSourceColor(tracerr.Wrap(err), 6)
for _, line := range strings.Split(errLog, "\n")[2:] {
rawLog(" " + line + "\n")
}
os.Exit(1)
}
}
// Gets user input.
func input(defVal string, msg string, params ...interface{}) string {
logNoNewline(6, ("%s")+": ", fmt.Sprintf(msg, params...))
if assumeYes {
rawLog("\n")
return defVal
}
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}
// Confirms in a y/n prompt.
func confirm(defVal, msg string, params ...interface{}) {
if !strings.Contains(input(defVal, msg, params...), "y") {
os.Exit(1)
}
}
// Logs if debug is set to true.
func debugLog(msg string, params ...interface{}) {
if debug {
log(5, msg, params...)
}
}