0

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

Logging logcat activity

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.

Community
  • 1
  • 1
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • You have to be more specific about what you want to do. Are you going to have the scan run once? Do you want it running all the time (a lot more complicated)? – you786 May 07 '13 at 22:08
  • I want to run it continuously in the background, scanning for my desired substrings and then posting events when they are found. – gonzobrains May 07 '13 at 22:40

1 Answers1

1

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

For example,

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

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

EDIT

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.

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.

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);
   }
 } 
}

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();
}

Then before you start listening the logs, you populate the triggerPhrases List in the LogCat listener.

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.
you786
  • 3,659
  • 5
  • 48
  • 74
  • I was looking for a bit more detail on the event raising part. I need to know how I can have another object register for the text it is looking for and also how to notify it that it has been found. – gonzobrains May 07 '13 at 22:42
  • I will try this out now. Is there anything special I need to do in order to just get logcat info from the point where my app starts up and nothing before? – gonzobrains May 08 '13 at 01:11
  • Would "logcat -v time" do it? – gonzobrains May 08 '13 at 01:15
  • AFAIK logcat doesn't show old messages, just from when you run the process onward. Also I am in the chat room you made if you want to continue. – you786 May 08 '13 at 01:17