9

Possible Duplicate:
How to find out if “debug mode” is enabled

Coming from C#, I cannot believe that Java has no way to execute code only when the program is being debugged. Somehow, Log4J seems to be able to do it. Does anyone know a possibility for this? I'm thinking of something like this:

#if DEBUG
    executedCode();
#endif

Or something like this:

if (Java.isDebugging())
    executeCode();

Ideas?

EDIT: Thanks to Matt Ball, the code in this possible duplicate works:

public static boolean debugging = java.lang.management.ManagementFactory.getRuntimeMXBean().
        getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
Community
  • 1
  • 1
Akku
  • 4,373
  • 4
  • 48
  • 67
  • 2
    What makes you think Log4J can do it? I know it does have a DEBUG log-level, but Log4J's log-level is set in a configuration file. – Jack Edmonds Jul 17 '12 at 14:24
  • 1
    Log4j's `Logger#isDebugEnabled()` has nothing to do with whether or not the code is being debugged. It's only based on the configuration of that logger. – Matt Ball Jul 17 '12 at 14:24
  • 1
    You might just add a commandline flag and check argv if your debug flag is set. Is easy as hell and fits your needs – Daniel Leschkowski Jul 17 '12 at 14:34

4 Answers4

2

I think you are conflating a debug build of a program with running under the debugger (unless C# is rather strange).

Many systems - including lots of C/C++ build environments, and I expect C#, given the way Microsoft has historically designed the Visual Studio build processes - have two different types of builds you can do:

  • Release build, with only info/error/warning logging, no debug symbols, extra optimizations, etc.
  • Debug build, with extra logging enabled, debug symbols, possibly tuned-down optimizations.

You use debug builds while developing and debugging your program, and release builds for final QA and release. The debug builds, however, are independent of being run under a debugger - they're normal programs, and if you run them the run and still output their extra logging information. This can be very useful.

Running them under a debugger is completely orthogonal. You can run the release build under a debugger (although, lacking debug symbols, it might not be very useful). In fact, I would submit that you do not want code to change based on whether you are running under a debugger - it makes it rather difficult to debug the code being actually run in the application.

And unlike C, C++, and C#, Java has no facility for compile-time conditionals, so everything is enabled in the program. You typically deal with this by having a very fast way to check if the debugging message is to be emitted, and doing as little work as possible in the case where it is not.

Michael Ekstrand
  • 28,379
  • 9
  • 61
  • 93
1

C doesn't execute code only when it is debugging, what you're showing is a conditional compile directive, the code only gets compiled when the flag value is true. The running code has no idea when it is being debugged. Likewise with Log4j it is reading from its configuration what level to use, it has no idea when it is being debugged.

Conditional compile flags were deliberately left out of Java, they wanted to avoid complexity. C# has a different philosophy.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

Perhaps you are thinking about debug logging which is not the same as using the debugger.

The problem you will find is that Java has more than half a dozen libraries widely used for logging.

There is the built in logger.

http://docs.oracle.com/javase/1.4.2/docs/guide/util/logging/overview.html

Log4j is widely used, and slf4j and Logback are also popular.

There are even libraries for abstracting away which logger you are actually using e.g. commons logging.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I'm thinking about System.out.println() that I only want to be doing when debugging. Something like reflection, that finds out if a debugger is attached. – Akku Jul 17 '12 at 14:31
  • 1
    @Akku, Eclipse allows you to specify different debug configurations. So you could probably setup your debug configuration to use a different Log4J configuration file than you use when deploying your application. – Jack Edmonds Jul 17 '12 at 14:34
  • You can check whether a JVM was started with debugging enabled, (See Matt Ball's comment) but you can't check whether a debugger is actually attached. – Peter Lawrey Jul 17 '12 at 14:34
  • Can you clarify why you want to do this? Is there something you can't run from your debugger? The debugger should allow you to run ad hoc code. – Peter Lawrey Jul 17 '12 at 14:36
  • @JackEdmonds: I don't use Eclipse. PeterLawrey: I'm doing a small .jar for some other developers and want to output debug info on the command line when debugging. Everyone: Yes, I know I should set this explicitly, but this is exactly why I don't want it. I hate doing more than I must. :-) – Akku Jul 17 '12 at 14:47
  • In Java, you enable DEBUG level logging when you want to enable additional logging for debugging purposes. This is because you want to be able to enable this selectively (for one class or package) in production without debugging the code. In some environments you can enable and disable selectively when the program is running (e.g. via a web interface) – Peter Lawrey Jul 17 '12 at 14:57
0

You can do it as described, with a global flag. Though it should note that it won't detect automatically, you'd have to change the flag yourself before you start debugging.

public class Globals
{
    public static final boolean debugFlag = false;
}

later...

if (Globals.debugFlag){...}

This is only really required if you want to do functional code when debugging. It is also a brute force solution, as you have to turn it on everywhere to turn it on anywhere. You could do something similar in a particular class you want to debug with a member flag if you need more granularity. If all you want is logging inside the debug block, see Peter's answer about logging libraries.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Thomas
  • 5,074
  • 1
  • 16
  • 12