Skip to content

Commit

Permalink
refactor timestamp reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mroote committed Jan 13, 2021
1 parent d18d480 commit 7ca4363
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/factorio/gamelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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))
}
Expand Down

0 comments on commit 7ca4363

Please sign in to comment.