2 // Helper for Console to allow indirect access to `stdout` using NSLog
5 // Sebastien Pouliot <sebastien@xamarin.com>
7 // Copyright 2012-2013 Xamarin Inc. All rights reserved.
14 using System.Runtime.InteropServices;
19 public static partial class Console {
21 class NSLogWriter : TextWriter {
22 [DllImport ("__Internal", CharSet=CharSet.Unicode)]
23 extern static void monotouch_log (string s);
25 [DllImport ("/usr/lib/libSystem.dylib")]
26 extern static int write (int fd, byte [] buffer, int n);
32 sb = new StringBuilder ();
35 public override System.Text.Encoding Encoding {
36 get { return System.Text.Encoding.UTF8; }
39 static void direct_write_to_stdout (string s)
41 byte [] b = Encoding.Default.GetBytes (s);
42 while (write (1, b, b.Length) == -1 && Marshal.GetLastWin32Error () == /* EINTR*/ 4)
46 public override void Flush ()
48 string s = sb.ToString ();
54 direct_write_to_stdout (s);
55 direct_write_to_stdout (Environment.NewLine);
61 // minimum to override - see http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx
62 public override void Write (char value)
71 // optimization (to avoid concatening chars)
72 public override void Write (string value)
76 if (value != null && value.Length >= CoreNewLine.Length && EndsWithNewLine (value))
83 bool EndsWithNewLine (string value)
85 for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) {
86 if (value [v] != CoreNewLine [i])
93 public override void WriteLine ()