Ema has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/354677 )

Change subject: bgp: log with util.log instead of printing to stdout
......................................................................

bgp: log with util.log instead of printing to stdout

Change-Id: Ib8508b2ab33c5eca2affefe028968b0d1033dd8f
---
M pybal/bgp.py
1 file changed, 33 insertions(+), 46 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/debs/pybal 
refs/changes/77/354677/1

diff --git a/pybal/bgp.py b/pybal/bgp.py
index 47d21c4..67488e9 100644
--- a/pybal/bgp.py
+++ b/pybal/bgp.py
@@ -17,8 +17,12 @@
 from zope.interface import implements, Interface
 
 # Twisted imports
-from twisted import copyright
-from twisted.internet import reactor, protocol, base, interfaces, error, defer
+from twisted.internet import reactor, protocol, interfaces, error, defer
+
+# Pybal imports
+from pybal import util
+
+log = util.log
 
 # Constants
 VERSION = 4
@@ -970,7 +974,7 @@
     
     def __setattr__(self, name, value):
         if name == 'state' and value != getattr(self, name):
-            print "State is now:", stateDescr[value]
+            log.info("State is now: {}".format(stateDescr[value]), 
system="bgp")
         super(FSM, self).__setattr__(name, value)
 
     def manualStart(self):
@@ -1102,8 +1106,7 @@
         
         elif self.state == ST_OPENCONFIRM:
             # State OpenConfirm, events 19, 20
-            # DEBUG
-            print "Running collision detection"
+            log.info("Running collision detection", system="bgp")
             
             # Perform collision detection
             self.protocol.collisionDetect()
@@ -1228,8 +1231,7 @@
         (event 23)
         """
         
-        # DEBUG
-        print "Collided, closing."
+        log.info("Collided, closing.", system="bgp")
 
         if self.state == ST_IDLE:
             return
@@ -1244,8 +1246,7 @@
         
         assert(self.delayOpen)
         
-        # DEBUG
-        print "Delay Open event"
+        log.info("Delay Open event", system="bgp")
         
         if self.state == ST_CONNECT:
             # State Connect, event 12
@@ -1368,8 +1369,7 @@
         # Set transport socket options
         self.transport.setTcpNoDelay(True)
         
-        # DEBUG
-        print "Connection established"
+        log.info("Connection established", system="bgp")
         
         # Set the local BGP id from the local IP address if it's not set
         if self.factory.bgpId is None:
@@ -1392,8 +1392,7 @@
             self.factory.connectionClosed(self)
             return
         
-        # DEBUG
-        print "Connection lost:", reason.getErrorMessage()
+        log.info("Connection lost: {}".format(reason.getErrorMessage()), 
system="bgp")
         
         try:
             self.fsm.connectionFailed()
@@ -1422,19 +1421,17 @@
     def sendOpen(self):
         """Sends a BGP Open message to the peer"""
         
-        # DEBUG
-        print "Sending Open"
+        log.info("Sending Open", system="bgp")
         
         self.transport.write(self.constructOpen())
     
     def sendUpdate(self, withdrawnPrefixes, attributes, nlri):
         """Sends a BGP Update message to the peer"""
         
-        # DEBUG
-        print "Sending Update"
-        print "Withdrawing:", withdrawnPrefixes
-        print "Attributes:", attributes
-        print "NLRI:", nlri
+        log.info("Sending Update", system="bgp")
+        log.info("Withdrawing: {}".format(withdrawnPrefixes), system="bgp")
+        log.info("Attributes: {}".format(attributes), system="bgp")
+        log.info("NLRI: {}".format(nlri), system="bgp")
         
         self.transport.write(self.constructUpdate(withdrawnPrefixes, 
attributes, nlri))
         self.fsm.updateSent()
@@ -1641,9 +1638,8 @@
     def openReceived(self, version, ASN, holdTime, bgpId):
         """Called when a BGP Open message was received."""
         
-        # DEBUG
-        print "OPEN: version:", version, "ASN:", ASN, "hold time:", \
-            holdTime, "id:", bgpId
+        log.info("OPEN: version: {} ASN: {} hold time: {} id: {}".format(
+            version, ASN, holdTime, bgpId), system="bgp")
             
         self.peerId = bgpId
         self.bgpPeering.setPeerId(bgpId)
@@ -1677,9 +1673,8 @@
     def notificationReceived(self, error, suberror, data=''):
         """Called when a BGP Notification message was received.
         """
-        
-        # DEBUG
-        print "NOTIFICATION:", error, suberror, [ord(d) for d in data]
+        log.info("NOTIFICATION: {} {} {}".format(
+            error, suberror, [ord(d) for d in data]), system="bgp")
         
         self.fsm.notificationReceived(error, suberror)
 
@@ -1693,8 +1688,8 @@
         # Derived times
         self.fsm.keepAliveTime = self.fsm.holdTime / 3
         
-        # DEBUG
-        print "Hold time:", self.fsm.holdTime, "Keepalive time:", 
self.fsm.keepAliveTime
+        log.info("Hold time: {} Keepalive time: {}".format(
+            self.fsm.holdTime, self.fsm.keepAliveTime), system="bgp")
 
     def collisionDetect(self):
         """Performs collision detection. Outsources to factory class 
