More uniform logging.
authorEric S. Raymond <esr@thyrsus.com>
Sun, 20 Oct 2013 22:28:30 +0000 (18:28 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Sun, 20 Oct 2013 22:28:30 +0000 (18:28 -0400)
irkerd

diff --git a/irkerd b/irkerd
index fd97424ff0a68b6f20f6cc933eba2d6832fcf4c3..0ffa477b0d2f8bb36c45adbfa397821dcf900524 100755 (executable)
--- a/irkerd
+++ b/irkerd
@@ -47,7 +47,6 @@ version = "1.20"
 
 import sys, getopt, urlparse, time, random, socket, signal, re
 import threading, Queue, SocketServer, select
-import logging
 try:
     import simplejson as json  # Faster, also makes us Python-2.4-compatible
 except ImportError:
@@ -107,12 +106,13 @@ class IRCError(Exception):
 
 class IRCClient():
     "An IRC client session to one or more servers."
-    def __init__(self):
+    def __init__(self, debuglevel):
         self.mutex = threading.RLock()
         self.server_connections = []
         self.event_handlers = {}
         self.add_event_handler("ping",
                                lambda c, e: c.ship("PONG %s" % e.target))
+        self.debuglevel = debuglevel
 
     def newserver(self):
         "Initialize a new server-connection object."
@@ -157,6 +157,11 @@ class IRCClient():
         with self.mutex:
             self.server_connections.remove(connection)
 
+    def debug(self, level, errmsg):
+        "Debugging information."
+        if self.debuglevel >= level:
+            sys.stderr.write("irkerd: %s\n" % errmsg)
+
 class LineBufferedStream():
     "Line-buffer a read stream."
     crlf_re = re.compile(b'\r?\n')
@@ -199,8 +204,8 @@ class IRCServerConnection():
 
     def connect(self, server, port, nickname,
                 password=None, username=None, ircname=None):
-        log.debug("connect(server=%r, port=%r, nickname=%r, ...)",
-                  server, port, nickname)
+        self.master.debug(2, "connect(server=%r, port=%r, nickname=%r, ...)" %
+                          (server, port, nickname))
         if self.socket is not None:
             self.disconnect("Changing servers")
 
@@ -249,7 +254,7 @@ class IRCServerConnection():
         self.buffer.append(incoming)
 
         for line in self.buffer:
-            log.debug("FROM: %s", line)
+            self.master.debug(2, "FROM: %s" % line)
 
             if not line:
                 continue
@@ -289,8 +294,8 @@ class IRCServerConnection():
                     target = arguments[0]
                     arguments = arguments[1:]
 
-            log.debug("command: %s, source: %s, target: %s, "
-                      "arguments: %s", command, prefix, target, arguments)
+            self.master.debug(2,
+                              "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):
@@ -344,7 +349,7 @@ class IRCServerConnection():
         "Ship a command to the server, appending CR/LF"
         try:
             self.socket.send(string + b'\r\n')
-            log.debug("TO: %s", string)
+            self.master.debug(2, "TO: %s" % string)
         except socket.error:
             self.disconnect("Connection reset by peer.")
 
@@ -665,7 +670,7 @@ class Irker:
     "Persistent IRC multiplexer."
     def __init__(self, debuglevel=0):
         self.debuglevel = debuglevel
-        self.irc = IRCClient()
+        self.irc = IRCClient(self.debuglevel)
         self.irc.add_event_handler("ping", self._handle_ping)
         self.irc.add_event_handler("welcome", self._handle_welcome)
         self.irc.add_event_handler("erroneusnickname", self._handle_badnick)
@@ -836,7 +841,6 @@ Options
 """)
 
 if __name__ == '__main__':
-    log = logging.getLogger(__name__)
     debuglvl = 0
     namestyle = "irker%03d"
     password = None
@@ -850,8 +854,6 @@ if __name__ == '__main__':
     for (opt, val) in options:
         if opt == '-d':                # Enable debug/progress messages
             debuglvl = int(val)
-            if debuglvl > 1:
-                logging.basicConfig(level=logging.DEBUG)
         elif opt == '-l':      # Logfile mode - report traffic read in
             logfile = val
         elif opt == '-n':      # Force the nick