现在的位置: 首页 > 综合 > 正文

保存 log 到手机内存的代码

2016年01月04日 ⁄ 综合 ⁄ 共 9586字 ⁄ 字号 评论关闭

1 package com.leader.hsj.utils;  
2   
3 import java.io.BufferedReader;  
4 import java.io.File;  
5 import java.io.FileNotFoundException;  
6 import java.io.FileOutputStream;  
7 import java.io.IOException;  
8 import java.io.InputStreamReader;  
9 import java.text.SimpleDateFormat;  
10 import java.util.Date;  
11   
12 import android.content.Context;  
13 import android.os.Environment;  
14   
15 /** 
16  * log日志统计保存 
17  *  
18  * @author 陈伟斌 
19  *  
20  */  
21   
22 public class LogcatHelper {  
23   
24     private static LogcatHelper INSTANCE = null;  
25     private static String PATH_LOGCAT;  
26     private LogDumper mLogDumper = null;  
27     private int mPId;  
28     private final static String LOG_FILE_DIR="leaderlog";  
29     /** 
30      *  
31      * 初始化目录 
32      *  
33      * */  
34     public void init(Context context) {  
35         if (Environment.getExternalStorageState().equals(  
36                 Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中  
37             PATH_LOGCAT = Environment.getExternalStorageDirectory()  
38                     .getAbsolutePath() + File.separator + LOG_FILE_DIR;  
39         } else {// 如果SD卡不存在,就保存到本应用的目录下  
40             PATH_LOGCAT = context.getFilesDir().getAbsolutePath()  
41                     + File.separator + LOG_FILE_DIR;  
42         }  
43         File file = new File(PATH_LOGCAT);  
44         if (!file.exists()) {  
45             file.mkdirs();  
46         }  
47     }  
48   
49     public static LogcatHelper getInstance(Context context) {  
50         if (INSTANCE == null) {  
51             INSTANCE = new LogcatHelper(context);  
52         }  
53         return INSTANCE;  
54     }  
55   
56     private LogcatHelper(Context context) {  
57         init(context);  
58         mPId = android.os.Process.myPid();  
59     }  
60   
61     public void start() {  
62         if (mLogDumper == null)  
63             mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);  
64         mLogDumper.start();  
65     }  
66   
67     public void stop() {  
68         if (mLogDumper != null) {  
69             mLogDumper.stopLogs();  
70             mLogDumper = null;  
71         }  
72     }  
73   
74     private class LogDumper extends Thread {  
75   
76         private Process logcatProc;  
77         private BufferedReader mReader = null;  
78         private boolean mRunning = true;  
79         String cmds = null;  
80         private String mPID;  
81         private FileOutputStream out = null;  
82   
83         public LogDumper(String pid, String dir) {  
84             mPID = pid;  
85             try {  
86                 out = new FileOutputStream(new File(dir, "leader-"  
87                         + getFileName() + ".log"));  
88             } catch (FileNotFoundException e) {  
89                 // TODO Auto-generated catch block  
90                 e.printStackTrace();  
91             }  
92   
93             /** 
94              *  
95              * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s 
96              *  
97              * 显示当前mPID程序的 E和W等级的日志. 
98              *  
99              * */  
100   
101             // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\"";  
102             // cmds = "logcat  | grep \"(" + mPID + ")\"";//打印所有日志信息  
103             // cmds = "logcat -s way";//打印标签过滤信息  
104             cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";  
105   
106         }  
107   
108         public void stopLogs() {  
109             mRunning = false;  
110         }  
111   
112         @Override  
113         public void run() {  
114             try {  
115                 logcatProc = Runtime.getRuntime().exec(cmds);  
116                 mReader = new BufferedReader(new InputStreamReader(  
117                         logcatProc.getInputStream()), 1024);  
118                 String line = null;  
119                 while (mRunning && (line = mReader.readLine()) != null) {  
120                     if (!mRunning) {  
121                         break;  
122                     }  
123                     if (line.length() == 0) {  
124                         continue;  
125                     }  
126                     if (out != null && line.contains(mPID)) {  
127                         out.write((getDateEN() + "  " + line + "\n")  
128                                 .getBytes());  
129                     }  
130                 }  
131   
132             } catch (IOException e) {  
133                 e.printStackTrace();  
134             } finally {  
135                 if (logcatProc != null) {  
136                     logcatProc.destroy();  
137                     logcatProc = null;  
138                 }  
139                 if (mReader != null) {  
140                     try {  
141                         mReader.close();  
142                         mReader = null;  
143                     } catch (IOException e) {  
144                         e.printStackTrace();  
145                     }  
146                 }  
147                 if (out != null) {  
148                     try {  
149                         out.close();  
150                     } catch (IOException e) {  
151                         e.printStackTrace();  
152                     }  
153                     out = null;  
154                 }  
155   
156             }  
157   
158         }  
159   
160     }  
161   
162     public static String getFileName() {  
163         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
164         String date = format.format(new Date(System.currentTimeMillis()));  
165         return date;// 2012年10月03日 23:41:31  
166     }  
167   
168     public static String getDateEN() {  
169         SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
170         String date1 = format1.format(new Date(System.currentTimeMillis()));  
171         return date1;// 2012-10-03 23:41:31  
172     }  
173   
174 }  

1.先加上此类

[java] view plaincopy

 

package com.leader.hsj.utils;  

  

