Source file
src/log/log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package log
16
17 import (
18 "fmt"
19 "io"
20 "os"
21 "runtime"
22 "sync"
23 "sync/atomic"
24 "time"
25 )
26
27
28
29
30
31
32
33
34
35
36
37
38 const (
39 Ldate = 1 << iota
40 Ltime
41 Lmicroseconds
42 Llongfile
43 Lshortfile
44 LUTC
45 Lmsgprefix
46 LstdFlags = Ldate | Ltime
47 )
48
49
50
51
52
53 type Logger struct {
54 mu sync.Mutex
55 prefix string
56 flag int
57 out io.Writer
58 buf []byte
59 isDiscard int32
60 }
61
62
63
64
65
66
67 func New(out io.Writer, prefix string, flag int) *Logger {
68 l := &Logger{out: out, prefix: prefix, flag: flag}
69 if out == io.Discard {
70 l.isDiscard = 1
71 }
72 return l
73 }
74
75
76 func (l *Logger) SetOutput(w io.Writer) {
77 l.mu.Lock()
78 defer l.mu.Unlock()
79 l.out = w
80 isDiscard := int32(0)
81 if w == io.Discard {
82 isDiscard = 1
83 }
84 atomic.StoreInt32(&l.isDiscard, isDiscard)
85 }
86
87 var std = New(os.Stderr, "", LstdFlags)
88
89
90 func Default() *Logger { return std }
91
92
93 func itoa(buf *[]byte, i int, wid int) {
94
95 var b [20]byte
96 bp := len(b) - 1
97 for i >= 10 || wid > 1 {
98 wid--
99 q := i / 10
100 b[bp] = byte('0' + i - q*10)
101 bp--
102 i = q
103 }
104
105 b[bp] = byte('0' + i)
106 *buf = append(*buf, b[bp:]...)
107 }
108
109
110
111
112
113
114 func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
115 if l.flag&Lmsgprefix == 0 {
116 *buf = append(*buf, l.prefix...)
117 }
118 if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
119 if l.flag&LUTC != 0 {
120 t = t.UTC()
121 }
122 if l.flag&Ldate != 0 {
123 year, month, day := t.Date()
124 itoa(buf, year, 4)
125 *buf = append(*buf, '/')
126 itoa(buf, int(month), 2)
127 *buf = append(*buf, '/')
128 itoa(buf, day, 2)
129 *buf = append(*buf, ' ')
130 }
131 if l.flag&(Ltime|Lmicroseconds) != 0 {
132 hour, min, sec := t.Clock()
133 itoa(buf, hour, 2)
134 *buf = append(*buf, ':')
135 itoa(buf, min, 2)
136 *buf = append(*buf, ':')
137 itoa(buf, sec, 2)
138 if l.flag&Lmicroseconds != 0 {
139 *buf = append(*buf, '.')
140 itoa(buf, t.Nanosecond()/1e3, 6)
141 }
142 *buf = append(*buf, ' ')
143 }
144 }
145 if l.flag&(Lshortfile|Llongfile) != 0 {
146 if l.flag&Lshortfile != 0 {
147 short := file
148 for i := len(file) - 1; i > 0; i-- {
149 if file[i] == '/' {
150 short = file[i+1:]
151 break
152 }
153 }
154 file = short
155 }
156 *buf = append(*buf, file...)
157 *buf = append(*buf, ':')
158 itoa(buf, line, -1)
159 *buf = append(*buf, ": "...)
160 }
161 if l.flag&Lmsgprefix != 0 {
162 *buf = append(*buf, l.prefix...)
163 }
164 }
165
166
167
168
169
170
171
172 func (l *Logger) Output(calldepth int, s string) error {
173 now := time.Now()
174 var file string
175 var line int
176 l.mu.Lock()
177 defer l.mu.Unlock()
178 if l.flag&(Lshortfile|Llongfile) != 0 {
179
180 l.mu.Unlock()
181 var ok bool
182 _, file, line, ok = runtime.Caller(calldepth)
183 if !ok {
184 file = "???"
185 line = 0
186 }
187 l.mu.Lock()
188 }
189 l.buf = l.buf[:0]
190 l.formatHeader(&l.buf, now, file, line)
191 l.buf = append(l.buf, s...)
192 if len(s) == 0 || s[len(s)-1] != '\n' {
193 l.buf = append(l.buf, '\n')
194 }
195 _, err := l.out.Write(l.buf)
196 return err
197 }
198
199
200
201 func (l *Logger) Printf(format string, v ...any) {
202 if atomic.LoadInt32(&l.isDiscard) != 0 {
203 return
204 }
205 l.Output(2, fmt.Sprintf(format, v...))
206 }
207
208
209
210 func (l *Logger) Print(v ...any) {
211 if atomic.LoadInt32(&l.isDiscard) != 0 {
212 return
213 }
214 l.Output(2, fmt.Sprint(v...))
215 }
216
217
218
219 func (l *Logger) Println(v ...any) {
220 if atomic.LoadInt32(&l.isDiscard) != 0 {
221 return
222 }
223 l.Output(2, fmt.Sprintln(v...))
224 }
225
226
227 func (l *Logger) Fatal(v ...any) {
228 l.Output(2, fmt.Sprint(v...))
229 os.Exit(1)
230 }
231
232
233 func (l *Logger) Fatalf(format string, v ...any) {
234 l.Output(2, fmt.Sprintf(format, v...))
235 os.Exit(1)
236 }
237
238
239 func (l *Logger) Fatalln(v ...any) {
240 l.Output(2, fmt.Sprintln(v...))
241 os.Exit(1)
242 }
243
244
245 func (l *Logger) Panic(v ...any) {
246 s := fmt.Sprint(v...)
247 l.Output(2, s)
248 panic(s)
249 }
250
251
252 func (l *Logger) Panicf(format string, v ...any) {
253 s := fmt.Sprintf(format, v...)
254 l.Output(2, s)
255 panic(s)
256 }
257
258
259 func (l *Logger) Panicln(v ...any) {
260 s := fmt.Sprintln(v...)
261 l.Output(2, s)
262 panic(s)
263 }
264
265
266
267 func (l *Logger) Flags() int {
268 l.mu.Lock()
269 defer l.mu.Unlock()
270 return l.flag
271 }
272
273
274
275 func (l *Logger) SetFlags(flag int) {
276 l.mu.Lock()
277 defer l.mu.Unlock()
278 l.flag = flag
279 }
280
281
282 func (l *Logger) Prefix() string {
283 l.mu.Lock()
284 defer l.mu.Unlock()
285 return l.prefix
286 }
287
288
289 func (l *Logger) SetPrefix(prefix string) {
290 l.mu.Lock()
291 defer l.mu.Unlock()
292 l.prefix = prefix
293 }
294
295
296 func (l *Logger) Writer() io.Writer {
297 l.mu.Lock()
298 defer l.mu.Unlock()
299 return l.out
300 }
301
302
303 func SetOutput(w io.Writer) {
304 std.SetOutput(w)
305 }
306
307
308
309 func Flags() int {
310 return std.Flags()
311 }
312
313
314
315 func SetFlags(flag int) {
316 std.SetFlags(flag)
317 }
318
319
320 func Prefix() string {
321 return std.Prefix()
322 }
323
324
325 func SetPrefix(prefix string) {
326 std.SetPrefix(prefix)
327 }
328
329
330 func Writer() io.Writer {
331 return std.Writer()
332 }
333
334
335
336
337
338 func Print(v ...any) {
339 if atomic.LoadInt32(&std.isDiscard) != 0 {
340 return
341 }
342 std.Output(2, fmt.Sprint(v...))
343 }
344
345
346
347 func Printf(format string, v ...any) {
348 if atomic.LoadInt32(&std.isDiscard) != 0 {
349 return
350 }
351 std.Output(2, fmt.Sprintf(format, v...))
352 }
353
354
355
356 func Println(v ...any) {
357 if atomic.LoadInt32(&std.isDiscard) != 0 {
358 return
359 }
360 std.Output(2, fmt.Sprintln(v...))
361 }
362
363
364 func Fatal(v ...any) {
365 std.Output(2, fmt.Sprint(v...))
366 os.Exit(1)
367 }
368
369
370 func Fatalf(format string, v ...any) {
371 std.Output(2, fmt.Sprintf(format, v...))
372 os.Exit(1)
373 }
374
375
376 func Fatalln(v ...any) {
377 std.Output(2, fmt.Sprintln(v...))
378 os.Exit(1)
379 }
380
381
382 func Panic(v ...any) {
383 s := fmt.Sprint(v...)
384 std.Output(2, s)
385 panic(s)
386 }
387
388
389 func Panicf(format string, v ...any) {
390 s := fmt.Sprintf(format, v...)
391 std.Output(2, s)
392 panic(s)
393 }
394
395
396 func Panicln(v ...any) {
397 s := fmt.Sprintln(v...)
398 std.Output(2, s)
399 panic(s)
400 }
401
402
403
404
405
406
407
408
409 func Output(calldepth int, s string) error {
410 return std.Output(calldepth+1, s)
411 }
412
View as plain text