@@ -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+
196220func (f * FactorioServer ) checkLogError (logline []string ) error {
197221 // TODO Handle errors generated by running Factorio Server
198222 log .Println (logline )
0 commit comments