SalesPoint Framework v3.1

log
Class Log

java.lang.Object
  extended bylog.Log
All Implemented Interfaces:
LogContext

public class Log
extends Object
implements LogContext

Represents a log file.

There is one global writable log file in the whole system. An instance of LogCreator is used to create the global log file as well as any local log file.

Anything loggable must suit the Loggable interface and must be able to generate an instance of LogEntry

To read in log files see LogInputStream and LogFileContent.

Since:
v1.0
Author:
Steffen Zschaler

Field Summary
protected  ObjectOutputStream ooOutput
          The log file's output stream.
protected  boolean saveToPersistence
          Save log file to persistence file or not.
protected static OutputStream theGlobalOutputStream
          Reference to the global output stream.
 
Constructor Summary
Log(OutputStream os)
          Construct a new log file.
 
Method Summary
 void addLogEntries(LogFileContent lfc)
          Appends all LogEntries that are saved in lfc to the current log.
 void changeOutputStream(OutputStream os)
          Change this log's outputstream.
static void closeGlobalLog()
          Closes the global log file if any log file was open.
 void closeLog()
          Closes this Log file.
static Log createLog(OutputStream os)
          Create a new Log file using the current Log creator.
protected  void finalize()
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
static Log getGlobalLog()
          Returns the current global log.
static File getGlobalLogFile()
           
static boolean getSaveToPersistence()
          States if the log file should be saved to the persistence file.
 void log(Loggable l)
          Adds one entry to the log file.
protected  void logCloseLog()
          Add a log entry when closing the log file.
protected  void logOpenLog()
          Add a log entry when opening the log file.
static void setGlobalLogFile(String filename)
          Sets the global log file and assigns a FileOutputStream to it.
static void setGlobalLogFile(String filename, boolean overwrite, boolean save)
          Sets the global log file and assigns a FileOutputStream to it.
static void setGlobalOutputStream(OutputStream newOutputStream)
          Changes the current OutputStream of the global log file.
static void setLogCreator(LogCreator lc)
          Change the Log creator.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ooOutput

protected ObjectOutputStream ooOutput
The log file's output stream.

See Also:
changeOutputStream(java.io.OutputStream), log(log.Loggable)

saveToPersistence

protected boolean saveToPersistence
Save log file to persistence file or not.


theGlobalOutputStream

protected static OutputStream theGlobalOutputStream
Reference to the global output stream.

Read Only

See Also:
setGlobalOutputStream(java.io.OutputStream)
Constructor Detail

Log

public Log(OutputStream os)
Construct a new log file.

Parameters:
os - the outputstream to write to.
Method Detail

closeLog

public void closeLog()
              throws IOException
Closes this Log file.

Throws:
IOException - if an error occurred while closing the underlying stream.
See Also:
closeGlobalLog()
Override:
Never

finalize

protected void finalize()
                 throws IOException
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Disposes this log file. If this is the global log file, calls closeGlobalLog(), else calls closeLog().

Throws:
IOException - if an error occurs while closing the underlying stream.
See Also:
closeLog(), closeGlobalLog()
Override:
Never

changeOutputStream

public void changeOutputStream(OutputStream os)
                        throws IOException
Change this log's outputstream.

If an outputstream exists it is closed prior to setting the new outputstream.

Parameters:
os - the new output stream.
Throws:
IOException - if an error occured while closing the old stream.
See Also:
setGlobalOutputStream(java.io.OutputStream)
Override:
Never

logCloseLog

protected void logCloseLog()
Add a log entry when closing the log file.

Currently does nothing. You can override this to write a log entry when the log file is being closed.

See Also:
closeLog()
Override:
Sometimes Override this method if you want to add a log entry when the log file is closed.

logOpenLog

protected void logOpenLog()
Add a log entry when opening the log file.

Currently does nothing. You can override this to write a log entry when the log file is being opened.

See Also:
changeOutputStream(java.io.OutputStream)
Override:
Sometimes Override this method if you want to add a log entry when the log file is opened.

