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) { }
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 longoption 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 packagecom.android.ddmuilib.logcat.LogCatMessageParserandcom.android.ddmuilib.logcat.LogCatPidToNameMapper.So, one workaround I can think of is to call up a shell (
adb shell), figure out your PID usingps, and then pipe the output of adb logcat to grep: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.