Class Trace
- java.lang.Object
-
- com.gargoylesoftware.base.trace.Trace
-
public class Trace extends java.lang.Object
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 - seegetController()
. 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 aTraceChannel
which in turn will print using whateverTraceWriter
s have been added to it. Print methods that do not take aTraceChannel
will use the default channel - seeTraceController.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 methodexit(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 callexit(int)
as Trace will automatically install a shutdown hook which will do the neccessary cleanup.- Version:
- $Revision: 1.6 $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Trace.WhereAmIException
An exception used to determine where the code is at any point in time.
-
Field Summary
Fields Modifier and Type Field Description static TraceChannel
err
The equivilent of "standard error"private static java.lang.String
LINE_SEPARATOR
static TraceChannel
out
The equivilent of "standard out"private static TraceController
TRACE_CONTROLLER
private static TraceItemDispatcher
TRACE_ITEM_DISPATCHER
-
Constructor Summary
Constructors Modifier Constructor Description private
Trace()
Private constructor to prevent instantiation of this class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static void
assertNotNull(java.lang.String fieldName, java.lang.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(package private) static TraceItemDispatcher
getDispatcher()
static void
print(TraceChannel channel, java.lang.String string)
Print a line to the specified channel.static void
print(java.lang.String string)
Print a line to the default channel If the channel is null then nothing will be printed.static void
printLines(TraceChannel channel, java.lang.String[] lines)
Print the specified lines to the trace channel.static void
printLines(java.lang.String[] lines)
Print the specified lines to the default trace channel.static void
println(TraceChannel channel, java.lang.String string)
Print the line to the specified channel with a new line at the end.static void
println(java.lang.String string)
Print the line to the default channel with a new line at the end.static void
printStackTrace(TraceChannel channel, java.lang.Throwable throwable)
Print the stack trace to the specified channel.static void
printStackTrace(java.lang.Throwable throwable)
Print the stack trace to the default channel.static java.lang.String
throwableToString(java.lang.Throwable t)
Dump the stack trace from the specified throwable into a String.static java.lang.String[]
throwableToStringArray(java.lang.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.
-
-
-
Field Detail
-
out
public static final TraceChannel out
The equivilent of "standard out"
-
err
public static final TraceChannel err
The equivilent of "standard error"
-
LINE_SEPARATOR
private static final java.lang.String LINE_SEPARATOR
-
TRACE_ITEM_DISPATCHER
private static final TraceItemDispatcher TRACE_ITEM_DISPATCHER
-
TRACE_CONTROLLER
private static final TraceController TRACE_CONTROLLER
-
-
Method Detail
-
print
public static void print(TraceChannel channel, java.lang.String string)
Print a line to the specified channel. If the channel is null then nothing will be printed.- Parameters:
channel
- The trace channel to usestring
- The text string to write.
-
print
public static void print(java.lang.String string)
Print a line to the default channel If the channel is null then nothing will be printed.- Parameters:
string
- The text string to write.
-
println
public static void println(TraceChannel channel, java.lang.String string)
Print the line to the specified channel with a new line at the end. If the channel is null then nothing will be printed.- Parameters:
channel
- The trace channel to usestring
- the string to write.
-
println
public static void println(java.lang.String string)
Print the line to the default channel with a new line at the end.- Parameters:
string
- the string to write.
-
printStackTrace
public static void printStackTrace(TraceChannel channel, java.lang.Throwable throwable)
Print the stack trace to the specified channel. If the channel is null then nothing will be printed.- Parameters:
channel
- The trace channel to usethrowable
- The exception to print
-
printStackTrace
public static void printStackTrace(java.lang.Throwable throwable)
Print the stack trace to the default channel.- Parameters:
throwable
- The exception to print.
-
printLines
public static void printLines(java.lang.String[] lines)
Print the specified lines to the default trace channel.- Parameters:
lines
- The lines to print.
-
printLines
public static void printLines(TraceChannel channel, java.lang.String[] lines)
Print the specified lines to the trace channel. If the channel is null then nothing will be printed.- Parameters:
channel
- The trace channel to uselines
- the lines to print.
-
whereAmI
public static void whereAmI()
Print a stack trace to show where we came from. It will be printed to the default channel.
-
whereAmI
public static void whereAmI(TraceChannel channel)
Print a stack trace to show where we came from. If the channel is null then nothing will be printed.- Parameters:
channel
- The trace channel to use.
-
throwableToStringArray
public static java.lang.String[] throwableToStringArray(java.lang.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. Additionally, expand all tabs to spaces.- Parameters:
t
- The exception.- Returns:
- The resulting string.
-
throwableToString
public static java.lang.String throwableToString(java.lang.Throwable t)
Dump the stack trace from the specified throwable into a String.- Parameters:
t
- The exception.- Returns:
- The resulting string.
-
getController
public static TraceController getController()
Return the controller object for the debugging stuff- Returns:
- The controller in use.
-
getDispatcher
static TraceItemDispatcher getDispatcher()
- Returns:
- The dispatcher
-
flush
public static void flush()
Flush the trace queue.
-
exit
public static void exit(int statusCode)
Shutdown all buffering and exit the VM with the specified status code.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.
- Parameters:
statusCode
- The status code returned when the application exits.
-
assertNotNull
protected static final void assertNotNull(java.lang.String fieldName, java.lang.Object fieldValue) throws DetailedNullPointerException
Verify that the specified value is not null. If it is then throw an exception- Parameters:
fieldName
- The name of the field to checkfieldValue
- The value of the field to check- Throws:
DetailedNullPointerException
- If fieldValue is null
-
-