java 相当于 __func__
#include <stdio.h>
void someFunc(void) {
printf("%s\n"), __func__);
}
每次调用该函数时,都会打印:
someFunc
What is the java equal?
我发现了
(new Exception()).getStackTrace()[0].getMethodName()
但是
java.lang.Thread.currentThread().getStackTrace()[1].getMethodName()
这些看起来很荒谬,有更简单的方法吗?
#include <stdio.h>
void someFunc(void) {
printf("%s\n"), __func__);
}
Each time the function is called it will print:
someFunc
What is the java equivalent?
I have found
(new Exception()).getStackTrace()[0].getMethodName()
And
java.lang.Thread.currentThread().getStackTrace()[1].getMethodName()
But these just seem ridiculous, is there an easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,没有更简单的方法了。由于 Java 没有宏功能,因此没有与 C++ 版本直接等效的东西。
No, there's no easier way. As Java doesn't have a macro facility, there's nothing directly equivalent to the C++ version.
通过一些配置,您可以设置类似 log4j 的内容,其模式包含方法名称(以及您想要的任何其他详细信息),那么您所要做的就是:
这里的好处是您只需进行一次设置,然后您可以在任意多个不同的地方重复使用记录器。
With a bit of configuration you could set up something like log4j with a pattern that includes the method name (and any other details you want), then all you have to do is something like:
The benefit here is that you do the setup once, and then you can reuse the logger in as many different places as you want.
需要时,您可以使用 log4j 并使用 %M 模式输出方法名称 -> http://logging.apache.org/log4j/1.2 /apidocs/org/apache/log4j/PatternLayout.html
不过,该文档有以下警告:
他们可能使用类似于您的
(new Exception()).getStackTrace()[0].getMethodName()
的技巧,但这是在每个日志语句中使用的非常慢的调用。另一方面,log4j 是高度可定制的,您可以将方法名称输出添加到对性能不关键的代码子集中。
You may use log4j and use %M patter to output the method name, when needed -> http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
The documentation, though, has this warning:
They probably use the trick similar to your
(new Exception()).getStackTrace()[0].getMethodName()
, but this is a very slow call to use in every log statement.On the other hand log4j is highly customizable and you may add the method name output to a subset of your code that is not performance critical.
答案是否定的。
顺便说一句,您发现的两种方法本质上是等效的。如果
t
是当前线程,则t.getStackTrace()
通过创建Exception
实例并调用getStaceTrace()< /代码> 就可以了。
正如其他人所指出的,在 Java 中创建异常来捕获堆栈跟踪是一项昂贵的操作,因此您应该谨慎执行此操作。
The answer is No.
Incidentally, the two approaches you've found are essentially equivalent. If
t
is the current thread, thent.getStackTrace()
works by creating an instance ofException
and callinggetStaceTrace()
on it.As others have noted, creating an
Exception
to capture the stacktrace is an expensive operation in Java, so you should do this WITH CAUTION.接收封闭方法的 java.lang.reflect.Method 的另一种可笑方法是:
这样您不仅可以找到方法名称,还可以找到该方法的其他有趣功能,例如注释,参数化类型等。
我希望有一种更内置的方式在运行时获取元数据......
Another ridiculous way for receiving the
java.lang.reflect.Method
of the enclosing method would be:This way you can not only find the method name, but also other interesting features of the method such as annotations, parameterized types, etc.
I wish there has been a more built-in way of acquiring metadata at runtime...