import java.io.BufferedReader;  

import java.io.File;  

import java.io.FileNotFoundException;  

import java.io.FileOutputStream;  

import java.io.IOException;  

import java.io.InputStreamReader;  

import java.text.SimpleDateFormat;  

10 import java.util.Date;  

11   

12 import android.content.Context;  

13 import android.os.Environment;  

14   

15 /** 

16  * log日志统计保存 

17  *  

18  * @author 陈伟斌 

19  *  

20  */  

21   

22 public class LogcatHelper {  

23   

24     private static LogcatHelper INSTANCE = null;  

25     private static String PATH_LOGCAT;  

26     private LogDumper mLogDumper = null;  

27     private int mPId;  

28     private final static String LOG_FILE_DIR="leaderlog";  

29     /** 

30      *  

31      * 初始化目录 

32      *  

33      * */  

34     public void init(Context context) {  

35         if (Environment.getExternalStorageState().equals(  

36                 Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中  

37             PATH_LOGCAT = Environment.getExternalStorageDirectory()  

38                     .getAbsolutePath() + File.separator + LOG_FILE_DIR;  

39         } else {// 如果SD卡不存在,就保存到本应用的目录下  

40             PATH_LOGCAT = context.getFilesDir().getAbsolutePath()  

41                     + File.separator + LOG_FILE_DIR;  

42         }  

43         File file = new File(PATH_LOGCAT);  

44         if (!file.exists()) {  

45             file.mkdirs();  

46         }  

47     }  

48   

49     public static LogcatHelper getInstance(Context context) {  

50         if (INSTANCE == null) {  

51             INSTANCE = new LogcatHelper(context);  

52         }  

53         return INSTANCE;  

54     }  

55   

56     private LogcatHelper(Context context) {  

57         init(context);  

58         mPId = android.os.Process.myPid();  

59     }  

60   

61     public void start() {  

62         if (mLogDumper == null)  

63             mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);  

64         mLogDumper.start();  

65     }  

66   

67     public void stop() {  

68         if (mLogDumper != null) {  

69             mLogDumper.stopLogs();  

70             mLogDumper = null;  

71         }  

72     }  

73   

74     private class LogDumper extends Thread {  

75   

76         private Process logcatProc;  

77         private BufferedReader mReader = null;  

78         private boolean mRunning = true;  

79         String cmds = null;  

80         private String mPID;  

81         private FileOutputStream out = null;  

82   

83         public LogDumper(String pid, String dir) {  

84             mPID = pid;  

85             try {  

86                 out = new FileOutputStream(new File(dir, "leader-"  

87                         + getFileName() + ".log"));  

88             } catch (FileNotFoundException e) {  

89                 // TODO Auto-generated catch block  

90                 e.printStackTrace();  

91             }  

92   

93             /** 

94              *  

95              * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s 

96              *  

97              * 显示当前mPID程序的 EW等级的日志. 

98              *  

99              * */  

100   

101             // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\"";  

102             // cmds = "logcat  | grep \"(" + mPID + ")\"";//打印所有日志信息  

103             // cmds = "logcat -s way";//打印标签过滤信息  

104             cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";  

105   

106         }  

107   

108         public void stopLogs() {  

109             mRunning = false;  

110         }  

111   

112         @Override  

113         public void run() {  

114             try {  

115                 logcatProc = Runtime.getRuntime().exec(cmds);  

116                 mReader = new BufferedReader(new InputStreamReader(  

117                         logcatProc.getInputStream()), 1024);  

118                 String line = null;  

119                 while (mRunning && (line = mReader.readLine()) != null) {  

120                     if (!mRunning) {  

121                         break;  

122                     }  

123                     if (line.length() == 0) {  

124                         continue;  

125                     }  

126                     if (out != null && line.contains(mPID)) {  

127                         out.write((getDateEN() + "  " + line + "\n")  

128                                 .getBytes());  

129                     }  

130                 }  

131   

132             } catch (IOException e) {  

133                 e.printStackTrace();  

134             } finally {  

135                 if (logcatProc != null) {  

136                     logcatProc.destroy();  

137                     logcatProc = null;  

138                 }  

139                 if (mReader != null) {  

140                     try {  

141                         mReader.close();  

142                         mReader = null;  

143                     } catch (IOException e) {  

144                         e.printStackTrace();  

145                     }  

146                 }  

147                 if (out != null) {  

148                     try {  

149                         out.close();  

150                     } catch (IOException e) {  

151                         e.printStackTrace();  

152                     }  

153                     out = null;  

154                 }  

155   

156             }  

157   

158         }  

159   

160     }  

161   

162     public static String getFileName() {  

163         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  

164         String date = format.format(new Date(System.currentTimeMillis()));  

165         return date;// 20121003日 23:41:31  

166     }  

167   

168     public static String getDateEN() {  

169         SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

170         String date1 = format1.format(new Date(System.currentTimeMillis()));  

171         return date1;// 2012-10-03 23:41:31  

172     }  

173   

174 }  

2.加上权限

[html] view plaincopy

 

175 <uses-permission android:name="android.permission.READ_LOGS" />  

176 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  


3.调用

[java] view plaincopy

 

177 LogcatHelper.getInstance(this).start(); // log保存到文件,便于调试,实际发布时请注释掉  

使用 adb logcat -d > logcat.txt 这个命令也行

抱歉!评论已关闭.