How to constanly monitor LogCat file?
Asked Answered
E

2

5

I need to somehow monitor the LogCat log, meaning that while my service is running I need to read the LogCat for new entries. At this moment I know only how to retrieve once the Log:

Process mLogcatProc = null;
    BufferedReader reader = null;
    try
    {
            mLogcatProc = Runtime.getRuntime().exec(new String[]
                   {"logcat", "-d", "ActivityManager:I *:S" });        

            reader = new BufferedReader(new InputStreamReader
    (mLogcatProc.getInputStream()));

            String line;
            final StringBuilder log = new StringBuilder();
            String separator = System.getProperty("line.separator"); 

            while ((line = reader.readLine()) != null)
            {
                    log.append(line);
                    log.append(separator);
            }

If I remove the -d option it will not exit but also it will not either work. So how can I modify the bellow code in order to continuously read new entries from LogCat?

Ensheathe answered 11/1, 2011 at 18:31 Comment(4)
May I ask why you want to do this? It sounds like a generally bad idea.Mccallister
adb logcat? Why do you want logcat within your program?Rabblerouser
Because it seems to be the only way to know when an activity starts. For what ? For an application protection app. Like App Protector.. and if you look on the permissions it needs reading system logs...Ensheathe
Hey Alex , did you find the answer to this. I am looking to this as wellDebi
B
6

This is how I did it, using Jiahao Liu's suggestion:

ReadThread thread;

public void startRecordingLogs()
{
  if(thread == null || !thread.isAlive())
  {
    ReadThread thread = new ReadThread();
    thread.start();
  }
}

public String stopRecordingLogs()
{
  String results = thread.stopLogging();
  return results;
}

private class ReadThread extends Thread{

  String results;
  Process process;
  Object lockObject = new Object();

  public void run(){
    synchronized(lockObject)
    {
      process = Runtime.getRuntime().exec("logcat -v time");        

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

      String line;
      final StringBuilder log = new StringBuilder();
      String separator = System.getProperty("line.separator"); 

      while ((line = reader.readLine()) != null)
      {
        log.append(line);
        log.append(separator);
      }
    }

    results = log.toString();
  }

  public String stopLogging()
  {
    process.destroy();
    synchronized(lockObject)
    {
      return results;
    }
  }
}
Balbriggan answered 9/8, 2012 at 4:51 Comment(10)
where is "results" defined?Neville
@Neville Right under where the ReadThread class is defined.Balbriggan
Yes, but unless I define another "results" outside of the class then results isn't defined for stopRecordingLogs().Neville
Did this code work for logging all logcat activity or only for your app's particular activity? I need to log all activity. Actually, I don't really want to just log; I want to scan and raise events if certain items are found in the logcat activity.Neville
For all logcat activity, and I've used it in the same manner you've described.Balbriggan
Can you post your code for that? I need to scan for particular entries and then post an event of some sort for another object to handle. Do I need to create a new SO question for you to answer for that?Neville
done. #16429849Neville
let us continue this discussion in chatNeville
I fixed my null issue. I needed to add permissions to AndroidManifest.xml.Neville
It's been awhile, but I think I used android.permission.READ_LOGS.Neville
O
2

You can create a new thread to running the logcat(without the option -d) in the background.

Note: Use the buffer events instead of the process ActivityManager, it is more accurate. "logcat -b events"

Outbid answered 1/5, 2011 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.