diff --git a/src/factorio_server.go b/src/factorio_server.go index 9576b7b..241c907 100644 --- a/src/factorio_server.go +++ b/src/factorio_server.go @@ -162,16 +162,21 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) { stdScanner := bufio.NewScanner(std) for stdScanner.Scan() { log.Printf("Factorio Server: %s", stdScanner.Text()) + if err := f.writeLog(stdScanner.Text()); err != nil { + log.Printf("Error: %s", err) + } + line := strings.Fields(stdScanner.Text()) - // Check if Factorio Server reports any errors if so handle it + // Ensure logline slice is in bounds if len(line) > 0 { + // Check if Factorio Server reports any errors if so handle it if line[1] == "Error" { err := f.checkLogError(line) if err != nil { log.Printf("Error checking Factorio Server Error: %s", err) } } - // If rcon port is opened connect to rcon + // If rcon port opens indicated in log connect to rcon rconLog := "Starting RCON interface at port " + strconv.Itoa(config.FactorioRconPort) // check if slice index is greater than 2 to prevent panic if len(line) > 2 { @@ -193,6 +198,25 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) { return nil } +func (f *FactorioServer) writeLog(logline string) error { + logfileName := config.FactorioDir + "factorio-server-console.log" + file, err := os.OpenFile(logfileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + log.Printf("Cannot open logfile for appending Factorio Server output: %s", err) + return err + } + defer file.Close() + + logline = logline + "\n" + + if _, err = file.WriteString(logline); err != nil { + log.Printf("Error appending to factorio-server-console.log: %s", err) + return err + } + + return nil +} + func (f *FactorioServer) checkLogError(logline []string) error { // TODO Handle errors generated by running Factorio Server log.Println(logline) diff --git a/src/handlers.go b/src/handlers.go index d041361..88a09c6 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -650,14 +650,7 @@ func StopServer(w http.ResponseWriter, r *http.Request) { } return } - if err != nil { - log.Printf("Error closing rcon connection: %s", err) - resp.Data = fmt.Sprintf("Error in stop server handler: %s", err) - if err := json.NewEncoder(w).Encode(resp); err != nil { - log.Printf("Error encoding config file JSON reponse: ", err) - } - return - } + log.Printf("Stopped Factorio server.") resp.Success = true resp.Data = fmt.Sprintf("Factorio server stopped") diff --git a/src/wsroutes.go b/src/wsroutes.go index a3d44dd..ebfea7a 100644 --- a/src/wsroutes.go +++ b/src/wsroutes.go @@ -8,7 +8,8 @@ import ( func logSubscribe(client *Client, data interface{}) { go func() { - t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true}) + logfile := config.FactorioDir + "factorio-server-console.log" + t, err := tail.TailFile(logfile, tail.Config{Follow: true}) if err != nil { log.Printf("Error subscribing to tail log %s", err) return