Reputation: 1940
In C++, the file of the source code and current line number is decided by FILE and INLINE, which is decided at compiling time, is there any method in Java that could do the similar thing? The file and line number is decided at compiling time instead of runtime? this will be convenient for log. I kind of doubt that use runtime method to detect these information will decrease the performance.
Upvotes: 6
Views: 3275
Reputation: 201447
You could use Thread.getStackTrace()
and something like
System.out.println(Thread.currentThread().getStackTrace()[1]);
Output includes the current method and line number (if debugging was compiled in). For example,
com.stackoverflow.Main.main(Main.java:23)
Upvotes: 6