Skip to content

Commit 7ca4363

Browse files
committed
refactor timestamp reformatting
1 parent d18d480 commit 7ca4363

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/api/handlers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/mroote/factorio-server-manager/bootstrap"
8-
"github.com/mroote/factorio-server-manager/factorio"
97
"io"
108
"io/ioutil"
119
"log"
@@ -16,6 +14,9 @@ import (
1614
"sync"
1715
"time"
1816

17+
"github.com/mroote/factorio-server-manager/bootstrap"
18+
"github.com/mroote/factorio-server-manager/factorio"
19+
1920
"github.com/gorilla/mux"
2021
)
2122

@@ -216,7 +217,7 @@ func LogTail(w http.ResponseWriter, r *http.Request) {
216217

217218
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
218219
config := bootstrap.GetConfig()
219-
resp, err = factorio.TailLog()
220+
resp, err = factorio.TailLog(config.FactorioLog)
220221
if err != nil {
221222
resp = fmt.Sprintf("Could not tail %s: %s", config.FactorioLog, err)
222223
return

src/factorio/gamelog.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
"github.com/mroote/factorio-server-manager/bootstrap"
1212
)
1313

14-
func tailLog(filename string) ([]string, error) {
15-
result := []string{}
14+
func TailLog(filename string) ([]string, error) {
15+
// Tail the Factorio game log file
16+
var result []string
1617

1718
config := bootstrap.GetConfig()
1819

@@ -46,7 +47,7 @@ func getOffset(line string) (string, error) {
4647

4748
func getStartTime(line string) time.Time {
4849
re, _ := regexp.Compile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}`)
49-
date := string(re.FindString(line))
50+
date := re.FindString(line)
5051
startTime, _ := time.Parse(time.RFC3339, strings.Replace(date, " ", "T", 1)+"Z")
5152

5253
return startTime
@@ -67,10 +68,10 @@ func replaceTimestampInLine(line string, offset string, startTime time.Time) str
6768
func reformatTimestamps(log []string) []string {
6869
firstLine := log[0]
6970
startTime := getStartTime(firstLine)
70-
result := []string{}
71+
var result []string
7172

72-
for i := range log {
73-
line := strings.TrimLeft(log[i], " \t")
73+
for _, line := range log {
74+
line = strings.TrimLeft(line, " \t")
7475
offset, _ := getOffset(line)
7576
result = append(result, replaceTimestampInLine(line, offset, startTime))
7677
}

0 commit comments

Comments
 (0)