public class Trace
extends Object
Trace
class provides a mechanism for logging output
messages to the console or any PrintStream or Writer.
A number of output message levels are supported by this class, including
errors, warnings, info messages, and debug messages. Outputting messages
for any one of these levels is accomplished by passing a message string
to the error()
, warning()
,
info()
, or debug()
functions. tab()
and untab()
functions.
Single-line multi-part messages (where the first part is tabbed from the
left, and additional parts are tacked on to the end of the line after
additional processing) may be created using the more()
functions. Indentation is normally two spaces per tab level, but this
may be changed by setting the defaultTabSize
variable.setOutputLevel()
function.printTimestamps(boolean)
method. The default
is to print without timestamps. When enabled, timestamps will be
printed with a simple date format: YYYY-MM-DD:HH:MM:SS, but this can be
changed if necessary by changing the value of the dateFormat
member variable.setPrintStream(PrintStream)
or setWriter(Writer)
respectively.Modifier and Type | Field and Description |
---|---|
private static boolean |
autoFlush
True to force an immediate newline after every output
|
static DateFormat |
dateFormat
Format to output dates in.
|
static int |
debug
Print all messages (debug, info, errors, and warnings.)
|
static int |
defaultTabSize
Amount to indent when
tab() is called. |
static int |
errors
Print errors only
|
private static Trace |
firstTrace
First trace instance ever created
|
static int |
info
Print info messages, errors, and warnings
|
private static int |
nThreads
Number of threads that have accessed Trace
|
private static int |
outputMask
Current mask of which messages to output
|
private static int |
prevMsgLevel
Previous message level
|
private static Trace |
prevTrace
Trace instance that last wrote to the output stream
|
private static PrintStream |
printStream
PrintStream to write to, or null to write to
writer |
private static boolean |
printTimestamps
True to print a timestamp on each line
|
static int |
silent
Print no messages
|
private static String |
spaces
Used for tabbing
|
private int |
tabCount
Current tab level for this thread
|
private int |
tabSize
Amount to indent when
tab() is called. |
private String |
threadId
String to prefix messages from this thread with
|
private static WeakHashMap |
threadTraces
Trace instance for the current thread
|
static int |
warnings
Print errors and warnings
|
private static Writer |
writer
Writer to write to, or null to write to
printStream |
Modifier | Constructor and Description |
---|---|
private |
Trace()
Private constructor -- used by
getThreadTrace() . |
Modifier and Type | Method and Description |
---|---|
static void |
clearTabs()
Resets the tab level to zero, undoing the effects of any calls to
tab() . |
static void |
debug(String msg)
Output a message at the 'debug' level.
|
static void |
debug(String msg,
Object... args) |
static void |
error(String msg)
Output a message at the 'error' level.
|
static void |
error(String msg,
Object... args) |
static String |
getCurrentThreadId()
Retrieve the thread identifier that is printed for the current thread.
|
static int |
getOutputLevel()
Retrieves the current output level established by
setOutputLevel(int) . |
static String |
getThreadId(Thread thread)
Retrieve the thread identifier that is printed for messages from the
specified thread.
|
private static Trace |
getThreadTrace()
Gets a thread-specific instance of Trace.
|
static void |
info(String msg)
Output a message at the 'info' level.
|
static void |
info(String msg,
Object... args) |
static void |
more(int level,
String msg)
Append more text to the previous output line if it was the same
output level; if not, write the text to a new output line.
|
static void |
more(String msg)
Append more text to the previous output line (unless of course it was
suppressed).
|
private void |
output(String msg,
boolean tabbed,
boolean linefeed)
Workhorse output function -- handles tabbing, prefixing the output with
a thread ID, timestamping, adding newlines, and directing to the proper
output PrintStream or Writer.
|
static void |
printTimestamps(boolean flag)
Enables or disables prefixing each output line with a timestamp.
|
static void |
setAutoFlush(boolean flag)
Enables or disables immediate newline and flushing of each output line
(note that this somewhat defeats the more() feature.)
|
static void |
setOutputLevel(int level)
Set the level of message output.
|
static void |
setPrintStream(PrintStream pstream)
Overrides the default output destination, System.out, with the given
alternate print stream.
|
static void |
setWriter(Writer w)
Overrides the default output destination, System.out, with the given
alternate Writer.
|
private void |
t_clearTabs()
Internal threaded helper for
clearTabs() . |
private void |
t_debug(String msg)
Internal threaded helper for
debug(String) . |
private void |
t_error(String msg)
Internal threaded helper for
error(String) . |
private void |
t_info(String msg)
Internal threaded helper for
info(String) . |
private void |
t_more(int level,
String msg)
Internal threaded helper for
more(int, String) . |
private void |
t_more(String msg)
Internal threaded helper for
more(String) . |
private void |
t_tab()
Internal threaded helper for
tab() |
private void |
t_untab()
Internal threaded helper for
untab() |
private void |
t_warning(String msg)
Internal threaded helper for
warning(String) . |
static void |
tab()
Indent all subsequent output lines by
defaultTabSize
(default 2) spaces. |
static void |
untab()
Undoes effect of
tab() , un-indenting subsequent output lines. |
static void |
warning(String msg)
Output a message at the 'warning' level.
|
static void |
warning(String msg,
Object... args) |
public static final int silent
public static final int errors
public static final int warnings
public static final int info
public static final int debug
public static int defaultTabSize
tab()
is called. Default value: 2public static DateFormat dateFormat
private static final WeakHashMap threadTraces
private static Trace prevTrace
private static Trace firstTrace
private static int nThreads
private static PrintStream printStream
writer
private static Writer writer
printStream
private static int prevMsgLevel
private static int outputMask
private static boolean printTimestamps
private static boolean autoFlush
private static final String spaces
private String threadId
private int tabCount
private int tabSize
tab()
is called.private Trace()
getThreadTrace()
. Calculates
a thread ID for this thread, used to prefix output messages.public static void setOutputLevel(int level)
public static int getOutputLevel()
setOutputLevel(int)
.public static void printTimestamps(boolean flag)
public static void setAutoFlush(boolean flag)
public static void tab()
defaultTabSize
(default 2) spaces. Call untab()
or clearTabs()
to
undo this effect.private void t_tab()
tab()
public static void untab()
tab()
, un-indenting subsequent output lines.private void t_untab()
untab()
public static void clearTabs()
tab()
.private void t_clearTabs()
clearTabs()
.public static void setPrintStream(PrintStream pstream)
pstream
- Where to direct subsequent output topublic static void setWriter(Writer w)
w
- Where to direct subsequent output topublic static String getCurrentThreadId()
public static String getThreadId(Thread thread)
public static void error(String msg)
public static void error(String msg, Object... args)
private void t_error(String msg)
error(String)
.public static void warning(String msg)
public static void warning(String msg, Object... args)
private void t_warning(String msg)
warning(String)
.public static void info(String msg)
public static void info(String msg, Object... args)
private void t_info(String msg)
info(String)
.public static void debug(String msg)
public static void debug(String msg, Object... args)
private void t_debug(String msg)
debug(String)
.public static void more(String msg)
private void t_more(String msg)
more(String)
.public static void more(int level, String msg)
private void t_more(int level, String msg)
more(int, String)
.private static Trace getThreadTrace()
private void output(String msg, boolean tabbed, boolean linefeed)