[Inteproxy-commits] r186 - in trunk: . inteproxy

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 11 18:11:11 CET 2009


Author: bh
Date: 2009-03-11 18:11:08 +0100 (Wed, 11 Mar 2009)
New Revision: 186

Modified:
   trunk/ChangeLog
   trunk/inteproxy/main.py
Log:
Better logging of httplib debug output:

* inteproxy/main.py (DebugLogOut): Helper class to redirect
sys.stdout to stdout and stderr for debuggig purposes
(setup_logging): If InteProxy is started with debug-level >= 2,
use DebugLogOut to have anything written to sys.stdout also
written to stderr.  This will make debug output from httplib,
which is written to stdout, end up in the InteProxy log.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-03-10 15:24:03 UTC (rev 185)
+++ trunk/ChangeLog	2009-03-11 17:11:08 UTC (rev 186)
@@ -1,3 +1,14 @@
+2009-03-11  Bernhard Herzog  <bh at intevation.de>
+
+	Better logging of httplib debug output:
+
+	* inteproxy/main.py (DebugLogOut): Helper class to redirect
+	sys.stdout to stdout and stderr for debuggig purposes
+	(setup_logging): If InteProxy is started with debug-level >= 2,
+	use DebugLogOut to have anything written to sys.stdout also
+	written to stderr.  This will make debug output from httplib,
+	which is written to stdout, end up in the InteProxy log.
+
 2009-03-10  Bernhard Herzog  <bh at intevation.de>
 
 	* HowTo-Release.txt: Add note about updating the copyright notice

Modified: trunk/inteproxy/main.py
===================================================================
--- trunk/inteproxy/main.py	2009-03-10 15:24:03 UTC (rev 185)
+++ trunk/inteproxy/main.py	2009-03-11 17:11:08 UTC (rev 186)
@@ -1,5 +1,5 @@
 #! /usr/bin/python
-# Copyright (C) 2006, 2007, 2008 by Intevation GmbH
+# Copyright (C) 2006, 2007, 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -24,7 +24,37 @@
 
 inteproxy_version = "0.4.1"
 
+class DebugLogOut:
 
+    """Helper class to redirect e.g. sys.stdout to sys.stderr
+
+    The httplib module writes debug output to stdout, but in InteProxy
+    we'd like to have that debug output in stderr.  At the same time,
+    legitimate output to sys.stdout should still appear in stdout.
+
+    Instances of this class simply write any string passed to its write
+    method to orig_stream and line by line to debug_stream, prefixing
+    each line with the given prefix.
+    """
+
+    def __init__(self, orig_stream, debug_stream, prefix):
+        self.orig_stream = orig_stream
+        self.debug_stream = debug_stream
+        self.prefix = prefix
+        self.beginning_of_line = True
+        self.rest_line = ""
+
+    def write(self, s):
+        self.orig_stream.write(s)
+        lines = s.split("\n")
+        for line in lines[:-1]:
+            full_line = self.rest_line + line
+            self.rest_line = ""
+            self.debug_stream.write(self.prefix + full_line + "\n")
+        for rest in lines[-1:]:
+            self.rest_line += rest
+
+
 def setup_logging(opts):
     """Sets up logging according to opts.
 
@@ -37,6 +67,11 @@
     is done, or a filename in which case sys.stderr is changed to write
     to that file.
 
+    If opts.debug_level is > 1, sys.stdout is replaced with an object
+    that writes to the original stdout and stderr to make sure that
+    debug output written to stdout (as is done by httplib) ends up in
+    the logfile too.
+
     The default behavior is trying to accomodate the following
     requirements:
 
@@ -54,7 +89,10 @@
     elif inteproxy.resources.in_py2exe():
         sys.stderr = open(os.devnull, "w")
 
+    if opts.debug_level > 1:
+        sys.stdout = DebugLogOut(sys.stdout, sys.stderr, "stdout: ")
 
+
 def setup_language():
     """Sets the language environment if it's not set yet.
     This function checks if any of the common language environment



More information about the Inteproxy-commits mailing list