1

I need to read and filter logcat logs within my application, of others applications. I found other questions and the most useful if Read logcat programmatically within application.

So I tried to write my code, but the result is always the same, that is it returns the logcat logs that made until the application was started.

public void getLog() {
    edLog.setText("");
    Log.e("imgspa", "ciao");
    try {
        //Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "*:I"});
        //Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "B4A:I"});
        //Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "-v", "time", "-e", "*VFY*"});
        //Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "-v", "time", "*:I"});
        //Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});
        Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "-v", "threadtime", "*:*"});
        BufferedReader br = new BufferedReader(new InputStreamReader(logcat.getInputStream()));
        String line;

        final StringBuilder sb = new StringBuilder();
        String separator = System.getProperty("line.separator");
        sb.append("inizio");
        sb.append(separator);
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append(separator);
        }
        sb.append("fine");
        edLog.setText(sb.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

As you can see I tried different solutions but no one can help me. I need to get the logcat, where the application name is "B4A".

Let me point out that: - I have a rooted device - I already added the READ_LOGS permissions

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
img.simone
  • 632
  • 6
  • 10
  • 23

3 Answers3

1

Since Android 4.1 it's no possible to read logs of other applications. See an explanation

For rooted device you need to obtain the READ_LOGS permission manually

String pname = getPackageName();
String[] CMDLINE = { "su", "-c", null };
if(getPackageManager().checkPermission(android.Manifest.permission.READ_LOGS, pname) != 0) {

    try {
       
        CMDLINE[2] = String.format("pm grant %s android.permission.READ_LOGS", pname);
        java.lang.Process p = Runtime.getRuntime().exec(CMDLINE);
        int res = p.waitFor();
        Log.d(TAG, "exec returned: " + res);
        if (res != 0)
            throw new Exception("failed to become root");
    } catch (Exception e) {
        Log.d(TAG, "exec(): " + e);
      
    }

}

Or just try it with .exec("su -c logcat -d")

Community
  • 1
  • 1
Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46
1

As of JellyBean, you cannot access the log in logcat that was not add there by your application. link

Auto-Droid ツ
  • 1,583
  • 1
  • 17
  • 31
0

The change is that third party applications can no longer get the read logs permission, however every app can read the logs containing only the lines they have written, without needing any permission.

Unless you rooted you cannot do this since Jelly Bean. See this Android bug report and this related discussion. Quote:

img.simone For more query you have to read this

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49