[tor-commits] [ooni-probe/master] Refactor exception logging function

art at torproject.org art at torproject.org
Sun Nov 18 17:43:53 UTC 2012


commit 434cae18828435644150ac475012a1f7689936f0
Author: Arturo Filastò <art at fuffa.org>
Date:   Sun Nov 18 18:37:58 2012 +0100

    Refactor exception logging function
    * Print out the stack trace when the exception is of
      twisted.python.failure.Failure type.
---
 ooni/runner.py    |    4 ++--
 ooni/utils/log.py |   18 +++++++++++-------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/ooni/runner.py b/ooni/runner.py
index f7fa5dc..40aa40c 100644
--- a/ooni/runner.py
+++ b/ooni/runner.py
@@ -158,8 +158,8 @@ def runTestWithInput(test_class, test_method, test_input, oreporter):
         log.debug("runTestWithInput: concluded %s" % test_name)
         return oreporter.testDone(test_instance, test_name)
 
-    def test_error(error, test_instance, test_name):
-        log.exception(error)
+    def test_error(failure, test_instance, test_name):
+        log.exception(failure)
 
     test_instance = test_class()
     test_instance.input = test_input
diff --git a/ooni/utils/log.py b/ooni/utils/log.py
index 6f4622d..d9b57bb 100644
--- a/ooni/utils/log.py
+++ b/ooni/utils/log.py
@@ -9,6 +9,7 @@ import traceback
 import logging
 
 from twisted.python import log as txlog
+from twisted.python.failure import Failure
 from twisted.python.logfile import DailyLogFile
 
 from ooni.utils import otime
@@ -51,13 +52,16 @@ def debug(msg, *arg, **kw):
 def err(msg, *arg, **kw):
     txlog.err("Error: " + str(msg), logLevel=logging.ERROR, *arg, **kw)
 
-def exception(msg):
-    txlog.err(msg)
-    exc_type, exc_value, exc_traceback = sys.exc_info()
-    traceback.print_exception(exc_type, exc_value, exc_traceback)
-
-def exception(*msg):
-    logging.exception(msg)
+def exception(error):
+    """
+    Error can either be an error message to print to stdout and to the logfile
+    or it can be a twisted.python.failure.Failure instance.
+    """
+    if isinstance(error, Failure):
+        error.printTraceback()
+    else:
+        exc_type, exc_value, exc_traceback = sys.exc_info()
+        traceback.print_exception(exc_type, exc_value, exc_traceback)
 
 class LoggerFactory(object):
     """





More information about the tor-commits mailing list