]> hydra-www.ietfng.org Git - dyna2/commitdiff
Crash handler. All uncatch exceptions are beautifully formatted and logged.
authortimv <tim.f.vieira@gmail.com>
Fri, 14 Jun 2013 14:42:00 +0000 (10:42 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 18 Jun 2013 01:21:55 +0000 (21:21 -0400)
src/Dyna/Backend/Python/interpreter.py
src/Dyna/Backend/Python/repl.py
src/Dyna/Backend/Python/utils.py

index da10aeb0a60726adc134c5f41c7a184352f52521..fdd6b8162e43658ecf5faeb9af26c74065974483 100644 (file)
@@ -203,7 +203,7 @@ import debug
 from chart import Chart, Term, _repr
 from defn import aggregator
 from utils import ip, red, green, blue, magenta, yellow, \
-    notimplemented, parse_attrs, ddict, dynac, \
+    notimplemented, parse_attrs, ddict, dynac, enable_crash_handler, \
     DynaCompilerError, DynaInitializerException
 from prioritydict import prioritydict
 from config import dotdynadir, dynahome
@@ -641,6 +641,9 @@ def main():
 
     interp = Interpreter()
 
+    enable_crash_handler()
+
+
     if args.profile:
         # When profiling, its common practice to disable the garbage collector.
         import gc
index aac79fe02ef6d95dcb554029a04969a2dcaa1db2..7758c8b6298c6d3ccf1003d9f0e2840e4b02771a 100644 (file)
@@ -1,10 +1,9 @@
-import os
-import cmd, readline
-import interpreter
-from utils import blue, yellow, green, magenta, ip, DynaCompilerError, DynaInitializerException
+import os, cmd, readline
+
+import debug, interpreter
+from utils import DynaCompilerError, DynaInitializerException, ip, crash_handler
 from chart import _repr
 from config import dotdynadir
-import debug
 
 
 class REPL(cmd.Cmd, object):
@@ -126,8 +125,11 @@ class REPL(cmd.Cmd, object):
         try:
             super(REPL, self).cmdloop()
         except KeyboardInterrupt:
+            # Catch Control-C and resume REPL.
             print '^C'
-            self.cmdloop()
-        except Exception as e:
+            self.cmdloop()   
+        except:
+            # report uncatch exception.
+            crash_handler()
+        finally:
             readline.write_history_file(self.hist)
-            raise e
index 4a35be758a2f59eec3e76740d03076a546405052..4a78017e7b5977b538935e61280864f5e0716437 100644 (file)
@@ -1,7 +1,8 @@
-import re
+import re, sys
 from subprocess import Popen, PIPE
 from IPython.frontend.terminal.embed import InteractiveShellEmbed
-from config import dynahome
+from IPython.core.ultratb import VerboseTB
+from config import dynahome, dotdynadir
 
 
 # interactive IPython shell
@@ -45,6 +46,43 @@ def dynac(f, out):
         raise DynaCompilerError(stderr)
 
 
+def crash_handler():
+    exception_handler(*sys.exc_info())
+
+
+def exception_handler(etype, evalue, tb):
+
+    # once for the log file.
+    with file(dotdynadir / 'crash.log', 'wb') as crashreport:
+        h = VerboseTB(color_scheme='Linux', call_pdb=False,
+                      ostream=crashreport,
+                      long_header=True, include_vars=True,
+                      check_cache=None)
+        h(etype, evalue, tb)
+
+    # once for the user
+    h = VerboseTB(color_scheme='Linux', call_pdb=False, ostream=None,
+                  tb_offset=0, long_header=False, include_vars=True,
+                  check_cache=None)
+    h(etype, evalue, tb)
+
+    # TODO: we should package up all relevant state including compiler
+    # version, codegen output, interpreter state (possibly without the
+    # chart -- because it might be too big to email); input to repl.
+    # This should all go into a tarball.
+
+    print 'FATAL ERROR (%s): %s' % (etype.__name__, evalue)
+    print 'Please report this error by emailing bugs@dyna.org. ' \
+        'Please attach the following file %s' % crashreport.name
+
+
+def enable_crash_handler():
+    """
+    Use our custom exception handler for handling uncaught exceptions.
+    """
+    sys.excepthook = exception_handler
+
+
 def notimplemented(*_,**__):
     raise NotImplementedError
 
@@ -214,6 +252,6 @@ if __name__ == '__main__':
         btree = t(x)
 
         assert check_binary(btree)
-        print 
+        print
         print 'sentence(%s) :=\n%s.' % (i, pretty(btree, 4).rstrip())
         print