changeset 1125:8208374e5bed

Apply fawzi's patch from #235. This has some issues which are addressed in my next commit.
author Frits van Bommel <fvbommel wxs.nl>
date Wed, 18 Mar 2009 15:20:07 +0100
parents e7f0c2b48047
children 899a2d90645b
files runtime/internal/dmain2.d runtime/internal/genobj.d
diffstat 2 files changed, 69 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/internal/dmain2.d	Wed Mar 18 15:03:17 2009 +0100
+++ b/runtime/internal/dmain2.d	Wed Mar 18 15:20:07 2009 +0100
@@ -230,28 +230,7 @@
             }
             catch (Exception e)
             {
-                while (e)
-                {
-                    if (e.file)
-                    {
-                       // fprintf(stderr, "%.*s(%u): %.*s\n", e.file, e.line, e.msg);
-                       console (e.classinfo.name)("@")(e.file)("(")(e.line)("): ")(e.toString)("\n");
-                    }
-                    else
-                    {
-                       // fprintf(stderr, "%.*s\n", e.toString());
-                       console (e.classinfo.name)(": ")(e.toString)("\n");
-                    }
-                    if (e.info)
-                    {
-                        console ("----------------\n");
-                        foreach (t; e.info)
-                            console (t)("\n");
-                    }
-                    if (e.next)
-                        console ("\n");
-                    e = e.next;
-                }
+                e.writeOut(delegate void(char[]s){ console(s); });
                 result = EXIT_FAILURE;
             }
             catch (Object o)
--- a/runtime/internal/genobj.d	Wed Mar 18 15:03:17 2009 +0100
+++ b/runtime/internal/genobj.d	Wed Mar 18 15:20:07 2009 +0100
@@ -905,9 +905,28 @@
 
 class Exception : Object
 {
+    struct FrameInfo{
+        long line;
+        ptrdiff_t offset;
+        size_t address;
+        char[] file;
+        char[] func;
+        char[256] charBuf;
+        void writeOut(void delegate(char[])sink){
+            char[25] buf;
+            sink(func);
+            sprintf(buf.ptr,"@%zx ",address);
+            sink(buf[0..strlen(buf.ptr)]);
+            sprintf(buf.ptr," %+td ",address);
+            sink(buf[0..strlen(buf.ptr)]);
+            sink(file);
+            sprintf(buf.ptr,":%ld",line);
+            sink(buf[0..strlen(buf.ptr)]);
+        }
+    }
     interface TraceInfo
     {
-        int opApply( int delegate( inout char[] ) );
+        int opApply( int delegate( ref FrameInfo fInfo ) );
     }
 
     char[]      msg;
@@ -916,25 +935,65 @@
     TraceInfo   info;
     Exception   next;
 
-    this( char[] msg, Exception next = null )
+    this( char[] msg, char[] file, long line, Exception next, TraceInfo info )
     {
+        // main constructor, breakpoint this if you want...
         this.msg = msg;
         this.next = next;
-        this.info = traceContext();
+        this.file = file;
+        this.line = cast(size_t)line;
+        this.info = info;
     }
 
-    this( char[] msg, char[] file, size_t line, Exception next = null )
+    this( char[] msg, Exception next=null )
     {
-        this(msg, next);
-        this.file = file;
-        this.line = line;
-        this.info = traceContext();
+        this(msg,"",0,next,rt_createTraceContext(null));
+    }
+
+    this( char[] msg, char[] file, long line, Exception next=null )
+    {
+        this(msg,file,line,next,rt_createTraceContext(null));
     }
 
     char[] toString()
     {
         return msg;
     }
+    
+    void writeOut(void delegate(char[])sink){
+        if (file)
+        {
+            char[25]buf;
+            sink(this.classinfo.name);
+            sink("@");
+            sink(file);
+            sink("(");
+            sprintf(buf.ptr,"%ld",line);
+            sink(buf[0..strlen(buf.ptr)]);
+            sink("): ");
+            sink(toString());
+            sink("\n");
+        }
+        else
+        {
+           sink(this.classinfo.name);
+           sink(": ");
+           sink(toString);
+           sink("\n");
+        }
+        if (info)
+        {
+            sink("----------------\n");
+            foreach (ref t; info){
+                t.writeOut(sink);
+                sink("\n");
+            }
+        }
+        if (next){
+            sink("\n");
+            next.writeOut(sink);
+        }
+    }
 }
 
 
@@ -968,7 +1027,7 @@
  *  An object describing the current calling context or null if no handler is
  *  supplied.
  */
-Exception.TraceInfo traceContext( void* ptr = null )
+extern(C) Exception.TraceInfo rt_createTraceContext( void* ptr ){
 {
     if( traceHandler is null )
         return null;