开发过程中,经常不能将手机连接电脑,通过adb来抓取log,所以就把抓log的代码放到程序中,程序启动的时候调用抓取log的代码,这样就可以在本地查看log日志
/****
* 保存ErrorLog日志到本地
* @author Dong
* 做成单利
*/
public class SaveErrorLogToSDCard {
public static SaveErrorLogToSDCard instance = new SaveErrorLogToSDCard();
private SaveErrorLogToSDCard(){}
public static SaveErrorLogToSDCard getInstance() {
return instance;
}
public void dumpLog2File(String fileName) {
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-v");
cmdLine.add("threadtime");
cmdLine.add("-f");
cmdLine.add(Environment.getExternalStorageDirectory() + "/" + fileName);
File f = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
try { //捕获log
Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));
} catch (IOException e) {
e.printStackTrace();
}
}
}