2 // Helper for Console to allow indirect access to `stdout` using NSLog
5 // Sebastien Pouliot <sebastien@xamarin.com>
7 // Copyright 2012-2014 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 xamarin_log (string s);
25 [DllImport ("/usr/lib/libSystem.dylib")]
26 extern static /* ssize_t */ IntPtr write (int fd, byte [] buffer, /* size_t */ IntPtr 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 var len = (IntPtr) b.Length;
43 while ((int) write (1, b, len) == -1 && Marshal.GetLastWin32Error () == /* EINTR*/ 4)
47 public override void Flush ()
49 string s = sb.ToString ();
55 direct_write_to_stdout (s);
56 direct_write_to_stdout (Environment.NewLine);
62 // minimum to override - see http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx
63 public override void Write (char value)
72 // optimization (to avoid concatening chars)
73 public override void Write (string value)
77 if (value != null && value.Length >= CoreNewLine.Length && EndsWithNewLine (value))
84 /* Called from TextWriter:WriteLine(string) */
85 public override void Write(char[] buffer, int index, int count) {
88 if (buffer != null && buffer.Length >= CoreNewLine.Length && EndsWithNewLine (buffer))
95 bool EndsWithNewLine (string value)
97 for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) {
98 if (value [v] != CoreNewLine [i])
105 bool EndsWithNewLine (char[] value)
107 for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) {
108 if (value [v] != CoreNewLine [i])
115 public override void WriteLine ()