返回

android-code-saveLog

发布时间:2022-11-10 02:08:19 386
# 工具

一个工具类。第一个用来将logcat保存到文件,第二个用来获得logcat字符串。

public class Logcat {
public final static String TAG = "VLC/Util/Logcat";

/**
* Writes the current app logcat to a file.
*
* @param filename The filename to save it as
* @throws IOException
*/
public static void writeLogcat(String filename) throws IOException {
String[] args = { "logcat", "-v", "time", "-d" };

Process process = Runtime.getRuntime().exec(args);

InputStreamReader input = new InputStreamReader(process.getInputStream());

FileOutputStream fileStream;
try {
fileStream = new FileOutputStream(filename);
} catch( FileNotFoundException e) {
return;
}

OutputStreamWriter output = new OutputStreamWriter(fileStream);
BufferedReader br = new BufferedReader(input);
BufferedWriter bw = new BufferedWriter(output);

try {
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
}catch(Exception e) {}
finally {
bw.close();
output.close();
br.close();
input.close();
}
}

/**
* Get the last 500 lines of the application logcat.
*
* @return the log string.
* @throws IOException
*/
public static String getLogcat() throws IOException {
String[] args = { "logcat", "-v", "time", "-d", "-t", "500" };

Process process = Runtime.getRuntime().exec(args);
InputStreamReader input = new InputStreamReader(
process.getInputStream());
BufferedReader br = new BufferedReader(input);
StringBuilder log = new StringBuilder();
String line;

while ((line = br.readLine()) != null)
log.append(line + "\n");

br.close();
input.close();

return log.toString();
}

}

 

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
腾讯TKE容器日志托管详细教程 2022-11-10 01:51:43