From 26596e5e002c516cb8da1a3525c9f5f0510ecdcb Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 26 Jul 2012 20:17:18 -0500 Subject: [PATCH] add debug logging to irclib rather than the gross print()ing note that with the default logging config, nothing is printed at INFO, so irclib is silent. but this is better control over the debugging --- dr.botzo.py | 3 --- extlib/irclib.py | 43 +++++++++++++++++-------------------------- logging.cfg | 8 +++++++- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/dr.botzo.py b/dr.botzo.py index fadf319..89425be 100644 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -49,9 +49,6 @@ except NoSectionError as e: except NoOptionError as e: sys.exit("Aborted due to error with necessary configuration: " + str(e)) -# load additional options -irclib.DEBUG = config.getboolean('dr.botzo', 'debug') - logging.config.fileConfig('logging.cfg') log = logging.getLogger('drbotzo') diff --git a/extlib/irclib.py b/extlib/irclib.py index d9e05fb..ae23766 100644 --- a/extlib/irclib.py +++ b/extlib/irclib.py @@ -64,6 +64,7 @@ Current limitations: """ import bisect +import logging import re import select import socket @@ -80,7 +81,7 @@ try: except Exception: VERSION = () -DEBUG = False +log = logging.getLogger('irclib') # TODO # ---- @@ -568,8 +569,7 @@ class ServerConnection(Connection): self.previous_buffer = lines.pop() for line in lines: - if DEBUG: - print "FROM SERVER:", line + log.debug("FROM SERVER:" + line) if not line: continue @@ -630,16 +630,14 @@ class ServerConnection(Connection): command = "ctcpreply" m = list(m) - if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, m) + log.debug("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]) + log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, [m])) self._handle_event(Event(command, prefix, target, [m])) else: target = None @@ -656,9 +654,8 @@ 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) + log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, arguments)) self._handle_event(Event(command, prefix, target, arguments)) def _handle_event(self, event): @@ -853,8 +850,7 @@ class ServerConnection(Connection): self.ssl.write(string + "\r\n") else: self.socket.send(string + "\r\n") - if DEBUG: - print "TO SERVER:", string + log.debug("TO SERVER:" + string) except socket.error: # Ouch! self.disconnect("Connection reset by peer.") @@ -1011,9 +1007,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) + log.debug("DCC connection from %s:%d" % ( + self.peeraddress, self.peerport)) self.irclibobj._handle_event( self, Event("dcc_connect", self.peeraddress, None, None)) @@ -1049,12 +1044,10 @@ class DCCConnection(Connection): prefix = self.peeraddress target = None for chunk in chunks: - if DEBUG: - print "FROM PEER:", chunk + log.debug("FROM PEER:" + chunk) arguments = [chunk] - if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, arguments) + log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, arguments)) self.irclibobj._handle_event( self, Event(command, prefix, target, arguments)) @@ -1073,8 +1066,7 @@ class DCCConnection(Connection): self.socket.send(string) if self.dcctype == "chat": self.socket.send("\n") - if DEBUG: - print "TO PEER: %s\n" % string + log.debug("TO PEER: %s\n" % string) except socket.error: # Ouch! self.disconnect("Connection reset by peer.") @@ -1109,8 +1101,7 @@ class SimpleIRCClient(object): def _dispatcher(self, c, e): """[Internal]""" - if DEBUG: - print("irclib.py:_dispatcher:%s" % e.eventtype()) + log.debug("irclib.py:_dispatcher:%s" % e.eventtype()) m = "on_" + e.eventtype() if hasattr(self, m): diff --git a/logging.cfg b/logging.cfg index 0f7b3db..bb2eec4 100644 --- a/logging.cfg +++ b/logging.cfg @@ -1,5 +1,5 @@ [loggers] -keys = root,drbotzo +keys = root,drbotzo,irclib [handlers] keys = stdout,logfile @@ -11,6 +11,12 @@ keys = verbose level = INFO handlers = stdout,logfile +[logger_irclib] +level = INFO +handlers = stdout,logfile +propagate = 0 +qualname = irclib + [logger_drbotzo] level = DEBUG handlers = stdout,logfile