阅读logcat的编程方式为应用程序 [英] Read logcat programmatically for an application

查看:153
本文介绍了阅读logcat的编程方式为应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过<一个href=\"http://stackoverflow.com/questions/12692103/read-logcat-programmatically-within-application\">this文章了。但似乎大多数它显示的文字是不是在我的应用程序。我怎样才能过滤器的消息,让它只显示日志我的申请。换句话说,我想表明它像一个在Android的工作室(只显示来自我的应用程序错误日志,显示时间标记等):

I've read this article already. But it seems most of the text it shows are not in my application. How can I filter the message and let it only shows the log for my application. In another word, I want to show it like the one in Android Studio (only showing the error log from my app, showing time stamp,etc):

我想是这样的logcat -d -v时间,但不起作用。任何想法?谢谢你。

I tried something like "logcat -d -v time", but doesn't work. Any idea? Thanks.

推荐答案

尝试以下code只得到您的应用程序日志。

Try following code to get logs of your application only.

public final class MyAppLogReader {

    private static final String TAG = MyAppLogReader.class.getCanonicalName();
    private static final String processId = Integer.toString(android.os.Process
            .myPid());

    public static StringBuilder getLog() {

        StringBuilder builder = new StringBuilder();

        try {
            String[] command = new String[] { "logcat", "-v", "threadtime" };

            Process process = Runtime.getRuntime().exec(command);

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processId)) {
                    builder.append(line);
                    //Code here
                }
            }
        } catch (IOException ex) {
            Log.e(TAG, "getLog failed", ex);
        }

        return builder;
    }
}

这篇关于阅读logcat的编程方式为应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