views:

703

answers:

6

I'm searching a way to include __LINE__ as a compile-time constant in outputted messages.

Various solutions seem to exist, but with a big runtime penalty as suggested in __LINE__ in JS and __LINE__ in C#. They are usually always based upon a runtime object StackFrame as log4j.

Using the log4j possibility of enabling/disabling on a needed basis isn't an option either since usually when an error happens, it's too late to enable the line numbers, and inlined code doesn't seem to have any line numbers any more. . Would it be possible to either :

  1. Pre-process the java source files before compilation, ideally with something integrated with Eclipse1, so the it can be tested on the developpement platform also.
  2. Be able to pre-process the class at loading time. The overhead would then be negligible by being amortized.

1. Actually I do use an ugly hack: calling a custom preprocessor just after checkout on the build server via ANT tricks

+4  A: 

It's impossible to get the effect of __LINE__ in C which happens at compile time.

You can get line number by calling a function like this at runtime,

public static int lineNumber() {
    return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
ZZ Coder
The debug-information about the line has to be compiled into the class-file, to access this information.
Mnementh
+2  A: 

If you compile with the debug=line option, the line information exists in the class file but the VM can throw that away at runtime. This depends on some options which you can specify when you start the Java program. Usually, the performance penalty for running with debug infos is around 5% plus a bit more memory in the perm gen space. Since this information is so invaluable, I see no reason to strip this information.

Your comment (Util.java(Inlined Compiled Code)) suggest that you're using aggressive optimizations. If you can, turn those off. That way, you can simply:

// For Java 1.4, use new Exception().getStackTrace()
int line = Thread.currentThread().getStackTrace()[0].getLineNumber();

when you need it (i.e. inside of catch or if (log.isInfoEnabled())).

As for __LINE__, the Java compiler has no support for this. Your only option is to use some hacks to filter the source. I suggest to define a variable int __LINE__ somewhere and set that to some random value (0 being random enough) when you need it. Then write a filter that replaces the number in __LINE__ = \d+ with the current line number. This allows to fix the line number in place.

Aaron Digulla
Why `new Exception().getStackTrace()` rather than `Thread.currentThread().getStackTrace()`?
David Moles
No reason but the fact that I didn't bother to search the Thread class for how to get the current stack and I knew that an Exception has it :)
Aaron Digulla
Steve Schnepp
`Thread.currentThread().getStackTrace()` doesn't seems to exists in 1.4.2... `new Exception().getStackTrace()` it will be then. And replaced at build time with the real line number for production.
Steve Schnepp
A: 

Out of the box I don't think Java offers what you need.

However if you were to write your own annotation processor and use the Java 6.0 compiler APIs I think you could solve this problem. I wouldn't care to guess how much work it would be to do so.

Note that Eclipse allows you to include your own annotation processing in its build mechanism.

djna
A: 

I don't know about such a thing in the Java-compiler. But you could write a simple program, that replaces a token like __LINE__ with the linenumber in the file and configure your buildprocess to execute this tool before compiling. Some sort of a preprocessor.

Mnementh
That's exactly what I would like to replace (see footnote 1.)
Steve Schnepp
A: 

I would use a logging framework for this, and let it deal with the messy details. Both java.util.logging and logback can easily show information about the caller similar to what LINE can provide.

The major benefit is that the information is only computed if necessary, so not outputting means no overhead.

Thorbjørn Ravn Andersen
A: 

That true that out the box, these macros are not available. However, using instrumentation, and debug information you can implements it. check http://www.gallot.be/?p=85

Dominique