Android unable to run logcat from application
Asked Answered
I

2

1

So I am writing a profiler that needs to be able to log exceptions during the profiling session. My plan was to use logcat to dump to a file either on the SD card or the internal storage and then when the profiling session was complete, zipping the file up and sending it up to the server. I added the android.permission.READ_LOGS in my manifest, and my code is as follows:

public void startLoggingExceptions() {
    String filename = null;
    String directory = null;
    String fullPath = null;
    String externalStorageState = null;

    // The directory will depend on if we have external storage available to us or not
    try {
        filename = String.valueOf(System.currentTimeMillis()) + ".log";
        externalStorageState = Environment.getExternalStorageState();

        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            if(android.os.Build.VERSION.SDK_INT <= 7) {
                directory = Environment.getExternalStorageDirectory().getAbsolutePath();
            } else {
                directory = ProfilerService.this.getExternalFilesDir(null).getAbsolutePath();
            }
        } else {
            directory = ProfilerService.this.getFilesDir().getAbsolutePath();
        }

        fullPath = directory + File.separator + filename;

        Log.w("ProfilerService", fullPath);
        Log.w("ProfilerService", "logcat -f " + fullPath + " *:E");

        exceptionLogger = Runtime.getRuntime().exec("logcat -f " + fullPath + " *:E");
    } catch (Exception e) {
        Log.e("ProfilerService", e.getMessage());
    }
}

exceptionLogger is a Process object, which I then call exceptionLogger.destroy() on when the profiling session is complete.

The behavior I am seeing is that the file is created where I specify but there is never any logging output in the file. I am using the dev tools application in the emulator to force an unhandled exception to show up in the logs, but my logcat output file remains empty. Any thoughts?

EDIT: So when I go into the adb shell, and then SU to the user account that is assigned to my application, I see the following when running logcat:

Unable to open log device '/dev/log/main': Permission denied

I had thought that adding the manifest permission to my app would then allow me to do this?

Illiquid answered 2/6, 2011 at 19:23 Comment(1)
That's weird, your Runtime.getRuntime().exec("...") part works on my device.Frug
I
15

And the moral of the story is: double check that your permission in your manifest file actually says READ_LOGS and not just READ_LOG.

Illiquid answered 2/6, 2011 at 19:48 Comment(0)
G
2

Just in case somebody runs into the same problem I had: When trying to read logcat from a Unit test, the permission needs to be added to the application under test, not the test project. Adding the permission to the test project only will still give permission errors.

Government answered 18/12, 2012 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.