forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_log.go
54 lines (43 loc) · 1.35 KB
/
custom_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
package quickfix
import (
"bytes"
"fmt"
)
func makeReadable(s []byte) string {
return string(bytes.Replace(s, []byte("\x01"), []byte("|"), -1))
}
type customLog struct {
sessionPrefix string
logFunc func(msg string, keysAndValues ...interface{})
}
func (l customLog) OnIncoming(s []byte) {
msg := fmt.Sprintf("FIX incoming [%v |%v]", l.sessionPrefix, makeReadable(s))
l.logFunc(msg)
}
func (l customLog) OnOutgoing(s []byte) {
msg := fmt.Sprintf("FIX outgoing [%v - |%v]", l.sessionPrefix, makeReadable(s))
l.logFunc(msg)
}
func (l customLog) OnEvent(s string) {
msg := fmt.Sprintf("FIX event [%v - %v]", l.sessionPrefix, s)
l.logFunc(msg)
}
func (l customLog) OnEventf(format string, a ...interface{}) {
l.OnEvent(fmt.Sprintf(format, a...))
}
type customLogFactory struct {
logFunc func(msg string, keysAndValues ...interface{})
}
func (f customLogFactory) Create() (Log, error) {
log := customLog{"GLOBAL", f.logFunc}
return log, nil
}
func (f customLogFactory) CreateSessionLog(sessionID SessionID) (Log, error) {
log := customLog{sessionID.String(), f.logFunc}
return log, nil
}
// NewCustomLogFactory creates an instance of LogFactory that
// logs messages and events using the provided log function
func NewCustomLogFactory(logFunc func(msg string, keysAndValues ...interface{})) LogFactory {
return customLogFactory{logFunc: logFunc}
}