你如何扫描logcat的输出和事件后的基础上找到的文本? [英] How do you scan logcat output and post event based on found text?

查看:177
本文介绍了你如何扫描logcat的输出和事件后的基础上找到的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扫描logcat的活动,职位和活动,如果我找到我想要的文字。我想在这里的做法:

I would like to scan logcat activity and post and event if I find my desired text. I am trying to approach here:

<一个href="http://stackoverflow.com/questions/4661234/how-to-constanly-monitor-logcat-file/11876989?noredirect=1#comment23561182_11876989">Logging logcat的活动

但我想知道如何反应,我在日志中发现,如张贴事件让另一个对象可以处理它。

but I would like to know how to react to what I find in the log, such as posting an event so another object can handle it.

推荐答案

获取所有的日志消息的字符串作为张贴在你所提到的答案。然后,使用常规EX pressions简单搜索的字符串。

Get the string of all log messages as posted in the answer you mentioned. Then, simply search the string using regular expressions.

例如,

String result = //... get the logs ...

if(result.contains("trigger text i am looking for")){
  myObject.raiseEvent();
}

修改

如果你正在尝试做一个持续监测的logcat的,这将是困难得多。其一,这个过程可能会被关闭,没有警告。所以,你需要一种方法来保持它的运行,或者不断地检查其是否正在运行。

If you are trying to do a constant monitoring of the logcat, this will be much more difficult. For one, the process might get shut down without warning. So you need a way to keep it running, or to constantly check that it is running.

其次,你联系的溶液不会在这种情况下的工作,因为这将等待直到stopLogging被调用,然后返回整个日志的记录时间间隔。

Secondly, the solution you linked to would not work in this case, as that waits until stopLogging is called, then returns the entire log for the recorded interval.

此外,你必须修改code,使其具有触发字及其相关的回调函数列表来运行。

Also, you'll have to modify that code so that it has a list of trigger words and their associated callback functions to run.

while ((line = reader.readLine()) != null){
 for(int i =0; i < triggerList.size(); i++){
   TriggerPhrase trigger = triggerList.get(i);
   if(line.contains(trigger.phrase))
   {
     trigger.onTriggerPhrase(line);
   }
 } 
}

在这里TriggerPhrase是有一个String短语成员变量和一个对象来实现一个回调函数的简单类。

where TriggerPhrase is a simple class that has a String phrase member variable and an object that implements a callback function.

public static class TriggerPhrase implements TriggerListener{
  String phrase;
  TriggerListener callback;
}

public interface TriggerListener{
   public void onTriggerPhrase();
}

然后就开始收听日志之前,填充在LogCat中听众的triggerPhrases 列表

TriggerPhrase monkeyPhrase = new TriggerPhrase();
monkeyPhrase.phrase = "monkeys";
monkeyPhrase.callback = new TriggerListener(){
  public void onTriggerPhrase(){
    notifyUser("found the phrase `monkeys` in the logcat");
  }
};
triggerPhrases.add(monkeyPhrase);
//etc.

这篇关于你如何扫描logcat的输出和事件后的基础上找到的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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