-
Notifications
You must be signed in to change notification settings - Fork 4
/
journal.go
72 lines (59 loc) · 1.68 KB
/
journal.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
package notifier
import (
"fmt"
"io/fs"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type journalEvent struct {
Timestamp time.Time `json:"timestamp"`
Event eventType `json:"event"`
Health float64 `json:"Health"`
PlayerPilot bool `json:"PlayerPilot"`
Fighter bool `json:"Fighter"`
ShieldsUp bool `json:"ShieldsUp"` // whether shields are up or down
TotalPiratesReward int `json:"TotalReward"` // total credits earned by killing pirates
MissionID int `json:"MissionID"`
MissionReward int `json:"Reward"` // credits earned by completing a mission
Active []struct {
MissionID int `json:"MissionID"`
Expires int `json:"Expires"`
} `json:"Active"` // contains active missions, logged at login
}
func journalFile(logPath string) (string, error) {
files, err := os.ReadDir(logPath)
if err != nil {
return "", err
}
var lastFile fs.DirEntry
var lastMod time.Time
for _, file := range files {
if file.IsDir() {
continue
}
if !strings.HasPrefix(file.Name(), "Journal") || !strings.HasSuffix(file.Name(), ".log") {
continue
}
finfo, err := file.Info()
if err != nil {
log.Infof("cannot get info for %s", file.Name())
continue
}
if lastMod.IsZero() || finfo.ModTime().After(lastMod) {
lastMod = finfo.ModTime()
lastFile = file
}
}
if lastMod.IsZero() || lastFile.Name() == "" {
return "", fmt.Errorf("cannot find the journal file")
}
return lastFile.Name(), nil
}
func (j *journalEvent) printLog(v ...interface{}) {
if j.Timestamp.Add(10 * time.Second).Before(time.Now()) {
return
}
log.Infoln(v...)
}