From 7ca4363c36103948aadeb2ab9fc9ffaeca2c77f2 Mon Sep 17 00:00:00 2001 From: Mitch Roote Date: Tue, 12 Jan 2021 21:11:38 -0500 Subject: [PATCH] refactor timestamp reformatting --- src/api/handlers.go | 7 ++++--- src/factorio/gamelog.go | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/api/handlers.go b/src/api/handlers.go index d0c6786d..8ae37ac1 100644 --- a/src/api/handlers.go +++ b/src/api/handlers.go @@ -4,8 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/mroote/factorio-server-manager/bootstrap" - "github.com/mroote/factorio-server-manager/factorio" "io" "io/ioutil" "log" @@ -16,6 +14,9 @@ import ( "sync" "time" + "github.com/mroote/factorio-server-manager/bootstrap" + "github.com/mroote/factorio-server-manager/factorio" + "github.com/gorilla/mux" ) @@ -216,7 +217,7 @@ func LogTail(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json;charset=UTF-8") config := bootstrap.GetConfig() - resp, err = factorio.TailLog() + resp, err = factorio.TailLog(config.FactorioLog) if err != nil { resp = fmt.Sprintf("Could not tail %s: %s", config.FactorioLog, err) return diff --git a/src/factorio/gamelog.go b/src/factorio/gamelog.go index b48badb9..472004b8 100644 --- a/src/factorio/gamelog.go +++ b/src/factorio/gamelog.go @@ -11,8 +11,9 @@ import ( "github.com/mroote/factorio-server-manager/bootstrap" ) -func tailLog(filename string) ([]string, error) { - result := []string{} +func TailLog(filename string) ([]string, error) { + // Tail the Factorio game log file + var result []string config := bootstrap.GetConfig() @@ -46,7 +47,7 @@ func getOffset(line string) (string, error) { func getStartTime(line string) time.Time { re, _ := regexp.Compile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}`) - date := string(re.FindString(line)) + date := re.FindString(line) startTime, _ := time.Parse(time.RFC3339, strings.Replace(date, " ", "T", 1)+"Z") return startTime @@ -67,10 +68,10 @@ func replaceTimestampInLine(line string, offset string, startTime time.Time) str func reformatTimestamps(log []string) []string { firstLine := log[0] startTime := getStartTime(firstLine) - result := []string{} + var result []string - for i := range log { - line := strings.TrimLeft(log[i], " \t") + for _, line := range log { + line = strings.TrimLeft(line, " \t") offset, _ := getOffset(line) result = append(result, replaceTimestampInLine(line, offset, startTime)) }