add logging from console to logfile

This commit is contained in:
majormjr 2016-12-22 22:57:04 -05:00 committed by Mitch Roote
parent 375fd87879
commit 3bf1f0cb07
3 changed files with 29 additions and 11 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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