tangjie
tangjie

Reputation: 477

How I start a process to run logcat on Android?

I want to read Android system level log file.So I use the following code:

        Process mLogcatProc = null;
    BufferedReader reader = null;
    try {
        mLogcatProc = Runtime.getRuntime().exec(
                new String[] { "logcat", "-d",
                        "AndroidRuntime:E [Your Log Tag Here]:V *:S" });

        reader = new BufferedReader(new InputStreamReader(mLogcatProc
                .getInputStream()));

        String line;
        final StringBuilder log = new StringBuilder();
        String separator = System.getProperty("line.separator");

        while ((line = reader.readLine()) != null) {
            log.append(line);
            log.append(separator);
        }

    }

    catch (IOException e) {}
    finally {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {}

    }

I also used in AndroidManifest.xml. But I can't read any line. The StringBuilder log is empty. And the method mLogcatProc.waitFor return 0.

So how can I read the log ?

Upvotes: 1

Views: 4795

Answers (2)

pocketDev
pocketDev

Reputation: 205

The code in original post works if you add correct permission to your AndroidManifest.xml

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

PS: You have to replace the "[Your Log Tag Here]" in your code with your actual TAG of course ;-)

Upvotes: 2

user51058
user51058

Reputation:

You cannot do this from a normal (non-suid) android application.

This is because a process needs to be running as root (or from adb) to read the logcat file in Android. If you have a terminal emulator installed on your [rooted] phone, try running logcat – it denies you permission to the log buffer unless you su to root.

The reason your StringBuffer ends up being empty is because the process outputs the error message to stderr (and you're reading stdin via your BufferedReader).

Upvotes: 2

Related Questions