Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__LINE__ equivalent in Java?

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

like image 422
Steve Schnepp Avatar asked Aug 28 '09 11:08

Steve Schnepp


3 Answers

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();
}
like image 115
ZZ Coder Avatar answered Oct 28 '22 14:10

ZZ Coder


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.

like image 24
Aaron Digulla Avatar answered Oct 28 '22 15:10

Aaron Digulla


I've found this class makes things easy if you're looking for "got to here, got to here" type logging. I have a file Here.java that I add to my projects that contains just the following class definition:

public class Here {
    public static String at () {
        StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
        String where = ste.getClassName() + " " + ste.getMethodName() + " " + ste.getLineNumber() + " ";
        return where;
    }
}

To use it in its simplest form:

Log.v(TAG, Here.At());

Or, if you want to see other stuff:

Log.v(TAG, Here.At() + String.format("interesting value = %d", my_val));

This would give "com.otherpackagenamestuff.MyClass myMethod 225" in the first case, and the same but with "interesting value = 123" appended in the second case.

like image 26
William T. Mallard Avatar answered Oct 28 '22 16:10

William T. Mallard