Android Learner
Android Learner

Reputation: 2579

Programmatically filter device logs

I am implementing functionality to read device logs in my application.

For this purpose I have taken help from Reading logs programatically. This code snippet gives the device logs (logs of all running processe). Thats fine and working.

Now I want to filter application log from device log. So how can I do it programmatically?

Upvotes: 1

Views: 872

Answers (2)

Ragunath Jawahar
Ragunath Jawahar

Reputation: 19723

1) Reading logs programmatically does not work from Ice Cream Sandwich.

2) Use the PackageManager to get the package manager to get the package names of installed applications.

3) Use you ActivityManager to get the list of running processes.

Match the package names of applications with the running processes and find the process id. And filter logs that belong to the target app by package name or process id.

This is just the large picture.

Upvotes: 0

Aman Gautam
Aman Gautam

Reputation: 3579

Did you check the logcat reference? http://developer.android.com/tools/debugging/debugging-log.html#alternativeBuffers and http://developer.android.com/tools/help/logcat.html

You can try

public class LogTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      Process process = Runtime.getRuntime().exec("logcat -b main -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log=new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
      }
      TextView tv = (TextView)findViewById(R.id.textView1);
      tv.setText(log.toString());
    } catch (IOException e) {
    }
  }
}

All you have to do is to specify your command. You can test your command by running it on adb. To filter it further have a look at http://developer.android.com/tools/debugging/debugging-log.html#filteringOutput

Upvotes: 1

Related Questions