forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
66 lines (52 loc) · 1.19 KB
/
log.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
package log // import "v2ray.com/core/common/log"
import (
"sync"
"v2ray.com/core/common/serial"
)
// Message is the interface for all log messages.
type Message interface {
String() string
}
// Handler is the interface for log handler.
type Handler interface {
Handle(msg Message)
}
// GeneralMessage is a general log message that can contain all kind of content.
type GeneralMessage struct {
Severity Severity
Content interface{}
}
// String implements Message.
func (m *GeneralMessage) String() string {
return serial.Concat("[", m.Severity, "] ", m.Content)
}
// Record writes a message into log stream.
func Record(msg Message) {
logHandler.Handle(msg)
}
var (
logHandler syncHandler
)
// RegisterHandler register a new handler as current log handler. Previous registered handler will be discarded.
func RegisterHandler(handler Handler) {
if handler == nil {
panic("Log handler is nil")
}
logHandler.Set(handler)
}
type syncHandler struct {
sync.RWMutex
Handler
}
func (h *syncHandler) Handle(msg Message) {
h.RLock()
defer h.RUnlock()
if h.Handler != nil {
h.Handler.Handle(msg)
}
}
func (h *syncHandler) Set(handler Handler) {
h.Lock()
defer h.Unlock()
h.Handler = handler
}