0

I am trying to obtain logcat output within a simple Android program using the following:

try {
      Process process = Runtime.getRuntime().exec("logcat -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log=new StringBuilder();
      String line = "";
      while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
      }

Unfortunately, bufferedReader.readline() always returns null.

What is the problem?

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • What API level are you testing on? I believe the ability for applications to read any logcat messages except their own was removed in one of the newer ones (Jelly Bean I think, but maybe ICS) This was a conscious decision made by the platform engineers in an attempt to make the system more secure. – FoamyGuy May 07 '13 at 23:39
  • I am on Android 4.0.3. I installed CatLog on my device and that works, so I am not sure what I am doing wrong in my test program. – gonzobrains May 07 '13 at 23:52

1 Answers1

1

It seems I needed to add the following permissions to the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />

This worked for me.

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • For 4.0.3, you only need READ_LOGS. See http://stackoverflow.com/questions/16795582/not-all-data-shown-when-android-logcat-is-read-programatically – Peri Hartman Nov 13 '13 at 21:23