编程过滤logcat的我的应用程序。不是为我工作 [英] Programmatically filtering logcat for my application. not working for me

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

问题描述

在一个Android应用程序,我试图让我的应用程序日志消息,并将其保存到文件
我使用下面的code。
我使用的是不同的标记每一个我班有几个人。
这样做的logcat -d给了我所有无关的消息..
把我的包的名字像

的logcat -d myapp.com:I *:•

不工作的结果是空的,但如果我这样做

的logcat -d MYCLASS1TAG:我MYCLASS2TAG *:•

然后它工作,但我有很多课。

如何才能把我的包名和得到的结果??

  {尝试
       工艺过程=调用Runtime.getRuntime()EXEC(logcat的-d);
的BufferedReader的BufferedReader =新的BufferedReader(新的InputStreamReader(process.getInputStream()));   串线;   而((行= bufferedReader.readLine())!= NULL)
    {
 //写信给我的文件在这里   }
  }赶上(IOException异常五){}


解决方案

我不知道如何在命令行做它,但ADT的Eclipse插件,您可以通过应用程序过滤。

编辑:我很好奇,所以我通过ADT源看着弄清楚ADT是怎么做的。长期和短期的是,它使用了 -v长选项,包括PID,每条消息,它不断地从PID到一个应用程序名称的地图。相关知情人士code文件是在包 com.android.ddmuilib.logcat.LogCatMessageParser com.android.ddmuilib.logcat.LogCatPidToNameMapper

所以,一个解决办法我能想到的是调用一个shell(亚行外壳),使用计算出你的PID PS ,然后管道亚行logcat的输出到grep的:

 亚行logcat -v长| grep的<您的PI​​D>

这将是一个痛苦位,因为你的PID会改变每次运行应用程序的时间,但是这是你怎么能在紧要关头做到这一点。

编辑:我刚刚注意到,长格式实际打印的PID在一行和下一行的消息,所以你可能需要使用类似的awk而不是grep的。我在外面测试一些东西,张贴随访。

In an android app I am trying to get my application log messages and saving them to file I am using the code below. I am using a different TAG for each of my class and there are several of them. doing logcat -d gives me all irrelevant messages.. putting my package name like

logcat -d myapp.com:I *:S

does not work the results are empty but if I do

logcat -d MYCLASS1TAG:I MYCLASS2TAG *:S

then it works, but I have many classes..

how can I just put my package name and get results ..??

try  {      
       Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

   String line;  

   while ((line = bufferedReader.readLine()) != null) 
    {


 //       write to my file here

   }
  }  catch (IOException e)    {   }

解决方案

I'm not sure how to do it from the command line, but the ADT plugin for Eclipse lets you filter by application.

edit: I was curious, so I looked through the ADT source to figure out how ADT does it. The long and short of it is that it uses the -v long option to include the PID with each message, and it keeps a Map from a PID to an app name. The relevant source code files are in the package com.android.ddmuilib.logcat.LogCatMessageParser and com.android.ddmuilib.logcat.LogCatPidToNameMapper.

So, one workaround I can think of is to call up a shell (adb shell), figure out your PID using ps, and then pipe the output of adb logcat to grep:

adb logcat -v long | grep <your PID>

It will be a bit of a pain since your PID will change every time you run the app, but that's how you can do it in a pinch.

edit: I just noticed that the long format actually prints the PID on one line and the message on the next line, so you may need to use something like awk instead of grep. I'll test a few things out and post a followup.

这篇关于编程过滤logcat的我的应用程序。不是为我工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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