do not use liblog's absolute path in DllImport
[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                 const string LibLog64 = "/system/lib64/liblog.so";
14
15                 TextWriter stdout;
16                 readonly string appname;
17                 StringBuilder line = new StringBuilder ();
18
19                 public LogcatTextWriter (string appname, TextWriter stdout)
20                 {
21                         this.appname = appname;
22                         this.stdout = stdout;
23                 }
24
25                 public override Encoding Encoding {
26                         get {return Encoding.UTF8;}
27                 }
28
29                 public override void Write (string s)
30                 {
31                         if (s != null)
32                                 foreach (char c in s)
33                                         Write (c);
34                 }
35
36                 public override void Write (char value)
37                 {
38                         if (value == '\n')
39                                 WriteLine ();
40                         else
41                                 line.Append (value);
42                 }
43
44                 public override void WriteLine ()
45                 {
46                         var o = line.ToString ();
47                         line.Clear ();
48
49                         Log (LogLevel.Info, appname, o);
50                         stdout.WriteLine (o);
51                 }
52
53                 enum LogLevel {
54                         Unknown,
55                         Default,
56                         Verbose,
57                         Debug,
58                         Info,
59                         Warn,
60                         Error,
61                         Fatal,
62                         Silent
63                 }
64
65                 public static bool IsRunningOnAndroid ()
66                 {
67                         return File.Exists (LibLog) || File.Exists (LibLog64);
68                 }
69
70                 [DllImport ("liblog")]
71                 static extern void __android_log_print (LogLevel level, string appname, string format, string args, IntPtr zero);
72
73                 static void Log (LogLevel level, string appname, string log)
74                 {
75                         __android_log_print (level, appname, "%s", log, IntPtr.Zero);
76                 }
77         }
78 }
79
80 #endif  // MONODROID