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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may