Disable and Enable Trace Output in Your Program

Disable and Enable Trace Output in Your Program

The prominent mechanism used to debug a Java application is to write string messages to System.out. However, this method clutters up the output of the program. In order to turn debugging statements off, you usually have to find all instances of the System.out in your source code and comment them out. Then you have to recompile your program.

Here is a technique you can use to disable and enable trace output in your program. The basic idea is to encapsulate the trace statements in a static class. The class checks a file to see if it should print the trace or not. This utility uses the class FileFlag defined in the Tip “Using Files as Persistent Flags.”

 1. public class MyTrace {2.   private boolean traceFlag_ = false;3. 4.   public MyTrace () {5. 	if (FileFlag.isFileFlag()) {6. 	  traceFlag_ = true;7. 	}8.   }9. 10.   public void setTraceFlag (boolean traceFlag) {11.     traceFlag_ = traceFlag;12.   }13. 14.   public void println (String str) {15. 	if (traceFlag_)16.       System.out.println(str);17.   }18. }

The class MyTrace defines a single attribute traceFlag_. This flag is initialized to false on Line 2. The constructor on Lines 4-8 checks to see if the fileFlag is set. If it is, the local traceFlag_ is set to true. The method println() on Lines 14-17 is the crux of this program. If traceFlag_ is set, it prints out the string passed in as a parameter. If not, it does nothing. The method setTraceFlag() on Lines 10-12 sets the state of traceFlag_. The main() method illustrates how you can use the MyTrace class. Typically the code in the main() method will be a part of the class that is using this trace utility. This code excerpt uses the MyTrace class:

 1.  FileFlag.setFileFlag(true)2.  MyTrace trace = new MyTrace();3.  trace.println("I am in debug mode");4.  trace.setTraceFlag(false);5.  trace.println("I am in execution mode");

On Line 1, the file flag is set to true. This creates the file flag.txt in the directory where the program is executing. Note that this code uses trace.println to print output to the screen instead of System.out.println. So if you want to use this utility in your existing classes, just replace instances of System.out.println with trace.println. Executing this code gives you:

     fileFlag_ is now ON    I am in debug mode

Note that the trace on Line 5 does not get printed out. This is because you programmatically set the flag to false on Line 4. If you want to disable the trace from the command line, you simply have to run:

 $ java FileFlag OFF

To turn it back on, you would run:

 $ java FileFlag ON.

This utility gives you two levels of control over your debugging output. You can disable or enable trace by changing the traceFlag_ programatically. This is not persisted between different executions of your program (because the FileFlag class is reloaded). You can also set or reset trace from the command line as shown in the example. Neither of these levels require code recompilation.

See also  5 Ways to Improve Customer Experience

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist