在Eclipse控制台中包含对应于System.out.print语句的文件名/行号 [英] Include filename/line number corresponding to System.out.print statements in Eclipse Console
问题描述
我的意图是在Eclipse控制台中显示生成输出的文件/行号,而不是消息的串行编号(尽管也可以是有趣的问题)。
我正在寻找类似于Chrome的JavaScript控制台,其中每一行都有一个链接,可以将您打印到打印的功能。
您可以创建一个定制的 PrintStream
来检查当前的堆栈跟踪,并在输出。然后,您可以使用 System.setOut
/ System.setErr
使所有stdout / stderr输出包含此信息。 / p>
通过格式化它,Eclipse会将其作为堆栈跟踪元素并生成链接。
这是一个完整的演示:
public class Main {
public static void main(String args []){
System。 setOut(new java.io.PrintStream(System.out){
private StackTraceElement getCallSite(){
for(StackTraceElement e:Thread.currentThread()
.getStackTrace() )
if(!e.getMethodName()。equals(getStackTrace)
&!e.getClassName()。equals(getClass()。getName()))
return e;
return null;
}
@Override
public void println(String s){
printl n((Object)s);
}
@Override
public void println(Object o){
StackTraceElement e = getCallSite();
String callSite = e == null? ?? :
String.format(%s。%s(%s:%d),
e.getClassName(),
e.getMethodName(),
e。 getFileName(),
e.getLineNumber());
super.println(o +\t\tat+ callSite);
}
});
System.out.println(Hello world);
printStuff();
}
public static void printStuff(){
System.out.println(More output!);
}
}
Eclipse控制台:
我认为这是一个黑客,我不会使用它,除了随意的调试。
Is there a way to show line numbers in the Eclipse console?
My intent here is to show in the Eclipse console the file/line number that produced the output, not a serial line numbering of the messages (although that could also be an interesting question).
I am looking for something similar to Chrome's javascript console, where every line has a link that takes you to the function that printed it.
You can create a custom PrintStream
that inspects the current stack trace and includes file / line numbers in the output. You can then use System.setOut
/System.setErr
to cause all stdout/stderr output to include this information.
By formatting it properly Eclipse will pick it up as stack trace elements and generate links.
Here's a complete demo:
public class Main {
public static void main(String args[]) {
System.setOut(new java.io.PrintStream(System.out) {
private StackTraceElement getCallSite() {
for (StackTraceElement e : Thread.currentThread()
.getStackTrace())
if (!e.getMethodName().equals("getStackTrace")
&& !e.getClassName().equals(getClass().getName()))
return e;
return null;
}
@Override
public void println(String s) {
println((Object) s);
}
@Override
public void println(Object o) {
StackTraceElement e = getCallSite();
String callSite = e == null ? "??" :
String.format("%s.%s(%s:%d)",
e.getClassName(),
e.getMethodName(),
e.getFileName(),
e.getLineNumber());
super.println(o + "\t\tat " + callSite);
}
});
System.out.println("Hello world");
printStuff();
}
public static void printStuff() {
System.out.println("More output!");
}
}
Eclipse Console:
I consider this to be a hack though, and I wouldn't use it for anything but casual debugging.
这篇关于在Eclipse控制台中包含对应于System.out.print语句的文件名/行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!