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?

tshepang
  • 10,772
  • 21
  • 84
  • 127
Alex
  • 2,153
  • 4
  • 29
  • 43

2 Answers2

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;
    }
  }
}
James
  • 5
  • 1
  • 4
you786
  • 3,735
  • 4
  • 42
  • 67
  • where is "results" defined? – gonzobrains May 07 '13 at 21:04
  • @gonzobrains Right under where the ReadThread class is defined. – you786 May 07 '13 at 21:05
  • Yes, but unless I define another "results" outside of the class then results isn't defined for stopRecordingLogs(). – gonzobrains May 07 '13 at 21:26
  • 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. – gonzobrains May 07 '13 at 21:39
  • For all logcat activity, and I've used it in the same manner you've described. – you786 May 07 '13 at 21:40
  • 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? – gonzobrains May 07 '13 at 21:51
  • done. http://stackoverflow.com/questions/16429349/how-do-you-scan-logcat-output-and-post-event-based-on-found-text – gonzobrains May 07 '13 at 22:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29565/discussion-between-gonzobrains-and-you786) – gonzobrains May 07 '13 at 22:43
  • I fixed my null issue. I needed to add permissions to AndroidManifest.xml. – gonzobrains May 08 '13 at 01:28
  • It's been awhile, but I think I used android.permission.READ_LOGS. – gonzobrains Feb 08 '16 at 15:12
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"

Jiahao Liu
  • 21
  • 2