public class Debug
extends java.lang.Object
Debug
class supports conditional printing of
debugging messages.
The turnOn
and turnOff
methods cause the
previous debugging state to be saved on a stack so that it can be
restored later using the restoreState
method.
For example, consider the following scenario. Method 1 turns on debugging and calls Method 2. Method 2, not knowing that debugging is already on, turns debugging on and calls Method 3. Method 3 turns debugging off. Before returning, Method 3 restores the previous state, which turns debugging back on (as set by Method 2). Before Method 2 returns, it also restores the state, which will leave debugging on (as set by Method 1). This is illustrated in the pseudo-code below.
in Method 1: (A: in Method 1; we don't know debugging state) turn on debugging (debugging state is now on; Debug.println will print) call Method 2 (B: now in Method 2; debugging state is unchanged (on)) turn on debugging (debugging state is on (still)) call Method 3 (C: now in Method 3; debugging state is unchanged (on)) turn off debugging (debugging state is now off; Debug.println will not print) restore state (debugging state is restored to what it was at point C (on)) return to Method 2 (D: now in Method 2; debugging state is unchanged (still on)) restore state (debugging state is still on because it was on at point B) return to Method 1 (E: now in Method 1; debugging state unchanged (still on)) restore state (debugging state is whatever it was at point A (unknown))Note that when Method 2 restores the debugging state, it does not go back to its most recent state, which would be off (as set by Method 3). State restoration is controlled by a stack, not by a toggle.
The Debug
class is
copyright© 2002 College Entrance Examination Board
(www.collegeboard.com).
Constructor and Description |
---|
Debug() |
Modifier and Type | Method and Description |
---|---|
static boolean |
isOff()
Checks whether debugging is off.
|
static boolean |
isOn()
Checks whether debugging is on.
|
static void |
print(java.lang.String message)
Prints debugging message without appending a newline character at
the end.
|
static void |
println(java.lang.String message)
Prints debugging message, appending a newline character at the end.
|
static void |
restoreState()
Restores the previous debugging state.
|
static void |
turnOff()
Turns debugging off.
|
static void |
turnOn()
Turns debugging on.
|
public static boolean isOn()
Debug.print
and Debug.println
) methods use isOn
to decide whether or not to print.public static boolean isOff()
public static void turnOn()
public static void turnOff()
public static void restoreState()
restoreState
turns
debugging off.public static void print(java.lang.String message)
message
is
printed to System.out
without a newline.message
- debugging message to printpublic static void println(java.lang.String message)
message
is
printed to System.out
followed by a newline.message
- debugging message to print