1

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 ?

4
  • It should be "logcat -d" afaik. But you might get an ANR then because it will not stop reading the log. You need to move the reading in a background thread, AsyncTask or something like that. Edit: forget that, no ANR.
    – zapl
    Commented Mar 17, 2012 at 16:27
  • can your TextView display more than 1 line (android:singleLine)? You could also try to do Log.d("TAG", log.toString()); after you do tv.setText(log.toString()); to verify that it's no display error. Or is there an Exception? If so please add it to your question.
    – zapl
    Commented Mar 17, 2012 at 18:05
  • @zapl I do get more than one line when I display using Log.d("TAG", log.toString()); Can you explain the second part as well: why does it not raise an exception and simply return log as null to the buffer reader when I remove the permission from the app ?
    – Jake
    Commented Mar 17, 2012 at 18:14
  • seems that Runtime#exec() does not throw an Exception if the command can't be executed. There is just nothing that can be read for the BufferedReader
    – zapl
    Commented Mar 17, 2012 at 18:20

2 Answers 2

3

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

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

in your manifest

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.