r164 - in /debtorrent/trunk: DebTorrent/BT1/AptListener.py DebTorrent/BT1/track.py DebTorrent/HTTPHandler.py DebTorrent/launchmanycore.py btdownloadheadless.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Sat Jul 14 00:01:45 UTC 2007


Author: camrdale-guest
Date: Sat Jul 14 00:01:45 2007
New Revision: 164

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=164
Log:
Move the HTTP logging to the HTTPHandler for clients and tracker.

Modified:
    debtorrent/trunk/DebTorrent/BT1/AptListener.py
    debtorrent/trunk/DebTorrent/BT1/track.py
    debtorrent/trunk/DebTorrent/HTTPHandler.py
    debtorrent/trunk/DebTorrent/launchmanycore.py
    debtorrent/trunk/btdownloadheadless.py

Modified: debtorrent/trunk/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/AptListener.py?rev=164&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/AptListener.py Sat Jul 14 00:01:45 2007
@@ -104,10 +104,6 @@
     @ivar trackerid: unknown
     @type prevtime: unknown
     @ivar prevtime: unknown
-    @type logfile: unknown
-    @ivar logfile: unknown
-    @type log: unknown
-    @ivar log: unknown
     @type allow_get: unknown
     @ivar allow_get: unknown
     @type uq_broken: unknown
@@ -169,28 +165,6 @@
         seed(self.trackerid)
                 
         self.prevtime = clock()
-        self.logfile = None
-        self.log = None
-        if (config['logfile']) and (config['logfile'] != '-'):
-            try:
-                self.logfile = config['logfile']
-                self.log = open(self.logfile,'a')
-                sys.stdout = self.log
-                print "# Log Started: ", isotime()
-            except:
-                print "**warning** could not redirect stdout to log file: ", sys.exc_info()[0]
-
-        if config['hupmonitor']:
-            def huphandler(signum, frame, self = self):
-                try:
-                    self.log.close ()
-                    self.log = open(self.logfile,'a')
-                    sys.stdout = self.log
-                    print "# Log reopened: ", isotime()
-                except:
-                    print "**warning** could not reopen logfile"
-             
-            signal.signal(signal.SIGHUP, huphandler)            
                 
         self.allow_get = config['allow_get']
         

Modified: debtorrent/trunk/DebTorrent/BT1/track.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/track.py?rev=164&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/track.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/track.py Sat Jul 14 00:01:45 2007
@@ -338,10 +338,6 @@
     @ivar prevtime: unknown
     @type timeout_downloaders_interval: C{int}
     @ivar timeout_downloaders_interval: seconds between expiring downloaders
-    @type logfile: unknown
-    @ivar logfile: unknown
-    @type log: unknown
-    @ivar log: unknown
     @type allow_get: unknown
     @ivar allow_get: unknown
     @type t2tlist: L{T2T.T2TList}
@@ -478,41 +474,6 @@
         self.prevtime = clock()
         self.timeout_downloaders_interval = config['timeout_downloaders_interval']
         rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval)
-        self.logfile = None
-        self.log = None
-        if (config['logfile']) and (config['logfile'] != '-'):
-            try:
-                self.logfile = config['logfile']
-                self.log = open(self.logfile,'a')
-                sys.stdout = self.log
-                print "# Log Started: ", isotime()
-            except:
-                print "**warning** could not redirect stdout to log file: ", sys.exc_info()[0]
-
-        if config['hupmonitor']:
-            def huphandler(signum, frame, self = self):
-                """Function to handle SIGHUP signals.
-                
-                Reopens the log file when a SIGHUP is received.
-                
-                @type signum: unknown
-                @param signum: ignored
-                @type frame: unknown
-                @param frame: ignored
-                @type self: L{Track}
-                @param self: the Track instance to reopen the log of
-                
-                """
-                
-                try:
-                    self.log.close ()
-                    self.log = open(self.logfile,'a')
-                    sys.stdout = self.log
-                    print "# Log reopened: ", isotime()
-                except:
-                    print "**warning** could not reopen logfile"
-             
-            signal.signal(signal.SIGHUP, huphandler)            
                 
         self.allow_get = config['allow_get']
         
@@ -1526,7 +1487,8 @@
     t = Tracker(config, r)
     r.bind(config['port'], config['bind'],
            reuse = True, ipv6_socket_style = config['ipv6_binds_v4'])
-    r.listen_forever(HTTPHandler(t.get, config['min_time_between_log_flushes']))
+    r.listen_forever(HTTPHandler(t.get, config['min_time_between_log_flushes'],
+                                 config['logfile'], config['hupmonitor']))
     t.save_state()
     print '# Shutting down: ' + isotime()
 

Modified: debtorrent/trunk/DebTorrent/HTTPHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/HTTPHandler.py?rev=164&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/HTTPHandler.py (original)
+++ debtorrent/trunk/DebTorrent/HTTPHandler.py Sat Jul 14 00:01:45 2007
@@ -16,10 +16,11 @@
 """
 
 from cStringIO import StringIO
-from sys import stdout
+from sys import stdout, exc_info
 import time
 from clock import clock
 from gzip import GzipFile
+import signal
 try:
     True
 except:
@@ -32,6 +33,21 @@
 
 months = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
+
+def isotime(secs = None):
+    """Create an ISO formatted string of the time.
+    
+    @type secs: C{float}
+    @param secs: number of seconds since the epoch 
+        (optional, default is to use the current time)
+    @rtype: C{string}
+    @return: the ISO formatted string representation of the time
+    
+    """
+    
+    if secs == None:
+        secs = time.time()
+    return time.strftime('%Y-%m-%d %H:%M UTC', time.gmtime(secs))
 
 class HTTPConnection:
     """A single connection from an HTTP client.
