public class LogStream extends Object
LogStream
instance maintains an internal buffer that is flushed to the underlying
output stream every time one of the println
methods is invoked, or a newline character (
'\n'
) is written.
All of the print
and println
methods return the {code LogStream} instance on
which they were invoked. This allows chaining of these calls to mitigate use of String
concatenation by the caller.
A LogStream
maintains a current indentation level. Each
line of output written to this stream has n
spaces prefixed to it where n
is the
value that would be returned by indentationLevel()
when the first character of a new
line is written.
A LogStream
maintains a current position for the current line
being written. This position can be advanced to a specified position by
filling this stream with a given character.Modifier and Type | Field and Description |
---|---|
private char |
indentation |
private boolean |
indentationDisabled |
private int |
indentationLevel |
static String |
LINE_SEPARATOR
The system dependent line separator.
|
private StringBuilder |
lineBuffer |
private PrintStream |
ps
The output stream to which this log stream writes.
|
static LogStream |
SINK
Null output stream that simply swallows any output sent to it.
|
private static PrintStream |
SINK_PS |
Modifier | Constructor and Description |
---|---|
private |
LogStream() |
|
LogStream(LogStream log)
Creates a new log stream that shares the same output stream as a given
LogStream . |
|
LogStream(OutputStream os)
Creates a new log stream.
|
Modifier and Type | Method and Description |
---|---|
void |
adjustIndentation(int delta)
Adjusts the current indentation level of this log stream.
|
void |
disableIndentation() |
void |
enableIndentation() |
LogStream |
fillTo(int position,
char filler)
Advances this stream's position to a given position by repeatedly
appending a given character as necessary.
|
void |
flush()
Flushes the stream.
|
private LogStream |
flushLine(boolean withNewline) |
private void |
indent()
Prepends
indentation to the current output line until its write position is equal to
the current indentationLevel() level. |
char |
indentation()
Gets the current indentation character of this log stream.
|
int |
indentationLevel()
Gets the current indentation level for this log stream.
|
PrintStream |
out() |
int |
position()
Gets the current column position of this log stream.
|
LogStream |
print(boolean b)
Writes a boolean value to this stream as
"true" or "false" . |
LogStream |
print(char c)
Writes a character value to this stream.
|
LogStream |
print(double d)
Writes a double value to this stream.
|
LogStream |
print(float f)
Writes a float value to this stream.
|
LogStream |
print(int i)
Prints an int value.
|
LogStream |
print(long l)
Writes a long value to this stream.
|
LogStream |
print(String s)
Writes a
String value to this stream. |
LogStream |
printf(String format,
Object... args)
Writes a formatted string to this stream.
|
LogStream |
println()
Writes a line separator to this stream.
|
LogStream |
println(boolean b)
Writes a boolean value to this stream followed by a line
separator.
|
LogStream |
println(char c)
Writes a character value to this stream followed by a line
separator.
|
LogStream |
println(double d)
Writes a double value to this stream followed by a line
separator.
|
LogStream |
println(float f)
Writes a float value to this stream followed by a line separator
.
|
LogStream |
println(int i)
Writes an int value to this stream followed by a line separator.
|
LogStream |
println(long l)
Writes a long value to this stream followed by a line separator.
|
LogStream |
println(String s)
Writes a
String value to this stream followed by a line
separator. |
void |
setIndentation(char c)
Sets the character used for indentation.
|
public static final LogStream SINK
private static final PrintStream SINK_PS
private final PrintStream ps
private final StringBuilder lineBuffer
private int indentationLevel
private char indentation
private boolean indentationDisabled
public static final String LINE_SEPARATOR
private LogStream()
public LogStream(OutputStream os)
os
- the underlying output stream to which prints are sentpublic LogStream(LogStream log)
LogStream
.log
- a LogStream whose output stream is shared with this onepublic final PrintStream out()
private void indent()
indentation
to the current output line until its write position is equal to
the current indentationLevel() level.public void flush()
public int position()
public int indentationLevel()
public void adjustIndentation(int delta)
delta
- public char indentation()
public void disableIndentation()
public void enableIndentation()
public void setIndentation(char c)
public LogStream fillTo(int position, char filler)
position
- the position to which this stream's position will be advancedfiller
- the character used to pad the streampublic LogStream print(boolean b)
"true"
or "false"
.b
- the value to be printedLogStream
instancepublic LogStream println(boolean b)
b
- the value to be printedLogStream
instancepublic LogStream print(char c)
c
- the value to be printedLogStream
instancepublic LogStream println(char c)
c
- the value to be printedLogStream
instancepublic LogStream print(int i)
i
- the value to be printedLogStream
instancepublic LogStream println(int i)
i
- the value to be printedLogStream
instancepublic LogStream print(float f)
f
- the value to be printedLogStream
instancepublic LogStream println(float f)
f
- the value to be printedLogStream
instancepublic LogStream print(long l)
l
- the value to be printedLogStream
instancepublic LogStream println(long l)
l
- the value to be printedLogStream
instancepublic LogStream print(double d)
d
- the value to be printedLogStream
instancepublic LogStream println(double d)
d
- the value to be printedLogStream
instancepublic LogStream print(String s)
String
value to this stream. This method ensures that the
position of this stream is updated correctly with respect to any
line separators present in s
.s
- the value to be printedLogStream
instancepublic LogStream println(String s)
String
value to this stream followed by a line
separator.s
- the value to be printedLogStream
instancepublic LogStream printf(String format, Object... args)
format
- a format string as described in String.format(String, Object...)
args
- the arguments to be formattedLogStream
instancepublic LogStream println()
LogStream
instance