Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.log
Class AbstractLog

java.lang.Object
  extended by dk.rode.thesis.meta.log.AbstractLog
All Implemented Interfaces:
Log
Direct Known Subclasses:
AppendableLog, NullLog

public abstract class AbstractLog
extends Object
implements Log

An abstract log implements the basic traits of any Log implementation.

The logger is not thread-safe!

Author:
Gunni Rode / rode.dk

Field Summary
private  boolean active
          True if active, i.e. logs messages, false if inactive.
private  Class<?> clazz
          The class to which this log is associated, if any.
private  boolean flushed
          True if the last logged message ended with a newline, false if not.
private  AtomicInteger lineNumber
          If not null, the line number representing the current value is logged.
 
Constructor Summary
protected AbstractLog(Class<?> clazz, boolean active, AtomicInteger lineNumber)
          Constructor.
 
Method Summary
 Log error(Object... messages)
          Logs each part of an error message supplied in messages with this log as is, no trailing new line or white space inserted between message parts, but with a single new line character after the last part as well as a prefix indicating the message represent an error message.
private  void forceNewLine()
          Prints a new line if this log is not flushed.
protected  CharSequence format(Object object)
          Formats the object supplied as object into a char sequence representation that can be logged.
 int getLineNumber()
          If this log logs line numbers, the current line number is returned; otherwise 0.
 String getPrefix()
          Returns the prefix of this log that will be prefixed to all log messages starting at a new line.
static String getPrefix(Class<?> clazz)
          Returns the current prefix to prefix any log message based on the class supplied as clazz.
 Log heading(Object... heading)
          Logs each part of a heading supplied in messages with this log as is, no trailing new line or white space inserted between heading parts, but with a single new line character after the last part, underscored on the next log line.
 boolean isActive()
          Returns true if this log is active, i.e. logs messages, false if inactive.
protected abstract  void log(Object... messages)
          Performs the actual logging of the messages supplied as messages with no new line postfixed.
 Log print(boolean condition, Object... messages)
          Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, if and only if condition is true.
 Log print(Object... messages)
          Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages.
 Log println(boolean condition, Object... messages)
          Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, if and only if condition is true, but always with a single new line character after the last part.
 Log println(Object... messages)
          Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, but with a single new line character after the last part.
 Log setActive(boolean active)
          If active is true, this log is set to active, i.e. to log messages.
 String toString()
          Returns the string representation of this log.
 Log warn(Object... messages)
          Logs each part of a warning message supplied in messages with this log as is, no trailing new line or white space inserted between message parts, but with a single new line character after the last part as well as a prefix indicating the message represent a warning message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

active

private boolean active
True if active, i.e. logs messages, false if inactive.


clazz

private Class<?> clazz
The class to which this log is associated, if any.

Null indicates the global log.


flushed

private boolean flushed
True if the last logged message ended with a newline, false if not.


lineNumber

private final AtomicInteger lineNumber
If not null, the line number representing the current value is logged.

If used, even empty lines logged will have a line number.

Default false.

Constructor Detail

AbstractLog

protected AbstractLog(Class<?> clazz,
                      boolean active,
                      AtomicInteger lineNumber)
Constructor.

Line numbers are logged if lineNumbers is not null.

Parameters:
clazz - The class to which this log is associated, if any. Null indicates global log.
active - True if this log is initially active, false if not.
lineNumber - The line number values; can be null, in which case line numbers are not generated.
See Also:
isActive()
Method Detail

error

public Log error(Object... messages)
Description copied from interface: Log
Logs each part of an error message supplied in messages with this log as is, no trailing new line or white space inserted between message parts, but with a single new line character after the last part as well as a prefix indicating the message represent an error message.

A part that is null is logged as the string null.

Example (default log):

   error("This", "is ", " an ", "error"):
   
   MAIN:xxx:123:: > ERROR - Thisis  an error(newline)
 
Where xxx is the name of the member invoking the error method and 123 the line number.

Specified by:
error in interface Log
Parameters:
messages - The message parts; can be null, corresponding to a single new line being printed.
Returns:
This log; never null.

forceNewLine

private void forceNewLine()
Prints a new line if this log is not flushed.


format

protected CharSequence format(Object object)
Formats the object supplied as object into a char sequence representation that can be logged.

Parameters:
object - The object; can be null.
Returns:
The char sequence representation; never null.

getLineNumber

public int getLineNumber()
Description copied from interface: Log
If this log logs line numbers, the current line number is returned; otherwise 0.

Specified by:
getLineNumber in interface Log
Returns:
The current line number, or null if line numbers are not logged.

getPrefix

public String getPrefix()
Description copied from interface: Log
Returns the prefix of this log that will be prefixed to all log messages starting at a new line.

Specified by:
getPrefix in interface Log
Returns:
The prefix.

getPrefix

public static final String getPrefix(Class<?> clazz)
Returns the current prefix to prefix any log message based on the class supplied as clazz.

The prefix will include the name of the member and line number of the calling context that issued the original log request.

