Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

logging - Android unable to run logcat from application

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...