[Telepathy-commits] [telepathy-python/master] Added debug_divert_messages to new telepathy.utils module.

Jonny Lamb jonny.lamb at collabora.co.uk
Tue Jan 6 03:06:33 PST 2009


Thanks to Laurent P <psycojoker at gmail.com> for the original patch.

Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>
---
 src/__init__.py |    1 +
 src/utils.py    |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100644 src/utils.py

diff --git a/src/__init__.py b/src/__init__.py
index e1fc7e2..4c2dace 100644
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -22,6 +22,7 @@ from telepathy._version import version, __version__
 from telepathy.constants import *
 from telepathy.errors import *
 from telepathy.interfaces import *
+from telepathy.utils import *
 
 import telepathy.client as client
 import telepathy.server as server
diff --git a/src/utils.py b/src/utils.py
new file mode 100644
index 0000000..ac4b204
--- /dev/null
+++ b/src/utils.py
@@ -0,0 +1,54 @@
+# telepathy-python - Base classes defining the interfaces of the Telepathy framework
+#
+# Copyright (C) 2008 Collabora Limited
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import os
+import sys
+
+def debug_divert_messages(filename):
+    """debug_divert_messages:
+    @filename: A file to which to divert stdout and stderr or None to do
+    nothing.
+
+    Open the given file for writing and duplicate its file descriptor to
+    be used for stdout and stderr. This has the effect of closing the previous
+    stdout and stderr, and sending all messages that would have gone there
+    to the given file instead.
+
+    By default the file is truncated and hence overwritten each time the
+    process is executed.
+    If the filename is prefixed with '+' then the file is not truncated and
+    output is added at the end of the file.
+    Passing None to this function is guaranteed to have no effect. This is
+    so you can call it with the recommended usage
+    debug_divert_messages (os.getenv('MYAPP_LOGFILE'))
+    and it won't do anything if the environment variable is not set."""
+
+    if filename is None:
+        return
+
+    try:
+        if filename.startswith('+'):
+            logfile = open(filename[1:], 'a')
+        else:
+            logfile = open(filename, 'w')
+    except IOError, e:
+        print "Can't open logfile '%s' : '%s'" % (filename, e)
+        return
+
+    sys.stdout = logfile
+    sys.stderr = logfile
-- 
1.5.6.5



More information about the Telepathy-commits mailing list