Question

Am I able to listen for messages being places into the LogCat by my own application?

For example something like...

// Somewhere in my application (on a background service):
Log.i("myModule", "Something been done");

and....

    // Somewhere else something like... 
    LogCatListener logCatListener = new LogCatListener()
    {
       public void onInfoRecieved(String tag, String message)
       { 
          //Do whatever you want with the message
       }
    }

I'm an Android noob so be gentle with me!

Thanks.

Was it helpful?

Solution

Unfortunately it looks like you directly can't do that for typical Log calls (.d, .i, .w). If you look at the source code of the Log class (https://android.googlesource.com/platform/frameworks/base/+/android-4.3_r2.1/core/java/android/util/Log.java) you'll see that these calls are just translated into a println_native, a private function mapped to a native implementation.

Also, the Log class is final, so you can't extend it and hook into .d , .i , .e, .w

I'm afraid your only solution is to write a Log class wrapper if you need to capture those calls. This will work for your custom Log's but obviously not for the system-issued calls to the Log class.

But good news is there's a special Log function that allows listeners. There's a funny method on the Log class:

 public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler)

And you can set a handler that will be called when you do

   Log.wtf (TAG, message)

funny google method names :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top