From: Eric S. Raymond Date: Sun, 26 Aug 2012 10:35:58 +0000 (-0400) Subject: Add debuglevel. X-Git-Tag: 1.0~127 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f472882a9979a96ec3389573bfe613f885bd1245;p=irker.git Add debuglevel. --- diff --git a/irker.py b/irker.py index ccdb7db..bf4e064 100755 --- a/irker.py +++ b/irker.py @@ -23,8 +23,8 @@ class SessionException(exceptions.Exception): class Session(): "IRC session and message queue processing." count = 1 - def __init__(self, irc, url): - self.irc = irc + def __init__(self, irker, url): + self.irker = irker self.url = url # The consumer thread self.queue = Queue.Queue() @@ -39,9 +39,9 @@ class Session(): self.servername = host self.channel = parsed.path.lstrip('/') self.port = int(port) - self.server = self.irc.server() - print "connecting: server=%s port=%s name=%s" % (self.servername, self.port, self.name()) - #self.server.connect(self.servername, self.port, self.name()) + self.server = self.irker.irc.server() + self.irker.debug(1, "connecting: server=%s port=%s name=%s" % (self.servername, self.port, self.name())) + self.server.connect(self.servername, self.port, self.name()) Session.count += 1 def enqueue(self, message): "Enque a message for transmission." @@ -60,13 +60,14 @@ class Session(): self.queue.join() def ship(self, channel, message): "Ship a message to the channel." - print "%s: %s" % (channel, message) - #self.server.connection.join(chaannel) - #self.server.privmsg(channel, message) + self.irker.debug(1, "%s gets %s" % (channel, repr(message))) + self.server.join(channel) + self.server.privmsg(channel, message) class Irker: "Persistent IRC multiplexer." - def __init__(self): + def __init__(self, debuglevel=0): + self.debuglevel = 0 self.irc = irclib.IRC() thread = threading.Thread(target=self.irc.process_forever) self.irc._thread = thread @@ -76,6 +77,10 @@ class Irker: def logerr(self, errmsg): "Log a processing error." sys.stderr.write("irker: " + errmsg + "\n") + def debug(self, level, errmsg): + "Debugging information." + if level >= self.debuglevel: + sys.stderr.write("irker: " + errmsg + "\n") def run(self, ifp, await=True): "Accept JSON relay requests from specified stream." while True: @@ -98,9 +103,14 @@ class Irker: channel = request['channel'] message = request['message'] if channel not in self.sessions: - self.sessions[channel] = Session(self.irc, channel) + self.sessions[channel] = Session(self, channel) self.sessions[channel].enqueue(message) if __name__ == '__main__': - irker = Irker() + debuglevel = 0 + (options, arguments) = getopt.getopt(sys.argv[1:], "-d:") + for (opt, val) in options: + if opt == '-v': + debuglevel = int(val) + irker = Irker(debuglevel=debuglevel) irker.run(sys.stdin)