From 013380c7e7ecee1c5f93afa59edf28986fe7afe4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 29 Jun 2015 15:36:08 +0100 Subject: [PATCH] Update router code to use common logger --- router/connection.go | 17 +++++++++++------ router/connection_maker.go | 7 ++++--- router/ethernet_decoder.go | 4 ++-- router/forwarder.go | 2 +- router/gossip_channel.go | 5 +++-- router/local_peer.go | 21 +++++++++++---------- router/router.go | 23 +++++++++++------------ router/udp_sender.go | 4 ++-- router/utils.go | 7 ++++--- 9 files changed, 49 insertions(+), 41 deletions(-) diff --git a/router/connection.go b/router/connection.go index fc10055b..03c69a94 100644 --- a/router/connection.go +++ b/router/connection.go @@ -4,10 +4,11 @@ import ( "encoding/binary" "encoding/gob" "fmt" - "log" "net" "sync" "time" + + . "github.com/weaveworks/weave/common" ) type Connection interface { @@ -83,7 +84,11 @@ func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { return T func (conn *RemoteConnection) Shutdown(error) {} func (conn *RemoteConnection) Log(args ...interface{}) { - log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...) + Log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...) +} + +func (conn *RemoteConnection) ErrorLog(args ...interface{}) { + Log.Errorln(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...) } func (conn *RemoteConnection) String() string { @@ -102,7 +107,7 @@ func (conn *RemoteConnection) String() string { // end up in the local peer's connections map. func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router, acceptNewPeer bool) { if connRemote.local != router.Ourself.Peer { - log.Fatal("Attempt to create local connection from a peer which is not ourself") + Log.Fatal("Attempt to create local connection from a peer which is not ourself") } // NB, we're taking a copy of connRemote here. actionChan := make(chan ConnectionAction, ChannelSize) @@ -227,7 +232,7 @@ func (conn *LocalConnection) ReceivedHeartbeat(remoteUDPAddr *net.UDPAddr, connU if oldRemoteUDPAddr == nil { return conn.sendFastHeartbeats() } else if oldRemoteUDPAddr.String() != remoteUDPAddr.String() { - log.Println("Peer", conn.remote, "moved from", old, "to", remoteUDPAddr) + Log.Println("Peer", conn.remote, "moved from", old, "to", remoteUDPAddr) } return nil }) @@ -378,9 +383,9 @@ func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err func (conn *LocalConnection) shutdown(err error) { if conn.remote == nil { - log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err) + Log.Errorf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err) } else { - conn.Log("connection shutting down due to error:", err) + conn.ErrorLog("connection shutting down due to error:", err) } if conn.TCPConn != nil { diff --git a/router/connection_maker.go b/router/connection_maker.go index 2e36b69f..acb1102d 100644 --- a/router/connection_maker.go +++ b/router/connection_maker.go @@ -3,10 +3,11 @@ package router import ( "bytes" "fmt" - "log" "math/rand" "net" "time" + + . "github.com/weaveworks/weave/common" ) const ( @@ -298,9 +299,9 @@ func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, dir } func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { - log.Printf("->[%s] attempting connection\n", address) + Log.Printf("->[%s] attempting connection\n", address) if err := cm.ourself.CreateConnection(address, acceptNewPeer); err != nil { - log.Printf("->[%s] error during connection attempt: %v\n", address, err) + Log.Errorf("->[%s] error during connection attempt: %v\n", address, err) cm.ConnectionTerminated(address, err) } } diff --git a/router/ethernet_decoder.go b/router/ethernet_decoder.go index fc96a14b..d989d48a 100644 --- a/router/ethernet_decoder.go +++ b/router/ethernet_decoder.go @@ -2,11 +2,11 @@ package router import ( "bytes" - "log" "net" "code.google.com/p/gopacket" "code.google.com/p/gopacket/layers" + . "github.com/weaveworks/weave/common" ) type EthernetDecoder struct { @@ -61,7 +61,7 @@ func (dec *EthernetDecoder) sendICMPFragNeeded(mtu int, sendFrame func([]byte) e return err } - log.Printf("Sending ICMP 3,4 (%v -> %v): PMTU= %v\n", dec.IP.DstIP, dec.IP.SrcIP, mtu) + Log.Printf("Sending ICMP 3,4 (%v -> %v): PMTU= %v\n", dec.IP.DstIP, dec.IP.SrcIP, mtu) return sendFrame(buf.Bytes()) } diff --git a/router/forwarder.go b/router/forwarder.go index 80367e9b..0d9798d0 100644 --- a/router/forwarder.go +++ b/router/forwarder.go @@ -281,7 +281,7 @@ func (fwd *Forwarder) accumulateAndSendFrames(ch <-chan *ForwardedFrame, frame * } func (fwd *Forwarder) logDrop(frame *ForwardedFrame) { - fwd.conn.Log("Dropping too big frame during forwarding: frame len:", len(frame.frame), "; effective PMTU:", fwd.maxPayload+UDPOverhead-fwd.effectiveOverhead()) + fwd.conn.ErrorLog("Dropping too big frame during forwarding: frame len:", len(frame.frame), "; effective PMTU:", fwd.maxPayload+UDPOverhead-fwd.effectiveOverhead()) } func (fwd *Forwarder) appendFrame(frame *ForwardedFrame) bool { diff --git a/router/gossip_channel.go b/router/gossip_channel.go index aea6d25d..795573f8 100644 --- a/router/gossip_channel.go +++ b/router/gossip_channel.go @@ -4,8 +4,9 @@ import ( "bytes" "encoding/gob" "fmt" - "log" "sync" + + . "github.com/weaveworks/weave/common" ) type GossipChannel struct { @@ -225,5 +226,5 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) { } func (c *GossipChannel) log(args ...interface{}) { - log.Println(append(append([]interface{}{}, "[gossip "+c.name+"]:"), args...)...) + Log.Println(append(append([]interface{}{}, "[gossip "+c.name+"]:"), args...)...) } diff --git a/router/local_peer.go b/router/local_peer.go index 3463e6ba..395a7a2c 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -2,10 +2,11 @@ package router import ( "fmt" - "log" "net" "sync" "time" + + . "github.com/weaveworks/weave/common" ) type LocalPeer struct { @@ -41,13 +42,13 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, frame []byte, dec *Ethernet if !found { // Not necessarily an error as there could be a race with the // dst disappearing whilst the frame is in flight - log.Println("Received packet for unknown destination:", dstPeer) + Log.Println("Received packet for unknown destination:", dstPeer) return nil } conn, found := peer.ConnectionTo(relayPeerName) if !found { // Again, could just be a race, not necessarily an error - log.Println("Unable to find connection to relay peer", relayPeerName) + Log.Println("Unable to find connection to relay peer", relayPeerName) return nil } return conn.(*LocalConnection).Forward(&ForwardedFrame{ @@ -70,9 +71,9 @@ func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, frame []byte, dec *Ethernet dec) if err != nil { if ftbe, ok := err.(FrameTooBigError); ok { - log.Printf("dropping too big DF broadcast frame (%v -> %v): PMTU= %v\n", dec.IP.DstIP, dec.IP.SrcIP, ftbe.EPMTU) + Log.Warnf("dropping too big DF broadcast frame (%v -> %v): PMTU= %v\n", dec.IP.DstIP, dec.IP.SrcIP, ftbe.EPMTU) } else { - log.Println(err) + Log.Errorln(err) } } } @@ -181,10 +182,10 @@ func (peer *LocalPeer) actorLoop(actionChan <-chan LocalPeerAction) { func (peer *LocalPeer) handleAddConnection(conn Connection) error { if peer.Peer != conn.Local() { - log.Fatal("Attempt made to add connection to peer where peer is not the source of connection") + Log.Fatal("Attempt made to add connection to peer where peer is not the source of connection") } if conn.Remote() == nil { - log.Fatal("Attempt made to add connection to peer with unknown remote peer") + Log.Fatal("Attempt made to add connection to peer with unknown remote peer") } toName := conn.Remote().Name dupErr := fmt.Errorf("Multiple connections to %s added to %s", conn.Remote(), peer.String()) @@ -223,7 +224,7 @@ func (peer *LocalPeer) handleAddConnection(conn Connection) error { func (peer *LocalPeer) handleConnectionEstablished(conn Connection) { if peer.Peer != conn.Local() { - log.Fatal("Peer informed of active connection where peer is not the source of connection") + Log.Fatal("Peer informed of active connection where peer is not the source of connection") } if dupConn, found := peer.connections[conn.Remote().Name]; !found || conn != dupConn { conn.Shutdown(fmt.Errorf("Cannot set unknown connection active")) @@ -236,10 +237,10 @@ func (peer *LocalPeer) handleConnectionEstablished(conn Connection) { func (peer *LocalPeer) handleDeleteConnection(conn Connection) { if peer.Peer != conn.Local() { - log.Fatal("Attempt made to delete connection from peer where peer is not the source of connection") + Log.Fatal("Attempt made to delete connection from peer where peer is not the source of connection") } if conn.Remote() == nil { - log.Fatal("Attempt made to delete connection to peer with unknown remote peer") + Log.Fatal("Attempt made to delete connection to peer with unknown remote peer") } toName := conn.Remote().Name if connFound, found := peer.connections[toName]; !found || connFound != conn { diff --git a/router/router.go b/router/router.go index a85d1fb1..00f53588 100644 --- a/router/router.go +++ b/router/router.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "fmt" "io" - "log" "net" "sync" "syscall" @@ -67,11 +66,11 @@ type PacketSourceSink interface { func NewRouter(config Config, name PeerName, nickName string) *Router { router := &Router{Config: config, gossipChannels: make(GossipChannels)} onMacExpiry := func(mac net.HardwareAddr, peer *Peer) { - log.Println("Expired MAC", mac, "at", peer) + Log.Println("Expired MAC", mac, "at", peer) } onPeerGC := func(peer *Peer) { router.Macs.Delete(peer) - log.Println("Removed unreachable peer", peer) + Log.Println("Removed unreachable peer", peer) } router.Ourself = NewLocalPeer(name, nickName, router) router.Macs = NewMacCache(macMaxAge, onMacExpiry) @@ -127,12 +126,12 @@ func (router *Router) Status() string { } func (router *Router) sniff(pio PacketSourceSink) { - log.Println("Sniffing traffic on", router.Iface) + Log.Println("Sniffing traffic on", router.Iface) dec := NewEthernetDecoder() mac := router.Iface.HardwareAddr if router.Macs.Enter(mac, router.Ourself.Peer) { - log.Println("Discovered our MAC", mac) + Log.Println("Discovered our MAC", mac) } go func() { for { @@ -159,7 +158,7 @@ func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecode return } if router.Macs.Enter(srcMac, router.Ourself.Peer) { - log.Println("Discovered local MAC", srcMac) + Log.Println("Discovered local MAC", srcMac) } if dec.DropFrame() { return @@ -202,7 +201,7 @@ func (router *Router) listenTCP(localPort int) { for { tcpConn, err := ln.AcceptTCP() if err != nil { - log.Println(err) + Log.Errorln(err) continue } router.acceptTCP(tcpConn) @@ -216,7 +215,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) { // on router.Port and we wait for them to send us something on UDP to // start. remoteAddrStr := tcpConn.RemoteAddr().String() - log.Printf("->[%s] connection accepted\n", remoteAddrStr) + Log.Printf("->[%s] connection accepted\n", remoteAddrStr) connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false, false) StartLocalConnection(connRemote, tcpConn, nil, router, true) } @@ -246,10 +245,10 @@ func (router *Router) udpReader(conn *net.UDPConn, po PacketSink) { if err == io.EOF { return } else if err != nil { - log.Println("ignoring UDP read error", err) + Log.Warnln("ignoring UDP read error", err) continue } else if n < NameSize { - log.Println("ignoring too short UDP packet from", sender) + Log.Warnln("ignoring too short UDP packet from", sender) continue } name := PeerNameFromBin(buf[:NameSize]) @@ -329,7 +328,7 @@ func (router *Router) handleUDPPacketFunc(relayConn *LocalConnection, dec *Ether dstMac := dec.Eth.DstMAC if router.Macs.Enter(srcMac, srcPeer) { - log.Println("Discovered remote MAC", srcMac, "at", srcPeer) + Log.Println("Discovered remote MAC", srcMac, "at", srcPeer) } if po != nil { router.LogFrame("Injecting", frame, dec) @@ -415,7 +414,7 @@ func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameS // itself included in the update, and we didn't know about // already. We ignore this; eventually we should receive an // update containing a complete topology. - log.Println("Topology gossip:", err) + Log.Println("Topology gossip:", err) return nil, nil, nil } if err != nil { diff --git a/router/udp_sender.go b/router/udp_sender.go index 4c264762..2159898f 100644 --- a/router/udp_sender.go +++ b/router/udp_sender.go @@ -1,12 +1,12 @@ package router import ( - "log" "net" "syscall" "code.google.com/p/gopacket" "code.google.com/p/gopacket/layers" + . "github.com/weaveworks/weave/common" ) type UDPSender interface { @@ -87,7 +87,7 @@ func (sender *RawUDPSender) Send(msg []byte) error { } defer f.Close() fd := int(f.Fd()) - log.Println("EMSGSIZE on send, expecting PMTU update (IP packet was", + Log.Println("EMSGSIZE on send, expecting PMTU update (IP packet was", len(packet), "bytes, payload was", len(msg), "bytes)") pmtu, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_MTU) if err != nil { diff --git a/router/utils.go b/router/utils.go index a57d9e8b..8b56e810 100644 --- a/router/utils.go +++ b/router/utils.go @@ -5,21 +5,22 @@ import ( "crypto/rand" "encoding/gob" "fmt" - "log" "net" + + . "github.com/weaveworks/weave/common" ) var void = struct{}{} func checkFatal(e error) { if e != nil { - log.Fatal(e) + Log.Fatal(e) } } func checkWarn(e error) { if e != nil { - log.Println(e) + Log.Warnln(e) } }