简体   繁体   English

监听我自己的应用程序制作的LogCat Entires

[英]Listening for LogCat Entires made by my own application

Am I able to listen for messages being places into the LogCat by my own application? 我可以监听自己的应用程序将消息放入LogCat吗?

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! 我是Android的菜鸟,所以请对我温柔!

Thanks. 谢谢。

Unfortunately it looks like you directly can't do that for typical Log calls ( .d , .i , .w ). 不幸的是,对于典型的Log调用( .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. 如果您查看Log类的源代码( https://android.googlesource.com/platform/frameworks/base/+/android-4.3_r2.1/core/java/android/util/Log.java ),将会看到这些调用只是被转换为println_native ,这是映射到本机实现的私有函数。

Also, the Log class is final, so you can't extend it and hook into .d , .i , .e , .w 另外,Log类是最终的,因此您无法扩展它并挂接到.d.i.e.w

I'm afraid your only solution is to write a Log class wrapper if you need to capture those calls. 恐怕您唯一的解决方案是如果需要捕获这些调用,则编写Log类包装器。 This will work for your custom Log's but obviously not for the system-issued calls to the Log class. 这将适用于您的自定义Log,但显然不适用于系统发出的Log类调用。

But good news is there's a special Log function that allows listeners. 但是好消息是,有一个特殊的Log函数允许侦听器。 There's a funny method on the Log class: Log类上有一个有趣的方法:

 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 :) 有趣的谷歌方法名称:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM