Class TracePrintStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.PrintStream
-
- de.christofreichardt.diagnosis.io.IndentablePrintStream
-
- de.christofreichardt.diagnosis.io.TracePrintStream
-
- All Implemented Interfaces:
Indentable,Closeable,Flushable,Appendable,AutoCloseable
public class TracePrintStream extends IndentablePrintStream
This is the main implementation of an indentable PrintStream. This stream uses a thread map to retrieve the current stack size and computes the extent of the indentation accordingly.- Author:
- Christof Reichardt
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classTracePrintStream.LockAccessHelper class for controlled access to thelockby classes outside the inheritance hierarchy and outside thede.christofreichardt.diagnosis.iopackage.
-
Field Summary
Fields Modifier and Type Field Description static intINDENT_CHAR_NUMBERnumber of spaces that should be used per indentationprotected static String[]INDENT_STRINGstring array that contains the indent stringsprotected ReentrantLocklockused to synchronize access to thisTracePrintStream.static intMAX_INDENT_NUMBERindicates the maximum number of indentationsprotected AbstractThreadMapthreadMapprovides access to the tracing contexts indexed by thread objects-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description TracePrintStream(AbstractThreadMap threadMap)Creates a new instance by passing aNullOutputStreamto the base class.TracePrintStream(BufferedOutputStream out, AbstractThreadMap threadMap)Creates a new instance of TracePrintStream by passing the given OutputStream to the underlying PrintStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidgrantLockAccess(FileTracer fileTracer)Grants access to thelock.voidlock()Acquires a lock to prevent concurrent access to theIndentablePrintStream.IndentablePrintStreamprintfIndentln(String format, Object... args)A convenience method to print an indented string onto the stream using the specified format string and the given arguments.IndentablePrintStreamprintIndent(String s)Prints an indented string.IndentablePrintStreamprintIndentln(String s)Prints an indented string together with a line feed.IndentablePrintStreamprintIndentString()Prints only the current indentation.voidrunWithLock(Runnable runnable)The givenRunnablewill be executed when the lock could be acquired.voidunlock()Unlocks the lock.-
Methods inherited from class java.io.PrintStream
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write
-
Methods inherited from class java.io.FilterOutputStream
write
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
MAX_INDENT_NUMBER
public static final int MAX_INDENT_NUMBER
indicates the maximum number of indentations- See Also:
- Constant Field Values
-
INDENT_CHAR_NUMBER
public static final int INDENT_CHAR_NUMBER
number of spaces that should be used per indentation- See Also:
- Constant Field Values
-
INDENT_STRING
protected static final String[] INDENT_STRING
string array that contains the indent strings
-
lock
protected ReentrantLock lock
used to synchronize access to thisTracePrintStream.
-
threadMap
protected final AbstractThreadMap threadMap
provides access to the tracing contexts indexed by thread objects
-
-
Constructor Detail
-
TracePrintStream
public TracePrintStream(AbstractThreadMap threadMap)
Creates a new instance by passing aNullOutputStreamto the base class.- Parameters:
threadMap- the to be appliedAbstractThreadMap
-
TracePrintStream
public TracePrintStream(BufferedOutputStream out, AbstractThreadMap threadMap)
Creates a new instance of TracePrintStream by passing the given OutputStream to the underlying PrintStream. The threadMap will be needed to determine the current stack size and hence the indentation level.- Parameters:
out- the underlying OutputStreamthreadMap- to compute the indentation level
-
-
Method Detail
-
grantLockAccess
public void grantLockAccess(FileTracer fileTracer)
Grants access to thelock.- Parameters:
fileTracer- theFileTracerwhich requests access to thelock.
-
printIndent
public IndentablePrintStream printIndent(String s)
Description copied from interface:IndentablePrints an indented string.- Specified by:
printIndentin interfaceIndentable- Specified by:
printIndentin classIndentablePrintStream- Parameters:
s- the string to be printed- Returns:
- the indentable stream itself
-
printIndentln
public IndentablePrintStream printIndentln(String s)
Description copied from interface:IndentablePrints an indented string together with a line feed.- Specified by:
printIndentlnin interfaceIndentable- Specified by:
printIndentlnin classIndentablePrintStream- Parameters:
s- the string to be printed- Returns:
- the indentable stream itself
-
printIndentString
public IndentablePrintStream printIndentString()
Description copied from interface:IndentablePrints only the current indentation.- Specified by:
printIndentStringin interfaceIndentable- Specified by:
printIndentStringin classIndentablePrintStream- Returns:
- the indentable stream itself
-
printfIndentln
public IndentablePrintStream printfIndentln(String format, Object... args)
Description copied from interface:IndentableA convenience method to print an indented string onto the stream using the specified format string and the given arguments.- Specified by:
printfIndentlnin interfaceIndentable- Specified by:
printfIndentlnin classIndentablePrintStream- Parameters:
format- the format stringargs- the to be printed arguments- Returns:
- the indentable stream itself
-
lock
public void lock()
Description copied from class:IndentablePrintStreamAcquires a lock to prevent concurrent access to theIndentablePrintStream. This is useful if multiple threads are writing to theIndentablePrintStream.- Specified by:
lockin classIndentablePrintStream
-
unlock
public void unlock()
Description copied from class:IndentablePrintStreamUnlocks the lock.- Specified by:
unlockin classIndentablePrintStream
-
runWithLock
public void runWithLock(Runnable runnable)
Description copied from class:IndentablePrintStreamThe givenRunnablewill be executed when the lock could be acquired.- Specified by:
runWithLockin classIndentablePrintStream- Parameters:
runnable- should contain print statements
-
-