Merge pull request #665 from andreas-auerswald/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                         if (s != null)
31                                 foreach (char c in s)
32                                         Write (c);
33                 }
34
35                 public override void Write (char value)
36                 {
37                         if (value == '\n')
38                                 WriteLine ();
39                         else
40                                 line.Append (value);
41                 }
42
43                 public override void WriteLine ()
44                 {
45                         var o = line.ToString ();
46                         line.Clear ();
47
48                         Log (LogLevel.Info, appname, o);
49                         stdout.WriteLine (o);
50                 }
51
52                 enum LogLevel {
53                         Unknown,
54                         Default,
55                         Verbose,
56                         Debug,
57                         Info,
58                         Warn,
59                         Error,
60                         Fatal,
61                         Silent
62                 }
63
64                 public static bool IsRunningOnAndroid ()
65                 {
66                         return File.Exists (LibLog);
67                 }
68
69                 [DllImport (LibLog)]
70                 static extern void __android_log_print (LogLevel level, string appname, string format, string args, IntPtr zero);
71
72                 static void Log (LogLevel level, string appname, string log)
73                 {
74                         __android_log_print (level, appname, "%s", log, IntPtr.Zero);
75                 }
76         }
77 }
78
79 #endif  // MONODROID