...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package rafttest
16
17 import (
18 "fmt"
19 "strings"
20
21 "go.etcd.io/etcd/raft/v3"
22 )
23
24 type logLevels [6]string
25
26 var lvlNames logLevels = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"}
27
28 type RedirectLogger struct {
29 *strings.Builder
30 Lvl int
31 }
32
33 var _ raft.Logger = (*RedirectLogger)(nil)
34
35 func (l *RedirectLogger) printf(lvl int, format string, args ...interface{}) {
36 if l.Lvl <= lvl {
37 fmt.Fprint(l, lvlNames[lvl], " ")
38 fmt.Fprintf(l, format, args...)
39 if n := len(format); n > 0 && format[n-1] != '\n' {
40 l.WriteByte('\n')
41 }
42 }
43 }
44 func (l *RedirectLogger) print(lvl int, args ...interface{}) {
45 if l.Lvl <= lvl {
46 fmt.Fprint(l, lvlNames[lvl], " ")
47 fmt.Fprintln(l, args...)
48 }
49 }
50
51 func (l *RedirectLogger) Debug(v ...interface{}) {
52 l.print(0, v...)
53 }
54
55 func (l *RedirectLogger) Debugf(format string, v ...interface{}) {
56 l.printf(0, format, v...)
57 }
58
59 func (l *RedirectLogger) Info(v ...interface{}) {
60 l.print(1, v...)
61 }
62
63 func (l *RedirectLogger) Infof(format string, v ...interface{}) {
64 l.printf(1, format, v...)
65 }
66
67 func (l *RedirectLogger) Warning(v ...interface{}) {
68 l.print(2, v...)
69 }
70
71 func (l *RedirectLogger) Warningf(format string, v ...interface{}) {
72 l.printf(2, format, v...)
73 }
74
75 func (l *RedirectLogger) Error(v ...interface{}) {
76 l.print(3, v...)
77 }
78
79 func (l *RedirectLogger) Errorf(format string, v ...interface{}) {
80 l.printf(3, format, v...)
81 }
82
83 func (l *RedirectLogger) Fatal(v ...interface{}) {
84 l.print(4, v...)
85 }
86
87 func (l *RedirectLogger) Fatalf(format string, v ...interface{}) {
88
89 l.printf(4, format, v...)
90 }
91
92 func (l *RedirectLogger) Panic(v ...interface{}) {
93 l.print(4, v...)
94 }
95
96 func (l *RedirectLogger) Panicf(format string, v ...interface{}) {
97 l.printf(4, format, v...)
98 }
99
View as plain text