Skip to content

Commit 6a87670

Browse files
committed
refactor timestamp reformatting
1 parent acb46f8 commit 6a87670

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

src/gamelog.go

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"errors"
54
"log"
65
"regexp"
76
"strings"
@@ -10,6 +9,11 @@ import (
109
"github.com/hpcloud/tail"
1110
)
1211

12+
// point of time when logging started
13+
var startTime time.Time
14+
15+
// set this to toggle log reformat of timestamps
16+
var reformatTimestampsEnabled = true
1317

1418
func tailLog(filename string) ([]string, error) {
1519
result := []string{}
@@ -24,57 +28,39 @@ func tailLog(filename string) ([]string, error) {
2428
result = append(result, line.Text)
2529
}
2630

27-
result = reformatTimestamps(result)
28-
29-
return result, nil
30-
}
31-
32-
33-
func getOffset(line string) (string, error) {
34-
re, _ := regexp.Compile(`^\d+.\d+`)
35-
36-
if !re.MatchString(line) {
37-
log.Printf("This line has no offset", line)
38-
return "error", errors.New(line)
31+
if reformatTimestampsEnabled {
32+
result = reformatTimestamps(result)
3933
}
4034

41-
offset := re.FindString(line)
42-
43-
return offset, nil
35+
return result, nil
4436
}
4537

46-
47-
func getStartTime(line string) (time.Time) {
38+
func getStartTime(log []string) {
4839
re, _ := regexp.Compile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}`)
49-
date := string(re.FindString(line))
50-
startTime, _ := time.Parse(time.RFC3339, strings.Replace(date, " ", "T", 1) + "Z")
51-
52-
return startTime
40+
date := string(re.FindString(log[0]))
41+
startTime, _ = time.Parse(time.RFC3339, strings.Replace(date, " ", "T", 1) + "Z")
5342
}
5443

55-
56-
func replaceTimestampInLine(line string, offset string, startTime time.Time) (string) {
57-
offset, err := getOffset(line)
58-
offsetDuration, _ := time.ParseDuration(offset + "s")
44+
// convert an offset timestamp from the log to a human readable timestamp
45+
func offsetToTimestamp(offset string) (string) {
46+
offsetDuration, err := time.ParseDuration(offset + "s")
5947
timestamp := startTime.Add(offsetDuration)
6048

6149
if err == nil {
62-
return timestamp.Format("2006-01-02 15:04:05") + ":" + strings.Replace(line, offset, "", 1)
50+
return timestamp.Format("2006-01-02 15:04:05") + ":" + strings.Replace(offset, offset, "", 1)
6351
} else {
64-
return line
52+
return offset
6553
}
6654
}
6755

68-
6956
func reformatTimestamps(log []string) ([]string) {
70-
firstLine := log[0]
71-
startTime := getStartTime(firstLine)
57+
getStartTime(log)
7258
result := []string{}
7359

7460
for i := range log {
7561
line := strings.TrimLeft(log[i], " \t")
76-
offset, _ := getOffset(line)
77-
result = append(result, replaceTimestampInLine(line, offset, startTime))
62+
re, _ := regexp.Compile(`^\d+\.\d+`)
63+
result = append(result, re.ReplaceAllStringFunc(line, offsetToTimestamp))
7864
}
7965

8066
return result

0 commit comments

Comments
 (0)