Smarter println that shows the depth in the stack

Posted by Hectoret on Stack Overflow See other posts from Stack Overflow or by Hectoret
Published on 2010-06-02T11:52:22Z Indexed on 2010/06/02 11:53 UTC
Read the original article Hit count: 145

Filed under:
|
|

I am using System.out.println in my code to track the execution of a program and get some useful output. This creates results like this in the console:

Main function. Program starts.
Method getArea. Getting values
Method getSide. Side is 6
Method getArea. First value is 6
Method getSide. Side is 8
Method getArea. Second value is 8
Method getArea. Area is 48
Main function. The final area is 48

I would like to create tha method, which adds a space in front of the output every time the code goes deeper in the method call stack. For example, the same code but instead of using System.out.println, now with Misc.smartPrintln:

Main function. Program starts.
 Method getArea. Getting values
  Method getSide. Side is 6
 Method getArea. First value is 6
  Method getSide. Side is 8
 Method getArea. Second value is 8
 Method getArea. Area is 48
Main function. The final area is 48

The method would have this definition:

public static void smartPrintln(String string);

I don't know how to implement this functionality. Any ideas how to solve this? And, could the use of a logger offer this functionality?

© Stack Overflow or respective owner

Related posts about java

Related posts about debugging