This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
227 lines (181 loc) · 5.09 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Author: Liam Stanley <[email protected]>
// Docs: https://marill.liam.sh/
// Repo: https://github.com/lrstanley/marill
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
"github.com/lrstanley/marill/utils"
)
// just setup a global logger, and change output during runtime...
var logf *os.File
var logger *log.Logger
func initLoggerWriter(w io.Writer) {
logger = log.New(w, "", log.Lshortfile|log.LstdFlags)
logger.Println("initializing logger")
}
func initLogger() {
var err error
if conf.out.DebugLog != "" && conf.out.PrintDebug {
logf, err = os.OpenFile(conf.out.DebugLog, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("error opening log file: %s, %v", conf.out.DebugLog, err)
os.Exit(1)
}
initLoggerWriter(io.MultiWriter(logf, os.Stdout))
return
}
if conf.out.DebugLog != "" {
logf, err = os.OpenFile(conf.out.DebugLog, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("error opening log file: %s, %v", conf.out.DebugLog, err)
os.Exit(1)
}
initLoggerWriter(logf)
return
}
if conf.out.PrintDebug {
initLoggerWriter(os.Stdout)
return
}
initLoggerWriter(ioutil.Discard)
}
func closeLogger() {
if logf != nil {
logf.Close()
}
}
// Color represents an ASCII color sequence for use with prettified output
type Color struct {
Name string
ID int
}
var colors = []*Color{
{"c", 0}, {"bold", 1}, {"black", 30}, {"red", 31}, {"green", 32}, {"yellow", 33},
{"blue", 34}, {"magenta", 35}, {"cyan", 36}, {"white", 37}, {"gray", 90},
{"lightred", 91}, {"lightgreen", 92}, {"lightyellow", 93}, {"lightblue", 94},
{"lightmagenta", 95}, {"lightcyan", 96}, {"lightgray", 97},
}
// StripColor strips all color {patterns} from input
func StripColor(format *string) {
for _, clr := range colors {
*format = strings.Replace(*format, "{"+clr.Name+"}", "", -1)
}
}
var reNonASCII = regexp.MustCompile(`\x1b.*?m`)
// StripColorBytes strips all color {patterns} from input (however, in bytes)
func StripColorBytes(format *[]byte) {
*format = reNonASCII.ReplaceAll(*format, []byte("")) //re-apply back to the original format
}
// FmtColor adds (or removes) color output depending on user input
func FmtColor(format *string, shouldStrip bool) {
if shouldStrip {
StripColor(format)
return
}
for _, clr := range colors {
*format = strings.Replace(*format, "{"+clr.Name+"}", "\x1b["+strconv.Itoa(clr.ID)+"m", -1)
}
*format = *format + "\x1b[0;m"
}
// Output is the bare out struct for which stdout messages are passed to
type Output struct {
log *log.Logger
buffer []string
logf *os.File
}
var out = Output{}
func initOutWriter(w ...io.Writer) {
out.log = log.New(io.MultiWriter(w...), "", 0)
}
func initOut(w io.Writer) {
var err error
if conf.out.Log != "" && !conf.out.IgnoreStd {
out.logf, err = os.OpenFile(conf.out.Log, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("error opening log file: %s, %v", conf.out.Log, err)
os.Exit(1)
}
initOutWriter(w, utils.NewFuncWriter(StripColorBytes, out.logf))
return
} else if conf.out.Log != "" {
out.logf, err = os.OpenFile(conf.out.Log, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("error opening log file: %s, %v", conf.out.Log, err)
os.Exit(1)
}
initOutWriter(utils.NewFuncWriter(StripColorBytes, out.logf))
return
}
if !conf.out.IgnoreStd {
initOutWriter(w)
return
}
initOutWriter(ioutil.Discard)
}
func closeOut() {
if out.logf != nil {
out.logf.Close()
}
}
func (o Output) Write(b []byte) (int, error) {
str := fmt.Sprintf("%s", b)
o.AddLog(str)
FmtColor(&str, conf.out.NoColors)
o.log.Print(str)
return len(b), nil
}
// AddLog adds log line to log stack
func (o *Output) AddLog(line string) {
o.buffer = append(o.buffer, line)
}
// Printf interprets []*Color{} escape codes and prints them to stdout
func (o *Output) Printf(format string, a ...interface{}) {
if conf.out.IgnoreStd {
return
}
FmtColor(&format, conf.out.NoColors)
out.log.Printf(format, a...)
o.AddLog(fmt.Sprintf(format, a...))
}
// Println interprets []*Color{} escape codes and prints them to stdout
func (o *Output) Println(a ...interface{}) {
if conf.out.IgnoreStd {
return
}
str := fmt.Sprint(a...)
FmtColor(&str, conf.out.NoColors)
out.log.Print(str)
o.AddLog(str)
}
// Fatalf interprets []*Color{} escape codes and prints them to stdout/logger, and exits
func (o *Output) Fatalf(format string, a ...interface{}) {
// print to regular stdout
if !conf.out.IgnoreStd {
str := fmt.Sprintf(fmt.Sprintf("{bold}{red}error:{c} %s", format), a...)
FmtColor(&str, conf.out.NoColors)
out.log.Print(str)
o.AddLog(str)
}
// strip color from format
StripColor(&format)
logger.Fatalf("error: "+format, a...)
}
// Fatal interprets []*Color{} escape codes and prints them to stdout, and exits
func (o *Output) Fatal(a ...interface{}) {
// print to regular stdout
if !conf.out.IgnoreStd {
str := fmt.Sprintf("{bold}{red}error:{c} %s", fmt.Sprintln(a...))
FmtColor(&str, conf.out.NoColors)
out.log.Print(str)
o.AddLog(str)
}
str := fmt.Sprintln(a...)
logger.Fatal("error: " + str)
}