From 2a0e2e6d682a5db49cfc4500b5a08568f8bcb697 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Sat, 27 Apr 2013 11:03:16 -0400 Subject: [PATCH] Avoid using reflection to redirect Console.Write* to NSLog --- mcs/class/corlib/System/Console.cs | 11 ++-- mcs/class/corlib/System/Console.iOS.cs | 91 ++++++++++++++++++++++++++++++++++ mcs/class/corlib/corlib.dll.sources | 1 + 3 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 mcs/class/corlib/System/Console.iOS.cs diff --git a/mcs/class/corlib/System/Console.cs b/mcs/class/corlib/System/Console.cs index bfe6929b500..929184985b4 100644 --- a/mcs/class/corlib/System/Console.cs +++ b/mcs/class/corlib/System/Console.cs @@ -39,7 +39,7 @@ using System.Text; namespace System { - public static class Console + public static partial class Console { #if !NET_2_1 private class WindowsConsole @@ -158,17 +158,16 @@ namespace System stdin = new CStreamReader (OpenStandardInput (0), inputEncoding); } else { #endif -#if FULL_AOT_RUNTIME - Type nslogwriter = Type.GetType ("MonoTouch.Foundation.NSLogWriter, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"); - stdout = (TextWriter) Activator.CreateInstance (nslogwriter); +#if MONOTOUCH + stdout = new NSLogWriter (); #else stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding); ((StreamWriter)stdout).AutoFlush = true; #endif stdout = TextWriter.Synchronized (stdout, true); -#if FULL_AOT_RUNTIME - stderr = (TextWriter) Activator.CreateInstance (nslogwriter); +#if MONOTOUCH + stderr = new NSLogWriter (); #else stderr = new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding); ((StreamWriter)stderr).AutoFlush = true; diff --git a/mcs/class/corlib/System/Console.iOS.cs b/mcs/class/corlib/System/Console.iOS.cs new file mode 100644 index 00000000000..e362e1390df --- /dev/null +++ b/mcs/class/corlib/System/Console.iOS.cs @@ -0,0 +1,91 @@ +// +// Helper for Console to allow indirect access to `stdout` using NSLog +// +// Authors: +// Sebastien Pouliot +// +// Copyright 2012-2013 Xamarin Inc. All rights reserved. +// + +#if MONOTOUCH + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; + +namespace System { + + public static partial class Console { + + class NSLogWriter : TextWriter { + + [DllImport ("__Internal", CharSet=CharSet.Unicode)] + extern static void monotouch_log (string s); + + StringBuilder sb; + + public NSLogWriter () + { + sb = new StringBuilder (); + } + + public override System.Text.Encoding Encoding { + get { return System.Text.Encoding.UTF8; } + } + + public override void Flush () + { + try { + monotouch_log (sb.ToString ()); + sb.Length = 0; + } + catch (Exception) { + } + } + + // minimum to override - see http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx + public override void Write (char value) + { + try { + sb.Append (value); + } + catch (Exception) { + } + } + + // optimization (to avoid concatening chars) + public override void Write (string value) + { + try { + sb.Append (value); + if (value != null && value.Length >= CoreNewLine.Length && EndsWithNewLine (value)) + Flush (); + } + catch (Exception) { + } + } + + bool EndsWithNewLine (string value) + { + for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) { + if (value [v] != CoreNewLine [i]) + return false; + } + + return true; + } + + public override void WriteLine () + { + try { + Flush (); + } + catch (Exception) { + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/mcs/class/corlib/corlib.dll.sources b/mcs/class/corlib/corlib.dll.sources index fa1e193ef43..fcda3a27168 100644 --- a/mcs/class/corlib/corlib.dll.sources +++ b/mcs/class/corlib/corlib.dll.sources @@ -114,6 +114,7 @@ System/CLSCompliantAttribute.cs System/CStreamReader.cs System/CStreamWriter.cs System/Console.cs +System/Console.iOS.cs System/ConsoleCancelEventArgs.cs System/ConsoleCancelEventHandler.cs System/ConsoleColor.cs -- 2.11.4.GIT