-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogparser.go
93 lines (86 loc) · 2.47 KB
/
logparser.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
package main
import (
"strings"
"errors"
"time"
"log"
"regexp"
"ark-notify/event"
)
func validateLogLine(logLine string) (error) {
// 50 is a length of timestamp part
if (len(logLine) < 50) {
return errors.New("line is too short")
}
if (string(logLine[0]) != "[") {
return errors.New("wrong character at beginning of timestamp")
}
if (string(logLine[24]) != "]") {
return errors.New("wrong character at ending of timestamp")
}
return nil
}
func parseKillEvent(ae *event.ArkEvent, logLine string) {
r := regexp.MustCompile("(.+ - Lvl \\d+ \\(.+\\)) was killed( by ((an? |)(.+)))?!")
m := r.FindStringSubmatch(logLine)
if len(m) == 0 {
log.Println("parseKillEvent failed: ", logLine)
return
}
ae.Info["Victim"] = m[1];
if m[5] != "" {
ae.Info["Assailant"] = m[5];
}
}
func parseJoinEvent(ae *event.ArkEvent, logLine string) {
r := regexp.MustCompile("(.+) joined this ARK")
m := r.FindStringSubmatch(logLine)
if len(m) == 0 {
log.Println("parseJoinEvent", logLine)
}
ae.Info["Player"] = m[1];
}
func parseLeaveEvent(ae *event.ArkEvent, logLine string) {
r := regexp.MustCompile("(.+) left this ARK")
m := r.FindStringSubmatch(logLine)
if len(m) == 0 {
log.Println("parseLeaveEvent", logLine)
}
ae.Info["Player"] = m[1];
}
func ParseEventFromLogLine(logLine string) (*event.ArkEvent, error) {
ae := event.ArkEvent{}
ae.Info = make(map[string]string)
var err error
if err = validateLogLine(logLine); err != nil {
return nil, errors.New("log format validation failed. maybe log format was changed?")
}
// get timestamp
log.Println("time.Parse", logLine[1:24])
var ts time.Time
if ts, err = time.Parse("2006.01.02-15.04.05", logLine[1:20]); err != nil { // TODO: support milliseconds
return nil, errors.New("parse timestamp has failed:" + err.Error())
}
ae.Timestamp = ts
logBody := logLine[50:len(logLine)-1]
log.Print("logBody:", logBody)
// detect event kind
if (strings.Contains(logLine, " was killed")) {
ae.Kind = event.KillEvent
parseKillEvent(&ae, logBody)
} else if (strings.Contains(logLine, " Tamed a")) {
ae.Kind = event.TameEvent
} else if (strings.Contains(logLine, " AdminCmd: ")) {
ae.Kind = event.AdminCmdEvent
} else if (strings.Contains(logLine, " joined this ARK")) {
ae.Kind = event.JoinEvent
parseJoinEvent(&ae, logBody)
} else if (strings.Contains(logLine, " left this ARK")) {
ae.Kind = event.LeaveEvent
parseLeaveEvent(&ae, logBody)
} else {
ae.Kind = event.DefaultEvent
}
ae.RawLog = logLine
return &ae, nil
}