public class Debug
extends java.lang.Object
System.out.printf()
to determine what is happening. As an alternative, the method
printf(String, Object...)
is provided. The nice thing about
printf(String, Object...)
in comparison to System.out.printf()
is that Debug.printf()
only prints when debug is turn on.
You can turn debug on and off by setting the debugLevel
variable to non zero.
This is normally done by a command line parameter to the main()
of your program. This module allows you to write debug code, but not need
to change your program in any way before turning it in.
If you use System.out.printf()
you MUST either comment out the lines
or remove them before you submit your code for grading. This is a simple alternative
to the many logging packages that have more advanced capabilities. See
this page for examples.
Modifier and Type | Field | Description |
---|---|---|
static int |
debugLevel |
Controls how much debug output is produced.
|
Modifier and Type | Method | Description |
---|---|---|
static void |
close() |
Close the output stream if it is not
System.err . |
static java.lang.String |
format(java.lang.String format,
java.lang.Object... args) |
An "extension" of
String.format(String, Object...) . |
static boolean |
HERE() |
Simple routine to print the fileName, lineNumber and methodName.
|
static java.lang.String[] |
init(java.lang.String[] args) |
This routine will set the
debugLevel from the arguments
passed to main() . |
static boolean |
printf(int level,
java.lang.String format,
java.lang.Object... args) |
Print a message if the parameter
level is less than or equal to
debugLevel . |
static boolean |
printf(java.lang.String format,
java.lang.Object... args) |
Print a message if the variable
debugLevel is non-zero. |
static void |
toFile(java.lang.String fileName) |
Send debugging output to a file.
|
public static int debugLevel
A value of 0 means no output. This is normally set by your program's main()
or by a command line argument. With the debugLevel
you can vary
the amount of output from none, to a lot.
public static java.lang.String[] init(java.lang.String[] args)
debugLevel
from the arguments
passed to main()
. If the first argument is -d
or
-dDigit
. the value will be used to initialize
debugLevel
and that argument will be removed.args
- the array of arguments passed to main
args
with the first value removed (if appropriate)public static java.lang.String format(java.lang.String format, java.lang.Object... args)
String.format(String, Object...)
. Automatically converts
arrays to Strings using the Arrays.toString()
methods.
Arrays of arrays are also handled. The argument list is exactly the same
as that of String.format()
, so any of the features of that
code may be used. If there is only a single argument after the
format and if that argument is an array, cast it to an Object using
(Object)
format
- the format string for the output (use %s
for arrays)args
- arguments for the format string (variable number)public static boolean HERE()
true
. Allows you to "abuse" the
assert
statement for high performance.public static boolean printf(java.lang.String format, java.lang.Object... args)
debugLevel
is non-zero.
The argument list is exactly the same as that of
String.format(String, Object...)
, so any of the features of
that code may be used. See the documentation of
format(String, Object...)
for details on printing arrays.
format
- the format string for the outputargs
- arguments for the format string (variable number)true
. Allows you to "abuse" the
assert
statement for high performance.public static boolean printf(int level, java.lang.String format, java.lang.Object... args)
level
is less than or equal to
debugLevel
.
The argument list following level
is exactly the same as that of String.format(String, Object...)
,
so any of the features of that code may be used. See the documentation of
format(String, Object...)
for details on printing arrays.
level
- controls whether message is printed or notformat
- the format string for the outputargs
- arguments for the format string (variable number)true
. Allows you to "abuse" the
assert
statement for high performance.public static void toFile(java.lang.String fileName) throws java.io.FileNotFoundException
fileName
- name of the file to send output tojava.io.FileNotFoundException
- if unable to access the filepublic static void close()
System.err
. For use in
conjunction with toFile(String)