log

public void log(Loggable l)
         throws LogNoOutputStreamException,
                IOException
Adds one entry to the log file. Calls l.getLogData().

Specified by:
log in interface LogContext
Parameters:
l - the loggable event to be logged.
Throws:
LogNoOutputStreamException - if no OutputStream has been specified.
IOException - if an IOException occurs when writing to the stream.
See Also:
Loggable
Override:
Never

addLogEntries

public void addLogEntries(LogFileContent lfc)
                   throws LogNoOutputStreamException,
                          IOException
Appends all LogEntries that are saved in lfc to the current log. This method is used when restoring a log file from a persistence file.

Parameters:
lfc - contains the LogEntries to be added
Throws:
LogNoOutputStreamException - if no OutputStream is specified
IOException - if a log entry cannot be written zu the output stream

getGlobalLog

public static Log getGlobalLog()
                        throws LogNoOutputStreamException
Returns the current global log.

If no log exists, one is created using the Outputstream as specified by setGlobalOutputStream(java.io.OutputStream)

Throws:
LogNoOutputStreamException - if setGlobalOutputStream() has not been called yet.
See Also:
setGlobalOutputStream(java.io.OutputStream), closeGlobalLog()

createLog

public static Log createLog(OutputStream os)
Create a new Log file using the current Log creator.

You should prefer calling this method to directly creating a new Log file as this method will provide an easy interface for adapting to new log classes.

Parameters:
os - the OutputStream to be used.

setLogCreator

public static void setLogCreator(LogCreator lc)
Change the Log creator.

Call to provide support for descended Log classes.

Parameters:
lc - the log creator to be used when creating log files.
See Also:
getGlobalLog()

closeGlobalLog

public static void closeGlobalLog()
                           throws IOException
Closes the global log file if any log file was open.

If no log file exists no exception is thrown. Closes the log file and it's OutputStream.

Throws:
IOException - if an error occurs while closing the underlying stream.
See Also:
getGlobalLog(), setGlobalOutputStream(java.io.OutputStream)

setGlobalOutputStream

public static void setGlobalOutputStream(OutputStream newOutputStream)
                                  throws IOException
Changes the current OutputStream of the global log file.

This method must be called at least once before any global log operation takes place.

If an OutputStream exists it will be closed automatically. To close the entire global log file use closeGlobalLog().

Parameters:
newOutputStream - the new global output stream
Throws:
IOException - if an error occurs while closing the original stream.
See Also:
getGlobalLog(), closeGlobalLog(), changeOutputStream(java.io.OutputStream)

setGlobalLogFile

public static void setGlobalLogFile(String filename,
                                    boolean overwrite,
                                    boolean save)
                             throws IOException
Sets the global log file and assigns a FileOutputStream to it.

Parameters:
filename - the file to be used as global log file.
overwrite - if true, an existing file with the name fileName will be overwritten, if false, the LogEntries will be appended to that existing file.
With this flag one can decide if consecutive runs of the application should be logged in one file or if the old log file should always be overwritten.
save - if true, the global log file will be written to the persistence file when the Shop is saved and accordingly loaded when the Shop's state is loaded. When that happens, the current log file will be deleted, even if overwrite is set to false.
Throws:
IOException

setGlobalLogFile

public static void setGlobalLogFile(String filename)
                             throws IOException
Sets the global log file and assigns a FileOutputStream to it.
This method calls setGlobalLogFile(filename, false, false), that is, Log entries will be append to older log files, if they exist. The log files will not be saved/restored when the Shop is saved/restored.

Parameters:
filename - the file to be used as global log file.
Throws:
IOException

getGlobalLogFile

public static File getGlobalLogFile()
Returns:
the global log file, if set. Otherwise null

getSaveToPersistence

public static boolean getSaveToPersistence()
States if the log file should be saved to the persistence file.

Returns:
true if the log file should be saved to the persistence file, otherwise false.

SalesPoint Framework v3.1