-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
56 lines (51 loc) · 1.33 KB
/
base.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
package xop
import (
"github.com/xoplog/xop-go/xopbase"
)
type loggers struct {
Flushers baseRequests
List baseLoggers
Removed baseLoggers
Added baseLoggers
}
// Copy copies everything but Added & Removed (keepHistory = false).
// List, is a deep-ish copy.
func (s loggers) Copy(keepHistory bool) loggers {
list := make(baseLoggers, len(s.List))
copy(list, s.List)
n := loggers{
Flushers: s.Flushers,
List: list,
}
if keepHistory {
if len(s.Added) != 0 {
n.Added = make(baseLoggers, len(s.Added))
copy(n.Added, s.Added)
}
if len(s.Removed) != 0 {
n.Removed = make(baseLoggers, len(s.Removed))
copy(n.Removed, s.Removed)
}
}
return n
}
func WithoutBase(baseLoggerToRemove xopbase.Logger) SeedModifier {
return func(s *Seed) {
for i, baseLogger := range s.loggers.List {
if baseLogger == baseLoggerToRemove {
s.loggers.Removed = append(s.loggers.Removed, baseLogger)
if i < len(s.loggers.List)-1 {
s.loggers.List[i], s.loggers.List[len(s.loggers.List)-1] =
s.loggers.List[len(s.loggers.List)-1], s.loggers.List[i]
}
s.loggers.List = s.loggers.List[:len(s.loggers.List)-1]
}
}
}
}
func WithBase(baseLogger xopbase.Logger) SeedModifier {
return func(s *Seed) {
s.loggers.List = append(s.loggers.List, baseLogger)
s.loggers.Added = append(s.loggers.Added, baseLogger)
}
}