|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.gargoylesoftware.base.trace.Trace
A class that provides a mechanism for logging diagnostic messages. This mechanism is significantly faster then calling System.out.println directly as the printing is done on a background thread which does not impact the performance of the application threads.
The methods for tracing are print(String), println(String) and printStackTrace(Throwable). The basic idea is that we want the trace methods to return to the working threads as quickly as possible so we put the thing to be traced on a queue. The queue is then processed by a second thread which performs all the formatting and actual output.
Trace can be configured using the TraceController
class - see
getController()
. It is possible to redirect System.out and
System.err so that calling System.out.println("foo") will be the same
as calling Trace.println("foo")
Trace.getController().setOutRedirected(true); Trace.getController().setErrRedirected(true);Calls to any of the print methods will print to a
TraceChannel
which in turn will print using whatever TraceWriter
s have been
added to it. Print methods that do not take a TraceChannel
will use the default channel - see
TraceController.setDefaultChannel(TraceChannel)
If you are using this code in a JVM prior to 1.3 then you need to read this:
Because trace lines are being processed on a second thread, there
might still be trace messages in the queue when the VM exits. To solve
this problem we introduce the method exit(int)
which will shut
down the second thread and then call System.exit(). If you are running on
a java 1.3 VM or higher then it is not neccessary to call exit(int)
as Trace will automatically install a shutdown hook which will do the
neccessary cleanup.
Nested Class Summary | |
static class |
Trace.WhereAmIException
An exception used to determine where the code is at any point in time. |
Field Summary | |
static TraceChannel |
err
The equivilent of "standard error" |
static TraceChannel |
out
The equivilent of "standard out" |
Method Summary | |
protected static void |
assertNotNull(String fieldName,
Object fieldValue)
Verify that the specified value is not null. |
static void |
exit(int statusCode)
Shutdown all buffering and exit the VM with the specified status code. |
static void |
flush()
Flush the trace queue. |
static TraceController |
getController()
Return the controller object for the debugging stuff |
static void |
print(String string)
Print a line to the default channel If the channel is null then nothing will be printed. |
static void |
print(TraceChannel channel,
String string)
Print a line to the specified channel. |
static void |
printLines(String[] lines)
Print the specified lines to the default trace channel. |
static void |
printLines(TraceChannel channel,
String[] lines)
Print the specified lines to the trace channel. |
static void |
println(String string)
Print the line to the default channel with a new line at the end. |
static void |
println(TraceChannel channel,
String string)
Print the line to the specified channel with a new line at the end. |
static void |
printStackTrace(Throwable throwable)
Print the stack trace to the default channel. |
static void |
printStackTrace(TraceChannel channel,
Throwable throwable)
Print the stack trace to the specified channel. |
static String |
throwableToString(Throwable t)
Dump the stack trace from the specified throwable into a String. |
static String[] |
throwableToStringArray(Throwable t)
Dump the stack trace from the specified throwable into an array of strings where each line in the trace is a separate string. |
static void |
whereAmI()
Print a stack trace to show where we came from. |
static void |
whereAmI(TraceChannel channel)
Print a stack trace to show where we came from. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final TraceChannel out
public static final TraceChannel err
Method Detail |
public static void print(TraceChannel channel, String string)
channel
- The trace channel to usestring
- The text string to write.public static void print(String string)
string
- The text string to write.public static void println(TraceChannel channel, String string)
channel
- The trace channel to usestring
- the string to write.public static void println(String string)
string
- the string to write.public static void printStackTrace(TraceChannel channel, Throwable throwable)
channel
- The trace channel to usethrowable
- The exception to printpublic static void printStackTrace(Throwable throwable)
throwable
- The exception to print.public static void printLines(String[] lines)
lines
- The lines to print.public static void printLines(TraceChannel channel, String[] lines)
channel
- The trace channel to uselines
- the lines to print.public static void whereAmI()
public static void whereAmI(TraceChannel channel)
channel
- The trace channel to use.public static String[] throwableToStringArray(Throwable t)
t
- The exception.
public static String throwableToString(Throwable t)
t
- The exception.
public static TraceController getController()
public static void flush()
public static void exit(int statusCode)
Note This is no longer needed if you are running JDK1.3 or higher as we now register a shutdown hook to disable buffering before the VM exits.
statusCode
- The status code returned when the application exits.protected static final void assertNotNull(String fieldName, Object fieldValue) throws DetailedNullPointerException
fieldName
- The name of the field to checkfieldValue
- The value of the field to check
DetailedNullPointerException
- If fieldValue is null
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |