update readme (#21797)
[mono-project.git] / mcs / class / corlib / System.IO / LogcatTextWriter.cs
blob9642e5a8c41dc58856d975243cd0d3899b74960e
1 #if MONODROID
3 using System;
4 using System.IO;
5 using System.Text;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
9 using Mono;
11 namespace System.IO {
13 class LogcatTextWriter : TextWriter {
15 const string LibLog = "/system/lib/liblog.so";
16 const string LibLog64 = "/system/lib64/liblog.so";
18 readonly byte[] appname;
20 TextWriter stdout;
21 StringBuilder line = new StringBuilder ();
23 public LogcatTextWriter (string appname, TextWriter stdout)
25 this.appname = Encoding.UTF8.GetBytes (appname + '\0');
26 this.stdout = stdout;
29 public override Encoding Encoding {
30 get {return Encoding.UTF8;}
33 public override void Write (string s)
35 if (s != null)
36 foreach (char c in s)
37 Write (c);
40 public override void Write (char value)
42 if (value == '\n')
43 WriteLine ();
44 else
45 line.Append (value);
48 public override void WriteLine ()
50 var o = line.ToString ();
51 line.Clear ();
53 Log (o);
54 stdout.WriteLine (o);
57 void Log (string message)
59 const int buffer_size = 512;
61 unsafe {
62 if (Encoding.UTF8.GetByteCount (message) < buffer_size) {
63 try {
64 fixed (char *b_message = message) {
65 byte* buffer = stackalloc byte[buffer_size];
66 int written = Encoding.UTF8.GetBytes (b_message, message.Length, buffer, buffer_size - 1);
67 buffer [written] = (byte)'\0';
69 Log (buffer);
72 return;
73 } catch (ArgumentException) {
74 /* It might be due to a failure to encode a character, or due to a smaller than necessary buffer. In the
75 * secode case, we want to fallback to simply allocating on the heap. */
79 using (SafeStringMarshal str = new SafeStringMarshal(message)) {
80 Log ((byte*) str.Value);
85 unsafe void Log (byte* b_message)
87 fixed (byte *b_appname = appname) {
88 Log (b_appname, 1 << 5 /* G_LOG_LEVEL_MESSAGE */, b_message);
92 public static bool IsRunningOnAndroid ()
94 return File.Exists (LibLog) || File.Exists (LibLog64);
97 [MethodImpl(MethodImplOptions.InternalCall)]
98 static unsafe extern void Log (byte *appname, int level, byte *message);
102 #endif // MONODROID