forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_log.go
102 lines (78 loc) · 2.41 KB
/
file_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package quickfix
import (
"fmt"
"log"
"os"
"path"
"github.com/quickfixgo/quickfix/config"
)
type fileLog struct {
eventLogger *log.Logger
messageLogger *log.Logger
}
func (l fileLog) OnIncoming(msg []byte) {
l.messageLogger.Print(string(msg))
}
func (l fileLog) OnOutgoing(msg []byte) {
l.messageLogger.Print(string(msg))
}
func (l fileLog) OnEvent(msg string) {
l.eventLogger.Print(msg)
}
func (l fileLog) OnEventf(format string, v ...interface{}) {
l.eventLogger.Printf(format, v...)
}
type fileLogFactory struct {
globalLogPath string
sessionLogPaths map[SessionID]string
}
// NewFileLogFactory creates an instance of LogFactory that writes messages and events to file.
// The location of global and session log files is configured via FileLogPath.
func NewFileLogFactory(settings *Settings) (LogFactory, error) {
logFactory := fileLogFactory{}
var err error
if logFactory.globalLogPath, err = settings.GlobalSettings().Setting(config.FileLogPath); err != nil {
return logFactory, err
}
logFactory.sessionLogPaths = make(map[SessionID]string)
for sid, sessionSettings := range settings.SessionSettings() {
logPath, err := sessionSettings.Setting(config.FileLogPath)
if err != nil {
return logFactory, err
}
logFactory.sessionLogPaths[sid] = logPath
}
return logFactory, nil
}
func newFileLog(prefix string, logPath string) (fileLog, error) {
l := fileLog{}
eventLogName := path.Join(logPath, prefix+".event.current.log")
messageLogName := path.Join(logPath, prefix+".messages.current.log")
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
return l, err
}
fileFlags := os.O_RDWR | os.O_CREATE | os.O_APPEND
eventFile, err := os.OpenFile(eventLogName, fileFlags, os.ModePerm)
if err != nil {
return l, err
}
messageFile, err := os.OpenFile(messageLogName, fileFlags, os.ModePerm)
if err != nil {
return l, err
}
logFlag := log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC
l.eventLogger = log.New(eventFile, "", logFlag)
l.messageLogger = log.New(messageFile, "", logFlag)
return l, nil
}
func (f fileLogFactory) Create() (Log, error) {
return newFileLog("GLOBAL", f.globalLogPath)
}
func (f fileLogFactory) CreateSessionLog(sessionID SessionID) (Log, error) {
logPath, ok := f.sessionLogPaths[sessionID]
if !ok {
return nil, fmt.Errorf("logger not defined for %v", sessionID)
}
prefix := SessionIDFilenamePrefix(sessionID)
return newFileLog(prefix, logPath)
}