Improved debugging.
authorEric S. Raymond <esr@thyrsus.com>
Sun, 26 Aug 2012 11:38:05 +0000 (07:38 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Sun, 26 Aug 2012 11:38:05 +0000 (07:38 -0400)
irclib.py

index 8b90a00d799b628b43fe795760b3acd77c100391..6964b6a46fc6132e11b0279408f4fbf865396d15 100644 (file)
--- a/irclib.py
+++ b/irclib.py
@@ -81,8 +81,6 @@ try:
 except Exception:
     VERSION = ()
 
-DEBUG = False
-
 # TODO
 # ----
 # (maybe) thread safety
@@ -581,8 +579,7 @@ class ServerConnection(Connection):
         self.previous_buffer = lines.pop()
 
         for line in lines:
-            if DEBUG:
-                print "FROM SERVER:", line
+            self.irclibobj.debug(1, "FROM SERVER: %s" % repr(line))
 
             if not line:
                 continue
@@ -643,16 +640,12 @@ class ServerConnection(Connection):
                             command = "ctcpreply"
 
                         m = list(m)
-                        if DEBUG:
-                            print "command: %s, source: %s, target: %s, arguments: %s" % (
-                                command, prefix, target, m)
+                        self.irclibobj.debug(1, "command: %s, source: %s, target: %s, arguments: %s" % (command, prefix, target, m))
                         self._handle_event(Event(command, prefix, target, m))
                         if command == "ctcp" and m[0] == "ACTION":
                             self._handle_event(Event("action", prefix, target, m[1:]))
                     else:
-                        if DEBUG:
-                            print "command: %s, source: %s, target: %s, arguments: %s" % (
-                                command, prefix, target, [m])
+                        self.irclibobj.debug(1, "command: %s, source: %s, target: %s, arguments: %s" % (command, prefix, target, [m]))
                         self._handle_event(Event(command, prefix, target, [m]))
             else:
                 target = None
@@ -669,9 +662,7 @@ class ServerConnection(Connection):
                     if not is_channel(target):
                         command = "umode"
 
-                if DEBUG:
-                    print "command: %s, source: %s, target: %s, arguments: %s" % (
-                        command, prefix, target, arguments)
+                self.irclibobj.debug(1, "command: %s, source: %s, target: %s, arguments: %s" % (command, prefix, target, m))
                 self._handle_event(Event(command, prefix, target, arguments))
 
     def _handle_event(self, event):
@@ -866,8 +857,7 @@ class ServerConnection(Connection):
                 self.ssl.write(string + "\r\n")
             else:
                 self.socket.send(string + "\r\n")
-            if DEBUG:
-                print "TO SERVER:", string
+            self.irclibobj.debug(1, "TO SERVER: " + repr(string))
         except socket.error:
             # Ouch!
             self.disconnect("Connection reset by peer.")
@@ -941,6 +931,7 @@ class DCCConnection(Connection):
     """
     def __init__(self, irclibobj, dcctype):
         super(DCCConnection, self).__init__(irclibobj)
+        self.irclibobj = irclibobj
         self.connected = 0
         self.passive = 0
         self.dcctype = dcctype
@@ -1024,9 +1015,8 @@ class DCCConnection(Connection):
             self.socket.close()
             self.socket = conn
             self.connected = 1
-            if DEBUG:
-                print "DCC connection from %s:%d" % (
-                    self.peeraddress, self.peerport)
+            self.irclibobj.debug(1, "DCC connection from %s:%d" % (
+                self.peeraddress, self.peerport))
             self.irclibobj._handle_event(
                 self,
                 Event("dcc_connect", self.peeraddress, None, None))
@@ -1062,12 +1052,10 @@ class DCCConnection(Connection):
         prefix = self.peeraddress
         target = None
         for chunk in chunks:
-            if DEBUG:
-                print "FROM PEER:", chunk
+            self.irclibobj.debug(1, "FROM PEER: " + repr(chunk))
             arguments = [chunk]
-            if DEBUG:
-                print "command: %s, source: %s, target: %s, arguments: %s" % (
-                    command, prefix, target, arguments)
+            self.irclibobj.debug(1, "command: %s, source: %s, target: %s, arguments: %s" % (
+                    command, prefix, target, arguments))
             self.irclibobj._handle_event(
                 self,
                 Event(command, prefix, target, arguments))
@@ -1086,8 +1074,7 @@ class DCCConnection(Connection):
             self.socket.send(string)
             if self.dcctype == "chat":
                 self.socket.send("\n")
-            if DEBUG:
-                print "TO PEER: %s\n" % string
+            self.irclibobj.debug("TO PEER: %s\n" % repr(string))
         except socket.error:
             # Ouch!
             self.disconnect("Connection reset by peer.")
@@ -1122,8 +1109,7 @@ class SimpleIRCClient(object):
 
     def _dispatcher(self, c, e):
         """[Internal]"""
-        if DEBUG:
-            print("irclib.py:_dispatcher:%s" % e.eventtype())
+        self.irclibobj.debug("dispatcher:%s" % e.eventtype())
 
         m = "on_" + e.eventtype()
         if hasattr(self, m):