简体   繁体   English

如何获取Android / Java中调试输出的方法名称?

[英]How to get method name for debug output in Android/Java?

I would like to make a macro that prints out the current method's name during Log.d and Log.e output. 我想创建一个在Log.d和Log.e输出期间打印出当前方法名称的宏。 Right now I simply type the method name within a hard-coded string, but this is obviously inefficient should the method name change in the future, as each string needs to be searched for and replaced. 现在,我只需在硬编码的字符串中键入方法名称,但是如果将来更改方法名称,这显然效率不高,因为需要搜索并替换每个字符串。

I am aware of using getMethodName() as indicated in this post: 我知道如这篇文章所述使用getMethodName():

How to get method name in Java 如何在Java中获取方法名称

This one also looks promising: 这个看起来也很有希望:

Debugging with helper extension method 使用辅助程序扩展方法进行调试

There are countless numbers of these posts on SO, but I'd like to find the best way for debugging purposes that does not impact runtime performance too much. SO上有无数此类帖子,但我想找到一种最佳的调试方法,该方法不会对运行时性能产生太大影响。 Since I am using Eclipse I would like to find a solution that works well with that IDE. 由于我使用的是Eclipse,因此我想找到一个可以与该IDE一起很好地工作的解决方案。

This is what I know of that is available to you for the current execution. 我知道这对于您当前的执行情况是可用的。

    Thread current = Thread.currentThread();
    StackTraceElement[] stack = current.getStackTrace();
    for(StackTraceElement element : stack)
    {
        if (!element.isNativeMethod()) {
            String className = element.getClassName();
            String fileName = element.getFileName();
            int lineNumber = element.getLineNumber();
            String methodName = element.getMethodName();
        }
    }

this is a simple yet very useful eclipse plug-in i mentioned in the comment above: download plugin 这是我在上面的评论中提到的一个简单但非常有用的Eclipse插件: 下载插件

its an incremental builder so typical build time is measured in fractions of seconds. 它是一个增量构建器,因此典型的构建时间以秒为单位。 add it to your .project file as a new builder <buildCommand> with name: 将其作为新生<buildCommand>添加到您的.project文件中,其名称为:

<name>org.pskink.logger.builder</name>

it should be the last builder in <buildSpec> list 它应该是<buildSpec>列表中的最后一个生成器

installation: just copy it to plugins folder and restart eclipse 安装:只需将其复制到plugins文件夹并重新启动eclipse

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

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