我想将日志文件从设备拉到我的PC上。我该怎么做呢?
发布于 2014-03-04 22:10:34
我希望这段代码能帮助到一些人。我花了两天时间弄清楚如何从设备上登录,然后过滤它:
public File extractLogToFileAndWeb(){
//set a file
Date datum = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY);
String fullName = df.format(datum)+"appLog.log";
File file = new File (Environment.getExternalStorageDirectory(), fullName);
//clears a file
if(file.exists()){
file.delete();
}
//write log to file
int pid = android.os.Process.myPid();
try {
String command = String.format("logcat -d -v threadtime *:*");
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
result.append(currentLine);
result.append("\n");
}
}
FileWriter out = new FileWriter(file);
out.write(result.toString());
out.close();
//Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath());
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
//clear the log
try {
Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return file;
}
正如@mehdok所指出的
向清单添加读取日志的权限
<uses-permission android:name="android.permission.READ_LOGS" />
发布于 2012-09-30 11:18:43
我会使用这样的东西:
$adb logcat -d > logcat.txt
-d选项将整个循环缓冲区转储到文本文件中,如果您正在查找特定的操作/意图,请尝试
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
希望这能有所帮助:)
发布于 2016-05-26 20:28:49
对于那些对USB调试或使用adb
不感兴趣的人,有一个更简单的解决方案。在Android 6(不确定之前的版本)中,开发者工具下有一个选项: Take Bug Report
单击此选项将准备一份错误报告,并提示您将其保存到驱动器或通过电子邮件发送。
我发现这是获取日志的最简单方法。我不喜欢打开USB调试。
https://stackoverflow.com/questions/2882253
复制相似问题