File: multisink.go

package info (click to toggle)
golang-github-hlandau-xlog 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (4)
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
package xlog

// A sink which dispatches to zero or more other sinks.
type MultiSink struct {
	sinks []Sink
}

// Add a sink to the MultiSink. Idempotent.
func (ms *MultiSink) Add(sink Sink) {
	for _, s := range ms.sinks {
		if s == sink {
			return
		}
	}

	ms.sinks = append(ms.sinks, sink)
}

// Remove a sink from the MultiSink. Idempotent.
func (ms *MultiSink) Remove(sink Sink) {
	var newSinks []Sink
	for _, s := range ms.sinks {
		if s != sink {
			newSinks = append(newSinks, s)
		}
	}

	ms.sinks = newSinks
}

// (Implements Sink.)
func (ms *MultiSink) ReceiveLocally(sev Severity, format string, params ...interface{}) {
	for _, s := range ms.sinks {
		s.ReceiveLocally(sev, format, params...)
	}
}

// (Implements Sink.)
func (ms *MultiSink) ReceiveFromChild(sev Severity, format string, params ...interface{}) {
	for _, s := range ms.sinks {
		s.ReceiveFromChild(sev, format, params...)
	}
}

// The null sink. All log messages to this sink will be discarded.
var NullSink Sink

func init() {
	NullSink = &MultiSink{}
}