forked from gxlog/gxlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slots.go
216 lines (176 loc) · 4.87 KB
/
slots.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
package logger
import (
"fmt"
"reflect"
"github.com/fufuok/gxlog/formatter"
"github.com/fufuok/gxlog/iface"
"github.com/fufuok/gxlog/writer"
)
// The Slot defines the slot type of Logger.
type Slot int
// All available slots here.
const (
Slot0 Slot = iota
Slot1
Slot2
Slot3
Slot4
Slot5
Slot6
Slot7
)
// MaxSlot is the total count of slots.
const MaxSlot = 8
type slotLink struct {
Formatter iface.Formatter
Writer iface.Writer
Level iface.Level
Filter Filter
}
var nullSlotLink = slotLink{
Formatter: formatter.Null(),
Writer: writer.Null(),
Level: iface.Off,
}
// Link sets the formatter and writer to the slot. The opts is used to specify
// the slot Level and/or the slot Filter. An opt MUST be a value of type Level,
// Filter or func(*Record)bool (the underlying type of Filter).
// The formatter and the writer must NOT be nil.
// If the Level of the slot is not specified, Trace is used.
func (log *Logger) Link(slot Slot, formatter iface.Formatter,
writer iface.Writer, opts ...interface{}) {
link := slotLink{
Formatter: formatter,
Writer: writer,
Level: iface.Trace,
}
for _, opt := range opts {
switch opt := opt.(type) {
case iface.Level:
link.Level = opt
case Filter:
link.Filter = opt
case func(*iface.Record) bool:
link.Filter = opt
case nil:
// noop
default:
panic(fmt.Sprintf("logger.Link: unknown link option type: %T", opt))
}
}
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot] = link
log.updateEquivalents()
}
// Unlink sets the Formatter, Writer and Filter of the slot to nil and
// the Level of the slot to Off.
func (log *Logger) Unlink(slot Slot) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot] = nullSlotLink
log.updateEquivalents()
}
// UnlinkAll sets the Formatter, Writer and Filter of all slots to nil and
// the Level of all slots to Off.
func (log *Logger) UnlinkAll() {
log.lock.Lock()
defer log.lock.Unlock()
for i := range log.slots {
log.slots[i] = nullSlotLink
}
log.updateEquivalents()
}
// CopySlot copies the Formatter, Writer, Level and Filter of Slot src
// to Slot dst.
func (log *Logger) CopySlot(dst, src Slot) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[dst] = log.slots[src]
log.updateEquivalents()
}
// MoveSlot copies the Formatter, Writer, Level and Filter of Slot from
// to Slot to, and then unlinks Slot from.
func (log *Logger) MoveSlot(to, from Slot) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[to] = log.slots[from]
log.slots[from] = nullSlotLink
log.updateEquivalents()
}
// SwapSlot swaps the Formatter, Writer, Level and Filter of the slots.
func (log *Logger) SwapSlot(left, right Slot) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[left], log.slots[right] = log.slots[right], log.slots[left]
log.updateEquivalents()
}
// SlotFormatter returns the Formatter of the slot.
func (log *Logger) SlotFormatter(slot Slot) iface.Formatter {
log.lock.Lock()
defer log.lock.Unlock()
return log.slots[slot].Formatter
}
// SetSlotFormatter sets the Formatter of the slot. The formatter must NOT be nil.
func (log *Logger) SetSlotFormatter(slot Slot, formatter iface.Formatter) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot].Formatter = formatter
log.updateEquivalents()
}
// SlotWriter returns the Writer of the slot.
func (log *Logger) SlotWriter(slot Slot) iface.Writer {
log.lock.Lock()
defer log.lock.Unlock()
return log.slots[slot].Writer
}
// SetSlotWriter sets the Writer of the slot. The writer must NOT be nil.
func (log *Logger) SetSlotWriter(slot Slot, writer iface.Writer) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot].Writer = writer
}
// SlotLevel returns the Level of the slot.
func (log *Logger) SlotLevel(slot Slot) iface.Level {
log.lock.Lock()
defer log.lock.Unlock()
return log.slots[slot].Level
}
// SetSlotLevel sets the Level of the slot.
func (log *Logger) SetSlotLevel(slot Slot, level iface.Level) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot].Level = level
}
// SlotFilter returns the Filter of the slot.
func (log *Logger) SlotFilter(slot Slot) Filter {
log.lock.Lock()
defer log.lock.Unlock()
return log.slots[slot].Filter
}
// SetSlotFilter sets the Filter of the slot.
func (log *Logger) SetSlotFilter(slot Slot, filter Filter) {
log.lock.Lock()
defer log.lock.Unlock()
log.slots[slot].Filter = filter
}
func (log *Logger) initSlots() {
for slot := 0; slot < MaxSlot; slot++ {
log.slots = append(log.slots, nullSlotLink)
}
}
func (log *Logger) updateEquivalents() {
for i := 0; i < MaxSlot; i++ {
log.equivalents[i] = log.equivalents[i][:0]
if !reflect.TypeOf(log.slots[i].Formatter).Comparable() {
continue
}
for j := i + 1; j < MaxSlot; j++ {
if !reflect.TypeOf(log.slots[j].Formatter).Comparable() ||
log.slots[i].Formatter != log.slots[j].Formatter {
continue
}
log.equivalents[i] = append(log.equivalents[i], j)
}
}
}