@@ -214,10 +230,10 @@
             ident = '-'
         else:
             ident = self.encoding
-        self.handler.log( self.connection.get_ip(), ident, '-',
-                          self.header, responsecode, len(data),
-                          self.headers.get('referer','-'),
-                          self.headers.get('user-agent','-') )
+        self.handler.write_log( self.connection.get_ip(), ident, '-',
+                                self.header, responsecode, len(data),
+                                self.headers.get('referer','-'),
+                                self.headers.get('user-agent','-') )
         self.done = True
         r = StringIO()
         r.write('HTTP/1.0 ' + str(responsecode) + ' ' + 
@@ -245,16 +261,26 @@
     @ivar minflush: the minimum amount of time between flushing the log
     @type lastflush: C{float}
     @ivar lastflush: the time of the last log flush
+    @type logfile: C{string}
+    @ivar logfile: the file name to write the logs to
+    @type log: C{file}
+    @ivar log: the file to write the logs to
     
     """
     
-    def __init__(self, getfunc, minflush):
+    def __init__(self, getfunc, minflush, logfile = None, hupmonitor = None):
         """Initialize the instance.
         
         @type getfunc: C{method}
         @param getfunc: the method to call with the processed GET requests
         @type minflush: C{float}
         @param minflush: the minimum amount of time between flushing the log
+        @type logfile: C{string}
+        @param logfile: the file to write the logs to
+            (optional, defaults to standard output)
+        @type hupmonitor: C{boolean}
+        @param hupmonitor: whether to reopen the log file on a HUP signal
+            (optional, default is False)
         
         """
         
@@ -262,6 +288,26 @@
         self.getfunc = getfunc
         self.minflush = minflush
         self.lastflush = clock()
+        self.logfile = None
+        self.log = None
+        if (logfile) and (logfile != '-'):
+            try:
+                self.logfile = logfile
+                self.log = open(self.logfile,'a')
+                print >> self.log, "# Log Started: ", isotime()
+            except:
+                print "**warning** could not open log file: ", exc_info()[0]
+            else:
+                if hupmonitor:
+                    def huphandler(signum, frame, self = self):
+                        try:
+                            self.log.close()
+                            self.log = open(self.logfile,'a')
+                            print >> self.log, "# Log reopened: ", isotime()
+                        except:
+                            print "**warning** could not reopen logfile"
+                     
+                    signal.signal(signal.SIGHUP, huphandler)            
 
     def external_connection_made(self, connection):
         """Create a new HTTPConnection object.
@@ -312,7 +358,7 @@
         if not c.data_came_in(data) and not c.closed:
             c.connection.shutdown(1)
 
-    def log(self, ip, ident, username, header,
+    def write_log(self, ip, ident, username, header,
             responsecode, length, referrer, useragent):
         """Print a log message.
         
@@ -336,10 +382,13 @@
         """
         
         year, month, day, hour, minute, second, a, b, c = time.localtime(time.time())
-        print '%s %s %s [%02d/%3s/%04d:%02d:%02d:%02d] "%s" %i %i "%s" "%s"' % (
+        print >> self.log, '%s %s %s [%02d/%3s/%04d:%02d:%02d:%02d] "%s" %i %i "%s" "%s"' % (
             ip, ident, username, day, months[month], year, hour,
             minute, second, header, responsecode, length, referrer, useragent)
         t = clock()
         if t - self.lastflush > self.minflush:
             self.lastflush = t
-            stdout.flush()
+            if self.log:
+                self.log.flush()
+            else:
+                stdout.flush()

Modified: debtorrent/trunk/DebTorrent/launchmanycore.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/launchmanycore.py?rev=164&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/launchmanycore.py (original)
+++ debtorrent/trunk/DebTorrent/launchmanycore.py Sat Jul 14 00:01:45 2007
@@ -367,7 +367,9 @@
             self.rawserver.bind(config['apt_port'], config['bind'],
                    reuse = True, ipv6_socket_style = config['ipv6_binds_v4'])
             self.rawserver.set_handler(HTTPHandler(self.aptlistener.get, 
-                                                   config['min_time_between_log_flushes']), 
+                                                   config['min_time_between_log_flushes'],
+                                                   config['logfile'], 
+                                                   config['hupmonitor']), 
                                        config['apt_port'])
     
             self.ratelimiter = RateLimiter(self.rawserver.add_task,

Modified: debtorrent/trunk/btdownloadheadless.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/btdownloadheadless.py?rev=164&op=diff
==============================================================================
--- debtorrent/trunk/btdownloadheadless.py (original)
+++ debtorrent/trunk/btdownloadheadless.py Sat Jul 14 00:01:45 2007
@@ -36,8 +36,6 @@
 from DebTorrent.clock import clock
 from DebTorrent import createPeerID, version
 from DebTorrent.ConfigDir import ConfigDir
-from DebTorrent.BT1.AptListener import AptListener
-from DebTorrent.HTTPHandler import HTTPHandler
 
 assert sys.version >= '2', "Install Python 2.0 or greater"
 try:




More information about the Debtorrent-commits mailing list