refactor: log with zap
This commit is contained in:
		@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"github.com/cyrilix/robocar-protobuf/go/events"
 | 
			
		||||
	"github.com/cyrilix/robocar-simulator/pkg/simulator"
 | 
			
		||||
	"github.com/golang/protobuf/ptypes/timestamp"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -32,7 +32,7 @@ type ThrottleSource interface {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(addressSimulator string, car *simulator.CarConfigMsg, racer *simulator.RacerBioMsg, camera *simulator.CamConfigMsg) *Gateway {
 | 
			
		||||
	l := log.WithField("simulator", addressSimulator)
 | 
			
		||||
	l := zap.S().With("simulator", addressSimulator)
 | 
			
		||||
	l.Info("run gateway from simulator")
 | 
			
		||||
 | 
			
		||||
	return &Gateway{
 | 
			
		||||
@@ -51,7 +51,7 @@ func New(addressSimulator string, car *simulator.CarConfigMsg, racer *simulator.
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Simulator interface to events gateway frames into events topicFrame */
 | 
			
		||||
// Gateway is Simulator interface to events gateway frames into events topicFrame
 | 
			
		||||
type Gateway struct {
 | 
			
		||||
	cancel chan interface{}
 | 
			
		||||
 | 
			
		||||
@@ -64,7 +64,7 @@ type Gateway struct {
 | 
			
		||||
	muControl   sync.Mutex
 | 
			
		||||
	lastControl *simulator.ControlMsg
 | 
			
		||||
 | 
			
		||||
	log *log.Entry
 | 
			
		||||
	log *zap.SugaredLogger
 | 
			
		||||
 | 
			
		||||
	frameSubscribers    map[chan<- *events.FrameMessage]interface{}
 | 
			
		||||
	steeringSubscribers map[chan<- *events.SteeringMessage]interface{}
 | 
			
		||||
@@ -205,7 +205,7 @@ func (g *Gateway) listen() error {
 | 
			
		||||
		case simulator.MsgTypeRacerInfo:
 | 
			
		||||
			g.broadcastRacerMsg(rawLine)
 | 
			
		||||
		default:
 | 
			
		||||
			log.Warnf("unmanaged simulator message: %v", string(rawLine))
 | 
			
		||||
			g.log.Warnf("unmanaged simulator message: %v", string(rawLine))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -269,7 +269,7 @@ func (g *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) *events.FrameRef
 | 
			
		||||
		Frame: msgSim.Image,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Debugf("events frame '%v/%v'", msg.Id.Name, msg.Id.Id)
 | 
			
		||||
	g.log.Debugf("events frame '%v/%v'", msg.Id.Name, msg.Id.Id)
 | 
			
		||||
 | 
			
		||||
	for fs := range g.frameSubscribers {
 | 
			
		||||
		fs <- msg
 | 
			
		||||
@@ -284,7 +284,7 @@ func (g *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg, frameRef
 | 
			
		||||
		Confidence: 1.0,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Debugf("events steering '%v'", steering.Steering)
 | 
			
		||||
	g.log.Debugf("events steering '%v'", steering.Steering)
 | 
			
		||||
	for ss := range g.steeringSubscribers {
 | 
			
		||||
		ss <- steering
 | 
			
		||||
	}
 | 
			
		||||
@@ -297,7 +297,7 @@ func (g *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg, frameRef
 | 
			
		||||
		Confidence: 1.0,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Debugf("events throttle '%v'", msg.Throttle)
 | 
			
		||||
	g.log.Debugf("events throttle '%v'", msg.Throttle)
 | 
			
		||||
	for ts := range g.throttleSubscribers {
 | 
			
		||||
		ts <- msg
 | 
			
		||||
	}
 | 
			
		||||
@@ -398,7 +398,7 @@ func (g *Gateway) writeCommand(content []byte) error {
 | 
			
		||||
		g.log.Errorf("unable to connect to simulator to send control command: %v", err)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	log.Debugf("write command to simulator: %v", string(content))
 | 
			
		||||
	g.log.Debugf("write command to simulator: %v", string(content))
 | 
			
		||||
	w := bufio.NewWriter(g.conn)
 | 
			
		||||
 | 
			
		||||
	_, err := w.Write(append(content, '\n'))
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/cyrilix/robocar-protobuf/go/events"
 | 
			
		||||
	"github.com/cyrilix/robocar-simulator/pkg/simulator"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -48,7 +48,7 @@ func TestGateway_ListenEvents(t *testing.T) {
 | 
			
		||||
	simulatorMock.WaitConnection()
 | 
			
		||||
	simulatorMock.EmitMsg(fmt.Sprintf("{\"msg_type\": \"%s\"}", simulator.MsgTypeCarLoaded))
 | 
			
		||||
 | 
			
		||||
	log.Trace("read test data")
 | 
			
		||||
	zap.S().Debug("read test data")
 | 
			
		||||
	testContent, err := ioutil.ReadFile("testdata/msg.json")
 | 
			
		||||
	lines := strings.Split(string(testContent), "\n")
 | 
			
		||||
 | 
			
		||||
@@ -93,7 +93,7 @@ func TestGateway_ListenEvents(t *testing.T) {
 | 
			
		||||
				throttleIdRef = msg.FrameRef
 | 
			
		||||
				wg.Done()
 | 
			
		||||
			case <-finished:
 | 
			
		||||
				log.Trace("loop ended")
 | 
			
		||||
				zap.S().Debugf("loop ended")
 | 
			
		||||
				endLoop = true
 | 
			
		||||
			case <-timeout:
 | 
			
		||||
				t.Errorf("not all event are published")
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/cyrilix/robocar-simulator/pkg/simulator"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -51,7 +51,7 @@ func (c *Gw2SimMock) listen() error {
 | 
			
		||||
		for {
 | 
			
		||||
			conn, err := c.ln.Accept()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Debugf("connection close: %v", err)
 | 
			
		||||
				zap.S().Debugf("connection close: %v", err)
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
			go c.handleConnection(conn)
 | 
			
		||||
@@ -65,6 +65,7 @@ func (c *Gw2SimMock) Addr() string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Gw2SimMock) handleConnection(conn net.Conn) {
 | 
			
		||||
	log := zap.S()
 | 
			
		||||
	c.initOnce.Do(c.init)
 | 
			
		||||
	reader := bufio.NewReader(conn)
 | 
			
		||||
	writer := bufio.NewWriter(conn)
 | 
			
		||||
@@ -131,7 +132,7 @@ func (c *Gw2SimMock) handleConnection(conn net.Conn) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Gw2SimMock) Close() error {
 | 
			
		||||
	log.Debugf("close mock server")
 | 
			
		||||
	zap.S().Debugf("close mock server")
 | 
			
		||||
	err := c.ln.Close()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to close mock server: %v", err)
 | 
			
		||||
@@ -154,7 +155,7 @@ type Sim2GwMock struct {
 | 
			
		||||
	conn          net.Conn
 | 
			
		||||
	writer        *bufio.Writer
 | 
			
		||||
	newConnection chan net.Conn
 | 
			
		||||
	logger        *log.Entry
 | 
			
		||||
	logger        *zap.SugaredLogger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Sim2GwMock) EmitMsg(p string) (err error) {
 | 
			
		||||
@@ -187,7 +188,7 @@ func (c *Sim2GwMock) WaitConnection() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Sim2GwMock) Start() error {
 | 
			
		||||
	c.logger = log.WithField("simulator", "mock")
 | 
			
		||||
	c.logger = zap.S().With("simulator", "mock")
 | 
			
		||||
	c.newConnection = make(chan net.Conn)
 | 
			
		||||
	ln, err := net.Listen("tcp", "127.0.0.1:")
 | 
			
		||||
	c.ln = ln
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user