Merge pull request #949 from ermshiperete/bug-novell-463149
[mono.git] / mcs / class / System / System.Diagnostics / DefaultTraceListener.cs
index 9b86f0cfd0bf51793dcfef35494fb14bec5e76e7..8eda0fa240fe3755986a765e1f4461e8831cd2eb 100644 (file)
@@ -3,11 +3,13 @@
 //
 // Authors:
 //   Jonathan Pryor (jonpryor@vt.edu)
+//   Atsushi Enomoto (atsushi@ximian.com)
 //
 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
 //
 // (C) 2002 Jonathan Pryor
+// (C) 2007 Novell, Inc.
 //
 
 //
@@ -35,12 +37,12 @@ using System;
 using System.IO;
 using System.Collections;
 using System.Diagnostics;
+using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Threading;
 
 namespace System.Diagnostics {
-
-       [ComVisible(false)]
        public class DefaultTraceListener : TraceListener {
 
                private static readonly bool OnWin32;
@@ -62,10 +64,19 @@ namespace System.Diagnostics {
                        OnWin32 = (Path.DirectorySeparatorChar == '\\');
 
                        if (!OnWin32) {
+#if TARGET_JVM
+                               string trace = java.lang.System.getProperty("MONO_TRACE");
+#else
                                // If we're running on Unix, we don't have OutputDebugString.
                                // Instead, send output to...wherever the MONO_TRACE_LISTENER environment
                                // variables says to.
                                String trace = Environment.GetEnvironmentVariable("MONO_TRACE_LISTENER");
+#endif
+
+#if MOBILE
+                               if (trace == null)
+                                       trace = ConsoleOutTrace;
+#endif
 
                                if (trace != null) {
                                        string file = null;
@@ -133,11 +144,10 @@ namespace System.Diagnostics {
                {
                }
 
-               // It's hard to do anything with a UI when we don't have Windows.Forms...
-               [MonoTODO]
+               [MonoTODO ("AssertUiEnabled defaults to False; should follow Environment.UserInteractive.")]
                public bool AssertUiEnabled {
-                       get {return assertUiEnabled;}
-                       set {/* ignore */}
+                       get { return assertUiEnabled; }
+                       set { assertUiEnabled = value; }
                }
 
                [MonoTODO]
@@ -149,15 +159,66 @@ namespace System.Diagnostics {
                public override void Fail (string message)
                {
                        base.Fail (message);
-                       WriteLine (new StackTrace().ToString());
                }
 
                public override void Fail (string message, string detailMessage)
                {
                        base.Fail (message, detailMessage);
+                       if (ProcessUI (message, detailMessage) == DialogResult.Abort)
+                               Thread.CurrentThread.Abort ();
                        WriteLine (new StackTrace().ToString());
                }
 
+               DialogResult ProcessUI (string message, string detailMessage)
+               {
+                       
+                       if (!AssertUiEnabled)
+                               return DialogResult.None;
+
+                       object messageBoxButtonsAbortRetryIgnore;
+                       MethodInfo msgboxShow;
+                       
+                       try {
+                               Assembly wfAsm = Assembly.Load (Consts.AssemblySystem_Windows_Forms);
+                               if (wfAsm == null)
+                                   return DialogResult.None;
+                               
+                               Type buttons = wfAsm.GetType ("System.Windows.Forms.MessageBoxButtons");
+                               messageBoxButtonsAbortRetryIgnore = Enum.Parse (buttons, "AbortRetryIgnore");
+                               msgboxShow = wfAsm.GetType ("System.Windows.Forms.MessageBox").GetMethod (
+                                       "Show",
+                                       new Type [] {typeof (string), typeof (string), buttons});
+                       } catch {
+                               return DialogResult.None;
+                       }
+
+                       if (msgboxShow == null || messageBoxButtonsAbortRetryIgnore == null)
+                               return DialogResult.None;
+
+                       string caption = String.Format ("Assertion Failed: {0} to quit, {1} to debug, {2} to continue", "Abort", "Retry", "Ignore");
+                       string msg = String.Format ("{0}{1}{2}{1}{1}{3}", message, Environment.NewLine, detailMessage, new StackTrace ());
+
+                       switch (msgboxShow.Invoke (null, new object [] {msg, caption, messageBoxButtonsAbortRetryIgnore}).ToString ()) {
+                       case "Ignore":
+                               return DialogResult.Ignore;
+                       case "Abort":
+                               return DialogResult.Abort;
+                       default:
+                               return DialogResult.Retry;
+                       }
+               }
+
+               enum DialogResult {
+                       None,
+                       Retry,
+                       Ignore,
+                       Abort
+               }
+
+#if TARGET_JVM
+               private void WriteDebugString (string message)
+               {
+#else
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static void WriteWindowsDebugString (string message);
 
@@ -166,6 +227,7 @@ namespace System.Diagnostics {
                        if (OnWin32)
                                WriteWindowsDebugString (message);
                        else
+#endif
                                WriteMonoTrace (message);
                }