Parameters:
clazz - The current class; can be null.
Returns:
The current prefix; never null, and never empty.
See Also:
CallerClass

heading

public Log heading(Object... heading)
Description copied from interface: Log
Logs each part of a heading supplied in messages with this log as is, no trailing new line or white space inserted between heading parts, but with a single new line character after the last part, underscored on the next log line.

A message that is null is logged as the string null.

Example (default log):

   heading("This", "is ", " a ", "heading"):
   
   (newline)
   MAIN:xxx:123:: Thisis  a heading(newline)
   MAIN:xxx:123:: =================(newline)
   (newline)
   (newline)
 
Where xxx is the name of the member invoking the heading method and 123 the line number.

Specified by:
heading in interface Log
Parameters:
heading - The message parts; can be null, corresponding to a single new line being printed (no underscore).
Returns:
This log; never null.

isActive

public final boolean isActive()
Description copied from interface: Log
Returns true if this log is active, i.e. logs messages, false if inactive.

Specified by:
isActive in interface Log
Returns:
True if active, false if inactive.

log

protected abstract void log(Object... messages)
                     throws IOException
Performs the actual logging of the messages supplied as messages with no new line postfixed.

All messages part are assumed to have a meaningful toString() representation and are not null, and there is no need for explicit formatting.

Parameters:
messages - The messages to log; never null.
Throws:
IOException - In case an exception occurs while logging.

print

public final Log print(boolean condition,
                       Object... messages)
Description copied from interface: Log
Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, if and only if condition is true.

A part that is null is logged as the string null.

Example (default log):

   i = 1;
   ..
   print(i > 0, "This", "is ", "message #", new Integer(42), "!"):
   
   MAIN:xxx:123:: Thisis message #42!
   
   ..
   
   i = 0;
   ..
   print(i > 0, "This", "is ", "message #", new Integer(42), "!"):
   
   (nothing logged)
 
Where xxx is the name of the member invoking the print method and 123 the line number.

Specified by:
print in interface Log
Parameters:
condition - The condition to use.
messages - The message parts; can be null, which will not print anything.
Returns:
This log; never null.

print

public final Log print(Object... messages)
Description copied from interface: Log
Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages.

A part that is null is logged as the string null.

Example (default log):

   print("This", "is ", "message #", new Integer(42), "!"):
   
   MAIN:xxx:123:: Thisis message #42!
 
Where xxx is the name of the member invoking the print method and 123 the line number.

Specified by:
print in interface Log
Parameters:
messages - The message parts; can be null, which will not print anything.
Returns:
This log; never null.

println

public final Log println(boolean condition,
                         Object... messages)
Description copied from interface: Log
Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, if and only if condition is true, but always with a single new line character after the last part. Hence, if condition is false, the effect is equivalent to println().

A part that is null is logged as the string null.

Example (default log):

   i = 1;
   ..
   println(i > 0, "This", "is ", "message #", new Integer(42), "!"):
   
   MAIN:xxx:123:: Thisis message #42!(newline)
   
   ..
   
   i = 0;
   ..
   println(i > 0, "This", "is ", "message #", new Integer(42), "!"):
   
   (newline logged only)
 
Where xxx is the name of the member invoking the println method and 123 the line number.

Specified by:
println in interface Log
Parameters:
condition - The condition to use.
messages - The message parts; can be null, corresponding to a single new line being printed.
Returns:
This log; never null.

println

public final Log println(Object... messages)
Description copied from interface: Log
Logs each message part supplied in messages with this log as is, no trailing new line or white space inserted between messages, but with a single new line character after the last part.

A part that is null is logged as the string null.

Example (default log):

   println("This", "is ", "message #", new Integer(42), "!"):
   
   MAIN:xxx:123:: Thisis message #42!(newline)
 
Where xxx is the name of the member invoking the println method and 123 the line number.

Specified by:
println in interface Log
Parameters:
messages - The message parts; can be null, corresponding to a single new line being printed.
Returns:
This log; never null.

setActive

public final Log setActive(boolean active)
Description copied from interface: Log
If active is true, this log is set to active, i.e. to log messages.

Specified by:
setActive in interface Log
Parameters:
active - True to (re-)activate, false to disable.
Returns:
This log; never null.

toString

public String toString()
Returns the string representation of this log.

Overrides:
toString in class Object
Returns:
The string representation; never null.

warn

public Log warn(Object... messages)
Description copied from interface: Log
Logs each part of a warning message supplied in messages with this log as is, no trailing new line or white space inserted between message parts, but with a single new line character after the last part as well as a prefix indicating the message represent a warning message.

A part that is null is logged as the string null.

Example (default log):

   warn("This", "is ", " a", "warning"):
   
   MAIN:xxx:123:: > WARN - Thisis  awarning(newline)
 
Where xxx is the name of the member invoking the warn method and 123 the line number.

Specified by:
warn in interface Log
Parameters:
messages - The message parts; can be null, corresponding to a single new line being printed.
Returns:
This log; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.