问题
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