Using GREP command to filter logcat in Android

六眼飞鱼酱① 提交于 2019-12-23 04:34:44

问题


I use the following code to read the logcat in Android.

Process process = Runtime.getRuntime().exec("logcat -v time");

Since the logcat file size is large, I have to filter the logcat messages. So I used the following code to filter logcat.

Process process = Runtime.getRuntime().exec("logcat -v time | grep -v -E \"(libloc|RPC)\"");

where (libloc|RPC) are tags.

But Grep code is not working in Android. Can anyone please help me with this?


回答1:


If grep is not working (I use regexp on the logcat UI directly) then you can make your own grep reading line by line.

Process process = Runtime.getRuntime().exec("logcat -v time");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((line = bufferedReader.readLine()) != null) {
  if(line.matches("^.*(libloc|RPC).*$")) {
     //code
  }
}

Reference




回答2:


I got the answer. I have to use the file name to use the grep command.

Runtime.getRuntime().exec("logcat -v time -f "+logCatFile.getAbsolutePath()+ " libloc:S RPC:S")


来源:https://stackoverflow.com/questions/17448912/using-grep-command-to-filter-logcat-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!