Android 无法从应用程序运行 logcat

发布于 2024-11-13 04:31:51 字数 1874 浏览 4 评论 0原文

因此,我正在编写一个分析器,需要能够在分析会话期间记录异常。我的计划是使用 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()

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

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

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

我曾以为将清单权限添加到我的应用程序将允许我执行以下操作这?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2024-11-20 04:31:51

这个故事的寓意是:仔细检查清单文件中的许可实际上是 READ_LOGS 而不仅仅是 READ_LOG。

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.

凉风有信 2024-11-20 04:31:51

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文