Use log instead of fmt for logging

Also fix up a few errors to lower case
This commit is contained in:
Marc Di Luzio 2020-06-11 19:04:53 +01:00
parent 1cafd4f2ce
commit 2f5863b17a
10 changed files with 49 additions and 47 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
@ -82,7 +83,7 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
var data rove.RegisterData
err := json.NewDecoder(b).Decode(&data)
if err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
log.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()
} else if len(data.Name) == 0 {
@ -109,7 +110,7 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
response.Success = true
}
fmt.Printf("register response:%+v\n", response)
log.Printf("register response:%+v\n", response)
return response, nil
}
@ -124,7 +125,7 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
// Decode the commands, verify them and the account, and execute the commands
var data rove.CommandData
if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
log.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()
}
@ -151,7 +152,7 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
response.Success = true
}
fmt.Printf("command response \taccount:%s\tresponse:%+v\n", id, response)
log.Printf("command response \taccount:%s\tresponse:%+v\n", id, response)
return response, nil
}
@ -189,7 +190,7 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
response.Success = true
}
fmt.Printf("radar response \taccount:%s\tresponse:%+v\n", id, response)
log.Printf("radar response \taccount:%s\tresponse:%+v\n", id, response)
return response, nil
}
@ -223,6 +224,6 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
response.Success = true
}
fmt.Printf("rover response \taccount:%s\tresponse:%+v\n", id, response)
log.Printf("rover response \taccount:%s\tresponse:%+v\n", id, response)
return response, nil
}

View file

@ -116,7 +116,7 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
s.sync.Add(1)
// Connect to the accountant
fmt.Printf("Dialing accountant on %s\n", accountantAddress)
log.Printf("Dialing accountant on %s\n", accountantAddress)
s.clientConn, err = grpc.Dial(accountantAddress, grpc.WithInsecure())
if err != nil {
return err
@ -139,7 +139,7 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
}
// Start the listen
fmt.Printf("Listening on %s\n", s.server.Addr)
log.Printf("Listening on %s\n", s.server.Addr)
if s.listener, err = net.Listen("tcp", s.server.Addr); err != nil {
return err
}
@ -164,7 +164,7 @@ func (s *Server) Run() {
s.sync.Add(1)
defer s.sync.Done()
fmt.Println("Executing server tick")
log.Println("Executing server tick")
// Run the command queues
s.world.ExecuteCommandQueues()
@ -175,7 +175,7 @@ func (s *Server) Run() {
log.Fatal(err)
}
s.schedule.Start()
fmt.Printf("First server tick scheduled for %s\n", s.schedule.Entries()[0].Next.Format("15:04:05"))
log.Printf("First server tick scheduled for %s\n", s.schedule.Entries()[0].Next.Format("15:04:05"))
}
// Serve the http requests
@ -252,7 +252,7 @@ func (s *Server) LoadWorld() error {
func (s *Server) wrapHandler(method string, handler Handler) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Log the request
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
log.Printf("%s\t%s\n", r.Method, r.RequestURI)
vars := mux.Vars(r)
@ -261,11 +261,11 @@ func (s *Server) wrapHandler(method string, handler Handler) func(w http.Respons
w.WriteHeader(http.StatusMethodNotAllowed)
} else if val, err := handler(s, vars, r.Body, w); err != nil {
fmt.Printf("Failed to handle http request: %s", err)
log.Printf("Failed to handle http request: %s", err)
w.WriteHeader(http.StatusInternalServerError)
} else if err := json.NewEncoder(w).Encode(val); err != nil {
fmt.Printf("Failed to encode reply to json: %s", err)
log.Printf("Failed to encode reply to json: %s", err)
w.WriteHeader(http.StatusInternalServerError)
} else {
@ -280,17 +280,17 @@ func (s *Server) SpawnRoverForAccount(account string) (game.RoverAttributes, uui
return game.RoverAttributes{}, uuid.UUID{}, err
} else if attribs, err := s.world.RoverAttributes(inst); err != nil {
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("no attributes found for created rover: %s", err)
} else {
keyval := accounts.DataKeyValue{Account: account, Key: "rover", Value: inst.String()}
resp, err := s.accountant.AssignValue(context.Background(), &keyval)
if err != nil || !resp.Success {
fmt.Printf("Failed to assign rover to account, %s, %s", err, resp.Error)
log.Printf("Failed to assign rover to account, %s, %s", err, resp.Error)
// Try and clear up the rover
if err := s.world.DestroyRover(inst); err != nil {
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
log.Printf("Failed to destroy rover after failed rover assign: %s", err)
}
return game.RoverAttributes{}, uuid.UUID{}, err