Android无法从应用程序运行logcat

时间:2011-06-02 19:23:36

标签: android logging logcat android-logcat

所以我正在编写一个分析器,它需要能够在分析会话期间记录异常。我的计划是使用logcat转储到SD卡或内部存储器上的文件,然后在分析会话完成时,将文件压缩并将其发送到服务器。我在清单中添加了android.permission.READ_LOGS,我的代码如下:

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是一个Process对象,然后在分析会话完成时调用exceptionLogger.destroy()

我看到的行为是在我指定的位置创建了文件,但文件中从未有任何日志记录输出。我在模拟器中使用dev工具应用程序强制在日志中显示未处理的异常,但我的logcat输出文件仍为空。有什么想法吗?

编辑:所以当我进入adb shell,然后将SU转到分配给我的应用程序的用户帐户时,运行logcat时会看到以下内容:

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

我原本以为将清单权限添加到我的应用程序会允许我这样做吗?

2 个答案:

答案 0 :(得分:13)

故事的寓意是:仔细检查您的清单文件中的权限是否实际上是READ_LOGS而不仅仅是READ_LOG。

答案 1 :(得分:1)

以防有人遇到同样的问题:当尝试从单元测试中读取logcat时,需要将权限添加到测试中的应用程序,而不是测试项目。仅向测试项目添加权限仍会给出权限错误。

相关问题