Skip to content

Commit 3bf1f0c

Browse files
committed
add logging from console to logfile
1 parent 375fd87 commit 3bf1f0c

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/factorio_server.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,21 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
162162
stdScanner := bufio.NewScanner(std)
163163
for stdScanner.Scan() {
164164
log.Printf("Factorio Server: %s", stdScanner.Text())
165+
if err := f.writeLog(stdScanner.Text()); err != nil {
166+
log.Printf("Error: %s", err)
167+
}
168+
165169
line := strings.Fields(stdScanner.Text())
166-
// Check if Factorio Server reports any errors if so handle it
170+
// Ensure logline slice is in bounds
167171
if len(line) > 0 {
172+
// Check if Factorio Server reports any errors if so handle it
168173
if line[1] == "Error" {
169174
err := f.checkLogError(line)
170175
if err != nil {
171176
log.Printf("Error checking Factorio Server Error: %s", err)
172177
}
173178
}
174-
// If rcon port is opened connect to rcon
179+
// If rcon port opens indicated in log connect to rcon
175180
rconLog := "Starting RCON interface at port " + strconv.Itoa(config.FactorioRconPort)
176181
// check if slice index is greater than 2 to prevent panic
177182
if len(line) > 2 {
@@ -193,6 +198,25 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
193198
return nil
194199
}
195200

201+
func (f *FactorioServer) writeLog(logline string) error {
202+
logfileName := config.FactorioDir + "factorio-server-console.log"
203+
file, err := os.OpenFile(logfileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
204+
if err != nil {
205+
log.Printf("Cannot open logfile for appending Factorio Server output: %s", err)
206+
return err
207+
}
208+
defer file.Close()
209+
210+
logline = logline + "\n"
211+
212+
if _, err = file.WriteString(logline); err != nil {
213+
log.Printf("Error appending to factorio-server-console.log: %s", err)
214+
return err
215+
}
216+
217+
return nil
218+
}
219+
196220
func (f *FactorioServer) checkLogError(logline []string) error {
197221
// TODO Handle errors generated by running Factorio Server
198222
log.Println(logline)

src/handlers.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,7 @@ func StopServer(w http.ResponseWriter, r *http.Request) {
650650
}
651651
return
652652
}
653-
if err != nil {
654-
log.Printf("Error closing rcon connection: %s", err)
655-
resp.Data = fmt.Sprintf("Error in stop server handler: %s", err)
656-
if err := json.NewEncoder(w).Encode(resp); err != nil {
657-
log.Printf("Error encoding config file JSON reponse: ", err)
658-
}
659-
return
660-
}
653+
661654
log.Printf("Stopped Factorio server.")
662655
resp.Success = true
663656
resp.Data = fmt.Sprintf("Factorio server stopped")

src/wsroutes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88

99
func logSubscribe(client *Client, data interface{}) {
1010
go func() {
11-
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true})
11+
logfile := config.FactorioDir + "factorio-server-console.log"
12+
t, err := tail.TailFile(logfile, tail.Config{Follow: true})
1213
if err != nil {
1314
log.Printf("Error subscribing to tail log %s", err)
1415
return

0 commit comments

Comments
 (0)