BGPPeering."""
@@ -1831,8 +1826,7 @@
         pass
     
     def clientConnectionLost(self, connector, reason):
-        # DEBUG
-        print "Client connection lost:", reason.getErrorMessage()     
+        log.info("Client connection lost: 
{}".format(reason.getErrorMessage()), system="bgp")
 
 class BGPServerFactory(BGPFactory):
     """Class managing the server (listening) side of the BGP
@@ -1848,9 +1842,7 @@
         """Builds a BGPProtocol instance by finding an appropriate
         BGPPeering factory instance to hand over to.
         """
-        
-        # DEBUG
-        print "Connection received from", addr.host
+        log.info("Connection received from {}".format(addr.host), system="bgp")
         
         try:
             bgpPeering = self.peers[addr.host]
@@ -1879,8 +1871,7 @@
        
     def buildProtocol(self, addr):
         """Builds a BGP protocol instance"""
-        
-        print "Building a new BGP protocol instance"
+        log.info("Building a new BGP protocol instance", system="bgp")
         
         p = BGPFactory.buildProtocol(self, addr)
         if p is not None:
@@ -1922,9 +1913,7 @@
 
     def clientConnectionFailed(self, connector, reason):
         """Called when the outgoing connection failed."""
-        
-        # DEBUG
-        print "Client connection failed:", reason.getErrorMessage()
+        log.info("Client connection failed: 
{}".format(reason.getErrorMessage()), system="bgp")
 
         # There is no protocol instance yet at this point.
         # Catch a possible NotificationException
@@ -1978,7 +1967,7 @@
         Called by FSM or Protocol when the BGP connection has been closed.
         """
         
-        print "Connection closed"
+        log.info("Connection closed", system="bgp")
         
         if protocol is not None:
             # Connection succeeded previously, protocol exists
@@ -2033,7 +2022,7 @@
     def protocolError(self, failure):
         failure.trap(BGPException)
         
-        print "BGP exception", failure
+        log.error("BGP exception: %s" % failure)
         
         e = failure.check(NotificationSent)
         try:
@@ -2041,9 +2030,9 @@
             failure.raiseException()
         except NotificationSent, e:
             if (e.error, e.suberror) == (ERR_MSG_UPDATE, 
ERR_MSG_UPDATE_ATTR_FLAGS):
-                print "exception on flags:", BGP.parseEncodedAttributes(e.data)
+                log.error("exception on flags: %s" % 
BGP.parseEncodedAttributes(e.data))
             else:
-                print e.error, e.suberror, e.data   
+                log.error("%s %s %s" % (e.error, e.suberror, e.data))
         
         # FIXME: error handling
         
@@ -2110,9 +2099,7 @@
         """Initiates a TCP connection to the peer. Should only be called from
         BGPPeering or FSM, otherwise use manualStart() instead.
         """
-        
-        # DEBUG
-        print "(Re)connect to", self.peerAddr
+        log.info("(Re)connect to {}".format(self.peerAddr), system="bgp")
         
         if self.fsm.state != ST_ESTABLISHED:        
             reactor.connectTCP(self.peerAddr, PORT, self)
@@ -2299,4 +2286,4 @@
         if (len(withdrawals) + len(advertisements) > 0
             ) or (MPReachNLRIAttribute in attributes
             ) or (MPUnreachNLRIAttribute in attributes):
-            self.estabProtocol.sendUpdate([w.prefix for w in withdrawals], 
attributes, [a.prefix for a in advertisements])
\ No newline at end of file
+            self.estabProtocol.sendUpdate([w.prefix for w in withdrawals], 
attributes, [a.prefix for a in advertisements])

-- 
To view, visit https://gerrit.wikimedia.org/r/354677
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib8508b2ab33c5eca2affefe028968b0d1033dd8f
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: master
Gerrit-Owner: Ema <e...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to