[corlib] Added metadata handlers to StackTrace
authorMarcos Henrich <marcos.henrich@xamarin.com>
Fri, 20 May 2016 11:03:46 +0000 (12:03 +0100)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Fri, 8 Jul 2016 21:40:38 +0000 (22:40 +0100)
Added a reflectable static method that can be used to customize data
displayed after a stack trace.

Added metadata handler that display the AOTID of the executing assembly.

mcs/class/corlib/System.Diagnostics/StackTrace.cs

index 420b497fc9a3d1f9bf8851094991bb8786c0cb29..8b3f76a439e41338abd97c8a61e04b1bc1d2bfc1 100644 (file)
@@ -37,6 +37,7 @@ using System.Security;
 using System.Security.Permissions;
 using System.Text;
 using System.Threading;
+using System.IO;
 
 namespace System.Diagnostics {
 
@@ -60,6 +61,15 @@ namespace System.Diagnostics {
                readonly StackTrace[] captured_traces;
                private bool debug_info;
 
+               private static Dictionary<string, Func<StackTrace, string>> metadataHandlers;
+
+               static StackTrace ()
+               {
+                       metadataHandlers = new Dictionary<string, Func<StackTrace, string>> ();
+
+                       InitMetadataHandlers ();
+               }
+
                [MethodImplAttribute (MethodImplOptions.NoInlining)]
                public StackTrace ()
                {
@@ -295,13 +305,38 @@ namespace System.Diagnostics {
                        }
 
                        AddFrames (sb);
+
+                       foreach (var handler in metadataHandlers) {
+                               var lines = handler.Value (this);
+                               using (var reader = new StringReader (lines)) {
+                                       string line;
+                                       while ((line = reader.ReadLine()) != null) {
+                                               sb.AppendLine ();
+                                               sb.Append (string.Format ("[{0}] {1}", handler.Key, line));
+                                       }
+                               }
+                       }
+
                        return sb.ToString ();
                }
 
+               
                internal String ToString (TraceFormat traceFormat)
                {
                        // TODO:
                        return ToString ();
                }
+
+               static void InitMetadataHandlers ()
+               {
+                       string aotid = Assembly.GetAotId ();
+                       if (aotid != "00000000-0000-0000-0000-000000000000")
+                               AddMetadataHandler ("AOTID", st => { return aotid; });
+               }
+
+               private static void AddMetadataHandler (string id, Func<StackTrace, string> handler)
+               {
+                       metadataHandlers.Add (id, handler);
+               }
        }
 }