forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_msglog.go
75 lines (52 loc) · 1.21 KB
/
handler_msglog.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
package cellnet
import (
"errors"
"sync"
)
type MsgLogHandler struct {
}
func (self *MsgLogHandler) Call(ev *Event) {
MsgLog(ev)
}
var defaultmsgLogHandler = new(MsgLogHandler)
func StaticMsgLogHandler() EventHandler {
return defaultmsgLogHandler
}
// Msg
// Data, MsgID
func MsgLog(ev *Event) {
if !log.IsDebugEnabled() {
return
}
ev.Parse()
if IsBlockedMessageByID(ev.MsgID) {
return
}
// 需要在收到消息, 不经过decoder时, 就要打印出来, 所以手动解开消息, 有少许耗费
log.Debugf("#%s(%s) sid: %d %s(%d) size: %d | %s", ev.Type.String(), ev.PeerName(), ev.SessionID(), ev.MsgName(), ev.MsgID, ev.MsgSize(), ev.MsgString())
}
var (
msgMetaByID = map[uint32]*MessageMeta{}
msgMetaByIDGuard sync.RWMutex
)
func IsBlockedMessageByID(msgid uint32) bool {
msgMetaByIDGuard.RLock()
defer msgMetaByIDGuard.RUnlock()
if _, ok := msgMetaByID[msgid]; ok {
return true
}
return false
}
var (
ErrMessageNotFound = errors.New("msg not exists")
)
func BlockMessageLog(msgName string) error {
meta := MessageMetaByName(msgName)
if meta == nil {
return ErrMessageNotFound
}
msgMetaByIDGuard.Lock()
msgMetaByID[meta.ID] = meta
msgMetaByIDGuard.Unlock()
return nil
}