Jake
Jake

Reputation: 16837

Android: Reading adb logcat logs via application

I am trying to write an app which reads the logs created by adb logcat. Following the code on link1 and link2, I have the following code:

try {
        Process process = Runtime.getRuntime().exec("logcat");
        System.out.println("Process : " + process); // shows process id
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        System.out.println("Buffered reader : " + bufferedReader.readLine());
        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) {
    }

To test what the buffered reader is reading, I put a println, but I get a message "cannot bind tcp:5038". The above code does not read any logs. I also tried using "logcat *:V" but I did not get logs of even lowest priority.

I gave my app the permission: android.permission.READ_LOGS.

I am testing my code on Android emulator.

Can someone please point out what am I doing wrong.

Thanks for helping.

EDIT:

I tried "logcat -d" and I got one line of log. In the code, it can be observed that a try/catch block has been provided; when I remove the permission READ_LOGS from the app, no exception is raised and the bufferReader simply prints null (Usually when an app does not find a permission it requires, it raises an exception). What is the reason for this behavior ?

EDIT2:

I tried Log.d(TAG, log.toString()) and got more than one line of text. Can someone please explain the last question from the previous edit: when I remove the required permission, why is an exception not raised by the app ?

Upvotes: 1

Views: 3418

Answers (2)

cinesex solo
cinesex solo

Reputation: 31

You can never get the permission to read logs through Runtime.getRuntime().

The android dev team decided to stop granting these permissions to third-party apps. Only system apps can now get them.

More details:https://code.google.com/p/acra/issues/detail?id=100

Upvotes: 0

aaronvargas
aaronvargas

Reputation: 14152

You are likely getting the message "cannot bind tcp:5038" due to not having

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

in your manifest

Upvotes: 3

Related Questions