问题
In an android app I am trying to get my application log messages and saving them to file I am using the code below. I am using a different TAG for each of my class and there are several of them. doing logcat -d gives me all irrelevant messages.. putting my package name like
logcat -d myapp.com:I *:S
does not work the results are empty but if I do
logcat -d MYCLASS1TAG:I MYCLASS2TAG *:S
then it works, but I have many classes..
how can I just put my package name and get results ..??
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
// write to my file here
}
} catch (IOException e) { }
回答1:
I'm not sure how to do it from the command line, but the ADT plugin for Eclipse lets you filter by application.
edit: I was curious, so I looked through the ADT source to figure out how ADT does it. The long and short of it is that it uses the -v long
option to include the PID with each message, and it keeps a Map from a PID to an app name. The relevant source code files are in the package com.android.ddmuilib.logcat.LogCatMessageParser
and com.android.ddmuilib.logcat.LogCatPidToNameMapper
.
So, one workaround I can think of is to call up a shell (adb shell
), figure out your PID using ps
, and then pipe the output of adb logcat to grep:
adb logcat -v long | grep <your PID>
It will be a bit of a pain since your PID will change every time you run the app, but that's how you can do it in a pinch.
edit: I just noticed that the long format actually prints the PID on one line and the message on the next line, so you may need to use something like awk instead of grep. I'll test a few things out and post a followup.
回答2:
So as I understand it you are trying to run logcat on the device to grab and save to file. Unfortunately in the current command line the filterspec only supports the tag:priority
format. You will need to run the output through your own line by line filter. You can probably automate this by figuring out your PID and then filtering lines based on that.
来源:https://stackoverflow.com/questions/9729085/programmatically-filtering-logcat-for-my-application-not-working-for-me