Merge pull request #194 from QuickJack/master
[mono.git] / mcs / class / corlib / System.IO / LogcatTextWriter.cs
1 #if MONODROID
2
3 using System;
4 using System.IO;
5 using System.Text;
6 using System.Runtime.InteropServices;
7
8 namespace System.IO {
9
10         class LogcatTextWriter : TextWriter {
11
12                 const string LibLog = "/system/lib/liblog.so";
13
14                 TextWriter stdout;
15                 readonly string appname;
16                 StringBuilder line = new StringBuilder ();
17
18                 public LogcatTextWriter (string appname, TextWriter stdout)
19                 {
20                         this.appname = appname;
21                         this.stdout = stdout;
22                 }
23
24                 public override Encoding Encoding {
25                         get {return Encoding.UTF8;}
26                 }
27
28                 public override void Write (string s)
29                 {
30                         foreach (char c in s)
31                                 Write (c);
32                 }
33
34                 public override void Write (char value)
35                 {
36                         if (value == '\n')
37                                 WriteLine ();
38                         else
39                                 line.Append (value);
40                 }
41
42                 public override void WriteLine ()
43                 {
44                         var o = line.ToString ();
45                         line.Clear ();
46
47                         Log (LogLevel.Info, appname, o);
48                         stdout.WriteLine (o);
49                 }
50
51                 enum LogLevel {
52                         Unknown,
53                         Default,
54                         Verbose,
55                         Debug,
56                         Info,
57                         Warn,
58                         Error,
59                         Fatal,
60                         Silent
61                 }
62
63                 public static bool IsRunningOnAndroid ()
64                 {
65                         return File.Exists (LibLog);
66                 }
67
68                 [DllImport (LibLog)]
69                 static extern void __android_log_print (LogLevel level, string appname, string format, string args, IntPtr zero);
70
71                 static void Log (LogLevel level, string appname, string log)
72                 {
73                         __android_log_print (level, appname, "%s", log, IntPtr.Zero);
74                 }
75         }
76 }
77
78 #endif  // MONODROID