-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
249 lines (242 loc) · 5.43 KB
/
stream.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2021 - 2023 PurpleSec Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package logx
import (
"errors"
"fmt"
"io"
"os"
)
// DefaultConsole is a pointer to the output that all the console Log structs
// will use when created.
//
// This can be set to any io.Writer. The default is the Stderr console.
var DefaultConsole io.Writer = os.Stderr
type file struct {
stream
f string
}
type stream struct {
*logger
l Level
p Level
}
// Console returns a console logger that uses the Console writer.
func Console(o ...Option) Log {
return Writer(DefaultConsole, o...)
}
func (s *stream) SetLevel(n Level) {
s.l = n
}
func (s *stream) SetPrefix(p string) {
s.logger.SetPrefix(p)
}
func (s *stream) SetPrintLevel(n Level) {
s.p = n
}
func (s *stream) Print(v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Print, 0, "", v...)
return
}
s.Log(s.p, 0, "", v...)
}
func (s *stream) Panic(v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Panic, 0, "", v...)
} else {
s.Log(Panic, 0, "", v...)
}
panic(fmt.Sprintln(v...))
}
func (s *stream) Println(v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Print, 0, "", v...)
return
}
s.Log(s.p, 0, "", v...)
}
func (s *stream) Panicln(v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Panic, 0, "", v...)
} else {
s.Log(Panic, 0, "", v...)
}
panic(fmt.Sprintln(v...))
}
// Writer returns a Log instance based on the Writer 'w' for the logging output
// and allows specifying non-default Logging options.
func Writer(w io.Writer, o ...Option) Log {
var (
f settingFlags = -1
p settingPrefix
l, k = invalidLevel, invalidLevel
)
for i := range o {
if o[i] == nil {
continue
}
switch o[i].setting() {
case setLevel:
l, _ = o[i].(Level)
case setFlags:
f, _ = o[i].(settingFlags)
case setPrint:
k, _ = o[i].(Level)
case setPrefix:
p, _ = o[i].(settingPrefix)
}
}
if f == -1 {
f = settingFlags(DefaultFlags)
}
if l == invalidLevel {
l = Warning
}
if k == invalidLevel {
k = Info
}
return &stream{l: l, p: k, logger: &logger{w: w, p: []byte(p), f: uint8(f)}}
}
// File will attempt to create a File backed Log instance that will write to file
// specified.
//
// This function will truncate the file before starting a new Log if the 'Append'
// option isn't specified.
func File(s string, o ...Option) (Log, error) {
var (
f settingFlags = -1
p settingPrefix
a settingAppend
n = os.O_WRONLY | os.O_CREATE
l, k = invalidLevel, invalidLevel
)
for i := range o {
if o[i] == nil {
continue
}
switch o[i].setting() {
case setLevel:
l, _ = o[i].(Level)
case setFlags:
f, _ = o[i].(settingFlags)
case setPrint:
k, _ = o[i].(Level)
case setAppend:
a, _ = o[i].(settingAppend)
case setPrefix:
p, _ = o[i].(settingPrefix)
}
}
if f == -1 {
f = settingFlags(DefaultFlags)
}
if l == invalidLevel {
l = Warning
}
if k == invalidLevel {
k = Info
}
if a {
n |= os.O_APPEND
}
w, err := os.OpenFile(s, n, 0644)
if err != nil {
return nil, errors.New(`cannot open "` + s + `" for logging: ` + err.Error())
}
return &file{f: s, stream: stream{l: l, p: k, logger: &logger{w: w, p: []byte(p), f: uint8(f)}}}, nil
}
func (s *stream) Info(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Info, 0, m, v...)
return
}
s.Log(Info, 0, m, v...)
}
func (s *stream) Error(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Error, 0, m, v...)
return
}
s.Log(Error, 0, m, v...)
}
func (s *stream) Fatal(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Fatal, 0, m, v...)
} else {
s.Log(Fatal, 0, m, v...)
}
if FatalExits {
os.Exit(1)
}
}
func (s *stream) Trace(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Trace, 0, m, v...)
return
}
s.Log(Trace, 0, m, v...)
}
func (s *stream) Debug(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Debug, 0, m, v...)
return
}
s.Log(Debug, 0, m, v...)
}
func (s *stream) Printf(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Print, 0, m, v...)
return
}
s.Log(s.p, 0, m, v...)
}
func (s *stream) Panicf(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Panic, 0, m, v...)
} else {
s.Log(Panic, 0, m, v...)
}
panic(fmt.Sprintf(m, v...))
}
func (s *stream) Warning(m string, v ...interface{}) {
if s == nil {
Global.(LogWriter).Log(Warning, 0, m, v...)
return
}
s.Log(Warning, 0, m, v...)
}
func (s *stream) Log(l Level, c int, m string, v ...interface{}) {
if l == Print {
// NOTE(dij): Duplicate code here to prevent loops.
if s.l > s.p {
return
}
if len(m) == 0 {
s.Output(3+c, "["+s.p.String()+"]: "+fmt.Sprint(v...))
} else {
s.Output(3+c, "["+s.p.String()+"]: "+fmt.Sprintf(m, v...))
}
return
}
if s.l > l {
return
}
if len(m) == 0 {
s.Output(3+c, "["+l.String()+"]: "+fmt.Sprint(v...))
} else {
s.Output(3+c, "["+l.String()+"]: "+fmt.Sprintf(m, v...))
}
}