gpt4 book ai didi

java - 如何使用调试日志信息动态生成堆栈帧

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:55 25 4
gpt4 key购买 nike

为了更好的调试,我经常希望:

Exception 
at com.example.blah.Something.method()
at com.example.blah.Xyz.otherMethod()
at com.example.hello.World.foo()
at com.example.debug.version_3_8_0.debug_info_something.Hah.method() // synthetic method
at com.example.x.A.wrappingMethod()

如上所示的调试堆栈帧将动态生成,就像 java.lang.reflect.Proxy 一样。 , 除了我想完全控制最终出现在代理上的整个完全限定的方法名称。

在调用站点,我会做一些愚蠢而简单的事情:

public void wrappingMethod() {
run("com.example.debug.version_3_8_0.debug_info_something.Hah.method()", () -> {
World.foo();
});
}

如您所见,wrappingMethod() 是一个真正的方法,最终出现在堆栈跟踪中,Hah.method() 是一个动态生成的方法,而World.foo() 又是一个真正的方法。

是的,我知道这会污染已经很深的堆栈跟踪。别担心。我有我的理由。

是否有一种(简单的)方法可以做到这一点或与上述类似的方法?

最佳答案

不需要代码生成来解决这个问题:

static void run(String name, Runnable runnable) {
try {
runnable.run();
} catch (Throwable throwable) {
StackTraceElement[] stackTraceElements = throwable.getStackTrace();
StackTraceElement[] currentStackTrace = new Throwable().getStackTrace();
if (stackTraceElements != null && currentStackTrace != null) { // if disabled
int currentStackSize = currentStackStrace.length;
int currentFrame = stackTraceElements.length - currentStackSize - 1;
int methodIndex = name.lastIndexOf('.');
int argumentIndex = name.indexOf('(');
stackTraceElements[currentFrame] = new StackTraceElement(
name.substring(0, methodIndex),
name.substring(methodIndex + 1, argumentIndex),
null, // file name is optional
-1); // line number is optional
throwable.setStackTrace(stackTraceElements);
}
throw throwable;
}
}

通过代码生成,您可以添加一个带有名称的方法,在方法中重新定义调用站点,展开框架并调用生成的方法,但这会增加很多工作量,而且永远不会同样稳定。

这种策略在测试框架中是一种相当常见的方法,we do it a lot in Mockito以及 JRebel 等其他实用程序通过重写异常堆栈帧来隐藏它们的魔力。

当使用 Java 9 时,使用 Stack Walker API 进行此类操作会更有效。 .

关于java - 如何使用调试日志信息动态生成堆栈帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39712695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com