I would like to be able to have my debug code (verbose logging, etc) compiled out when I make a production release, but compiled in for debugging.
I understand that if I branch on a constant, the compiler will remove the branch, which is what I want. So I have a class:
class Debug {
public static final boolean ON=true;
}
and my debug code is inside a branch like this:
if (Debug.ON) {
// Verbose / expensive logging goes here
}
My question is, how do I arrange for Debug.ON to be set to true or false at compile time, short of actually editing the source file?
EDIT: Note that I am not concerned with controlling whether log output appears or not - I am using java.util.Logging and it can take care of that. I am more concerned with compiling out any expensive code that is preparing log messages which will never get output, or taking measurements that are not needed in production mode.
I guess I could have an ant target that makes the source file from a template or something, but it seems like a hack. In C++ I'd be able to define a pre-processor symbol which I could pass in with -D - is there any Java equivalent? Or some other better way?