Trace the executionTag(s): Environment
JDK1.5+
Generate a StackTrace and then parse the StackTraceElements.[LogUtils.java]
public class LogUtils {
private static final String NEWLINE = System.getProperty("line.separator");
private LogUtils (){ }
public static String getStack(int deep) {
// deep = 0 no level, only current calling method
// n from "n" levels
StringBuilder sb = new StringBuilder();
StackTraceElement ste [] = Thread.currentThread().getStackTrace();
int k = 2; // startingpoint 0:getstacktrace() 1: getStack()
int j = ste.length - 1;
// process the stack
if (deep > j) deep = j;
else deep = deep + k;
while (k <= deep) {
String line = ste[k].toString();
sb.append(line + NEWLINE);
k++;
}
return sb.toString();
}
public static void main(String args[]){
Test test = new Test();
test.doit();
}
}
class Test {
public void doit() {
System.out.println("*Howto Trace only 1 level\n" + LogUtils.getStack(1));
System.out.println("*Howto Trace only 10 levels\n" + LogUtils.getStack(10));
System.out.println("*Howto Trace no level (current)\n" + LogUtils.getStack(0));
}
}
And the output should be
*Howto Trace only 1 level Test.doit(LogUtils.java:35) LogUtils.main(LogUtils.java:29) *Howto Trace only 10 levels Test.doit(LogUtils.java:36) LogUtils.main(LogUtils.java:29) *Howto Trace no level (current) Test.doit(LogUtils.java:37)
See also this Howto
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com