2 回答
![?](http://img1.sycdn.imooc.com/5458502c00012d4a02200220-100-100.jpg)
TA贡献1895条经验 获得超3个赞
替换System.out为您自己的PrintStream,使用System.setOut(),使其匹配(不)所需的输出,在匹配子句上放置一个断点,并查看堆栈跟踪。
由于PrintStream有很多很多方法,而不是覆盖在生成(不)期望输出时可能调用的所有可能方法,让我们将所有PrintStream输出定向到FilterOutputStream我们自己设计的 a ;那么我们应该只需要覆盖一个方法,但是我们需要重建我们的字符串。
例子:
class Prospector extends FilterOutputStream {
int count = 0;
private OutputStream original;
public Prospector(OutputStream out) {
super(out);
original = out;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
String buffer = new String(b, off, len, StandardCharsets.ISO_8859_1);
if (buffer.contains("]------ get duration(=")) {
if (count == 0) {
new Exception().printStackTrace(original);
}
count++; // Set breakpoint here
}
super.write(b, off, len);
}
}
当程序启动时,安装一个新的PrintStream包装原件在我们的Prospector:
System.setOut(new PrintStream(new Prospector(System.out)));
在指定的行上设置断点。当程序在断点处停止时,查找堆栈跟踪以找到生成输出的类/方法。
当神秘类生成您的目标线时,可能类似于...
System.out.format("[%s %s]------ get duration(=%f) is %s -------%n",
"isDeveloper", "true", 3.552653, "test impl");
...这个方法可以被多次调用,buffer包含行的各个部分,连续。在上述情况下,这将是:
"["
"isDeveloper"
" "
"true"
"]------ get duration(="
"3.552653"
") is "
"test impl"
" -------"
"\n"
理想情况下,]------ get duration(=应该足够独特以找到您正在寻找的类/方法,但您可以根据需要进行调整。
也可以将整个文本格式化为一个String,并在一次调用中打印出来。这就是为什么.contains(...)用于匹配所需的输出。
添加回答
举报