Class AbstractTracer
- Direct Known Subclasses:
DebugLogTee,FileTracer,NetTracer,NullTracer,QueueTracer
Defines the basic behaviour of tracers. A tracer comes with a map which maps threads on so called
TracingContexts. Each of these TracingContexts manage a stack of TraceMethods.
To be observed methods can be pushed on the method stack assigned to the current thread. When a method is
pushed on the stack a notification will be written to an output stream. When the method is popped from the
stack again a notification will be written together with the elapsed time too.
A TracePrintStream can be used to print additional output. Dependent on the size of the stack
the output can be intended thus providing a clearly arranged format. If the size of the stack exceeds a
configured limit the tracing output will be discarded. If the stack size decreases below this limit the output
will be printed again.
It's possible for a tracer to manage a stack for more than one thread. Then it's the responsibility of the
client to synchronize access to the output stream with getSyncObject(). However it is recommended
to use another tracer for each thread.
A distinction is made between tracing and logging. Log messages can be redirected to conventional logging systems
such as the logging system of the Java platform or log4j, see for an example JDKLoggingRouter.
Before a tracer generates output it must be opened and the tracing context of the current thread must be initialised, e.g.:
final AbstractTracer tracer = new FileTracer("Example");
tracer.open();
try {
class Foo {
void bar() {
tracer.entry("void", this, "bar()");
try {
tracer.out().printfIndentln("This is an example.");
}
finally {
tracer.wayout();
}
}
}
Foo foo = new Foo();
foo.bar(); // nothing will be printed because no tracing context has been provided
tracer.initCurrentTracingContext(2, true);
foo.bar(); // this will generate output
}
finally {
tracer.close();
}
The generated output can be found at ./log/Example.log - whereas the directory ./log must exist - and looks like:
--> TraceLog opened!
Time : Mi Apr 02 23:14:41 MESZ 2014
Bufsize : 512
Autoflush: true
ENTRY--void Foo[12275192].bar()--main[1]
This is an example.
RETURN-void Foo[12275192].bar()--(+0ms)--(+0ms)--main[1]
--> TraceLog closing!
Time : Mi Apr 02 23:14:41 MESZ 2014
- Author:
- Christof Reichardt
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassIndicates exceptional states within the AbstractTracer context. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionAbstractTracer(String name) Constructor expects a name for the tracer, preferably unique. -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves the current tracing context, that is - for example - subsequent calls toout()from the current thread will return the NullPrintStream.abstract voidclose()Derived classes should provide code that closes the output streams.Deprecated.Indicates an entering of a method which belongs to a class.Indicates an entering of a method which belongs to an object.protected StringGives a string representation about the error state of theIndentablePrintStreamof this Tracer instance.protected StringGives a string representation about the version of this library.protected BufferedOutputStreamDerived classes may use this getter to retrieve the buffer of the actualTracePrintStream.intGives the buffer size of the actualTracePrintStream.getName()The name of the tracer.protected NullPrintStreamA replacement for /dev/null.Synchronizes access to theTracePrintStream.protected AbstractThreadMapProvides access to the tracing contexts indexed by Threads.protected TracePrintStreamDerived classes may use this getter to retrieve the actualTracePrintStream.voidInitialises the current tracing context by taking the values for debugLevel and online from the configured debug map.voidinitCurrentTracingContext(int debugLevel, boolean online) Initialises the current tracing context with the given debugLevel and online state.booleanIndicates whether the output stream will be flushed when leaving a method bywayout().booleanisOpened()Indicates whether the actualTracePrintStreamis opened.voidlogException(LogLevel logLevel, Throwable throwable, Class clazz, String methodName) Logs an exception with the given logLevel and the originating class.voidlogMessage(LogLevel logLevel, String message, Class clazz, String methodName) Logs a message with the given logLevel and the originating class.abstract voidopen()Derived classes should provide code that opens the respective output streams.out()Returns some kind of anIndentablePrintStreambased upon the the current managed stack size, the configured debug level and the online state of the current tracing context.protected IndentablePrintStreamout(int level) Returns some kind of an IndentablePrintStream based upon the given level, the configured debug level and the online state of the current tracing context.protected voidreadConfiguration(XPath xpath, Node node) Reads the configuration for this particular tracer instance by evaluating the given node with the given xpath engine.protected voidsetAutoflush(boolean autoflush) Used during the configuration.protected voidsetBufferedOutputStream(BufferedOutputStream bufferedOutputStream) Derived classes may use this setter to inform the base class about the buffer of the actualTracePrintStream.voidsetBufferSize(int bufferSize) Configures the buffer size of the actualTracePrintStream.protected voidsetOpened(boolean opened) Derived classes may use this method to inform the base class that the actualTracePrintStreamis opened.protected voidsetTracePrintStream(TracePrintStream tracePrintStream) Derived classes may use this setter to inform the base class about the actualTracePrintStream.protected Stringsubstitute(String expression) Replaces the given argument by repeatedly substituting all expressions of the form ${property-key} with the corresponding property value.wayout()Indicates the exiting of a method.
-
Field Details
-
VERSION
- See Also:
-
-
Constructor Details
-
AbstractTracer
Constructor expects a name for the tracer, preferably unique.- Parameters:
name- the name of the tracer.
-
-
Method Details
-
getName
The name of the tracer.- Returns:
- the name
-
isOpened
public boolean isOpened()Indicates whether the actualTracePrintStreamis opened.- Returns:
- the opened
-
setOpened
protected void setOpened(boolean opened) Derived classes may use this method to inform the base class that the actualTracePrintStreamis opened.- Parameters:
opened- the opened to set
-
isAutoflush
public boolean isAutoflush()Indicates whether the output stream will be flushed when leaving a method bywayout().- Returns:
- the autoflush
-
setAutoflush
protected void setAutoflush(boolean autoflush) Used during the configuration. Indicates whether the output stream will be flushed when leaving a method bywayout().- Parameters:
autoflush- the autoflush to set
-
getBufferSize
public int getBufferSize()Gives the buffer size of the actualTracePrintStream.- Returns:
- the bufferSize
-
setBufferSize
public void setBufferSize(int bufferSize) Configures the buffer size of the actualTracePrintStream.- Parameters:
bufferSize- the bufferSize to set
-
getSyncObject
Synchronizes access to theTracePrintStream. Clients with multipleTracingContexts should use it to synchronize their access toout().- Returns:
- the syncObject
-
getNullPrintStream
A replacement for /dev/null.- Returns:
- the nullPrintStream
-
getBufferedOutputStream
Derived classes may use this getter to retrieve the buffer of the actualTracePrintStream. -
setBufferedOutputStream
Derived classes may use this setter to inform the base class about the buffer of the actualTracePrintStream. -
getTracePrintStream
Derived classes may use this getter to retrieve the actualTracePrintStream. -
setTracePrintStream
Derived classes may use this setter to inform the base class about the actualTracePrintStream. -
getThreadMap
Provides access to the tracing contexts indexed by Threads.- Returns:
- the threadMap
-
readConfiguration
protected void readConfiguration(XPath xpath, Node node) throws XPathExpressionException, AbstractTracer.Exception Reads the configuration for this particular tracer instance by evaluating the given node with the given xpath engine.- Parameters:
xpath- the xpath enginenode- the config node- Throws:
XPathExpressionException- indicates xpath problemsAbstractTracer.Exception- indicates problems when configuring certain tracer instances
-
open
public abstract void open()Derived classes should provide code that opens the respective output streams. -
close
public abstract void close()Derived classes should provide code that closes the output streams. -
out
Returns some kind of anIndentablePrintStreambased upon the the current managed stack size, the configured debug level and the online state of the current tracing context. This is theNullPrintStreamif the stack size is greater than the debug level or if the thread is offline. Otherwise it's theTracePrintStream.- Returns:
- an
IndentablePrintStream
-
out
Returns some kind of an IndentablePrintStream based upon the given level, the configured debug level and the online state of the current tracing context. It will be theNullPrintStreamif the given level is greater than the debug level or if the thread is offline. Otherwise it's theTracePrintStream.- Parameters:
level- the level of the to be printed data- Returns:
- an
IndentablePrintStream
-
entry
Deprecated.Indicates an entering of a method.- Parameters:
methodSignature- the signature of the method as string representation- Returns:
- the TraceMethod which has been put onto the stack - a mere data object for internal use primarily. May be null.
-
entry
Indicates an entering of a method which belongs to an object. If aTracingContextexists for the current thread aTraceMethodobject will be created and thereupon pushed onto the stack of aThreadMap.- Parameters:
returnType- the return type of the method as string representationobject- the object that owns the methodmethodSignature- the remaining method signature (without return type) inclusive parameter as string representation- Returns:
- the TraceMethod which has been put onto the stack - a mere data object for internal use primarily. May be null.
-
entry
Indicates an entering of a method which belongs to a class. If aTracingContextexists for the current thread aTraceMethodobject will be created and thereupon pushed onto the stack of aThreadMap.- Parameters:
returnType- the return type of the method as string representationclazz- the class to which that method belongmethodSignature- the remaining method signature (without return type) inclusive parameter as string representation- Returns:
- the TraceMethod which has been put onto the stack - a mere data object for internal use primarily. May be null.
-
wayout
Indicates the exiting of a method.- Returns:
- the TraceMethod which has been popped from the stack - a mere data object for internal use primarily. May be null.
-
logMessage
Logs a message with the given logLevel and the originating class.- Parameters:
logLevel- one of the predefined levels INFO, WARNING, ERROR, FATAL and SEVEREmessage- the to be logged messageclazz- the originating classmethodName- the originating method
-
logException
Logs an exception with the given logLevel and the originating class.- Parameters:
logLevel- one of the predefined levels INFO, WARNING, ERROR, FATAL and SEVEREthrowable- the to be logged throwableclazz- the originating classmethodName- the name of the relevant method
-
initCurrentTracingContext
public void initCurrentTracingContext(int debugLevel, boolean online) Initialises the current tracing context with the given debugLevel and online state.- Parameters:
debugLevel- controls the extent of the outputonline- a value of false delivers no output of the current thread at all whereas a value of true delivers output controlled by debugLevel
-
initCurrentTracingContext
public void initCurrentTracingContext()Initialises the current tracing context by taking the values for debugLevel and online from the configured debug map. -
clearCurrentTracingContext
public void clearCurrentTracingContext()Removes the current tracing context, that is - for example - subsequent calls toout()from the current thread will return the NullPrintStream. -
substitute
Replaces the given argument by repeatedly substituting all expressions of the form ${property-key} with the corresponding property value.- Parameters:
expression- the to be replaced expression- Returns:
- the replaced expression
- Throws:
AbstractTracer.Exception- indicates problems during the replacement
-
formatStreamErrorState
Gives a string representation about the error state of theIndentablePrintStreamof this Tracer instance.- Returns:
- a formatted status line
-
formatVersionInfo
Gives a string representation about the version of this library.- Returns:
- a formatted status line
-
entry(String returnType, Class clazz, String methodSignature)orentry(String returnType, Object object, String methodSignature)