forked from FCAI-Lab/etcd-io_raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction_env_logger.go
138 lines (112 loc) · 3.26 KB
/
interaction_env_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
// Copyright 2019 The etcd Authors
//
// 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 rafttest
import (
"fmt"
"strings"
"go.etcd.io/raft/v3"
)
type logLevels [6]string
var lvlNames logLevels = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"}
type RedirectLogger struct {
*strings.Builder
Lvl int // 0 = DEBUG, 1 = INFO, 2 = WARNING, 3 = ERROR, 4 = FATAL, 5 = NONE
}
var _ raft.Logger = (*RedirectLogger)(nil)
func (l *RedirectLogger) printf(lvl int, format string, args ...interface{}) {
if l.Lvl <= lvl {
fmt.Fprint(l, lvlNames[lvl], " ")
fmt.Fprintf(l, format, args...)
if n := len(format); n > 0 && format[n-1] != '\n' {
l.WriteByte('\n')
}
}
}
func (l *RedirectLogger) print(lvl int, args ...interface{}) {
if l.Lvl <= lvl {
fmt.Fprint(l, lvlNames[lvl], " ")
fmt.Fprintln(l, args...)
}
}
func (l *RedirectLogger) Debug(v ...interface{}) {
l.print(0, v...)
}
func (l *RedirectLogger) Debugf(format string, v ...interface{}) {
l.printf(0, format, v...)
}
func (l *RedirectLogger) Info(v ...interface{}) {
l.print(1, v...)
}
func (l *RedirectLogger) Infof(format string, v ...interface{}) {
l.printf(1, format, v...)
}
func (l *RedirectLogger) Warning(v ...interface{}) {
l.print(2, v...)
}
func (l *RedirectLogger) Warningf(format string, v ...interface{}) {
l.printf(2, format, v...)
}
func (l *RedirectLogger) Error(v ...interface{}) {
l.print(3, v...)
}
func (l *RedirectLogger) Errorf(format string, v ...interface{}) {
l.printf(3, format, v...)
}
func (l *RedirectLogger) Fatal(v ...interface{}) {
l.print(4, v...)
panic(fmt.Sprint(v...))
}
func (l *RedirectLogger) Fatalf(format string, v ...interface{}) {
l.printf(4, format, v...)
panic(fmt.Sprintf(format, v...))
}
func (l *RedirectLogger) Panic(v ...interface{}) {
l.print(4, v...)
panic(fmt.Sprint(v...))
}
func (l *RedirectLogger) Panicf(format string, v ...interface{}) {
l.printf(4, format, v...)
// TODO(pavelkalinnikov): catch the panic gracefully in datadriven package.
// This would allow observing all the intermediate logging while debugging,
// and testing the cases when panic is expected.
panic(fmt.Sprintf(format, v...))
}
// Override StringBuilder write methods to silence them under NONE.
func (l *RedirectLogger) Quiet() bool {
return l.Lvl == len(lvlNames)-1
}
func (l *RedirectLogger) Write(p []byte) (int, error) {
if l.Quiet() {
return 0, nil
}
return l.Builder.Write(p)
}
func (l *RedirectLogger) WriteByte(c byte) error {
if l.Quiet() {
return nil
}
return l.Builder.WriteByte(c)
}
func (l *RedirectLogger) WriteRune(r rune) (int, error) {
if l.Quiet() {
return 0, nil
}
return l.Builder.WriteRune(r)
}
func (l *RedirectLogger) WriteString(s string) (int, error) {
if l.Quiet() {
return 0, nil
}
return l.Builder.WriteString(s)
}