Class Log

java.lang.Object
edu.uky.cs.nil.tt.Log
All Implemented Interfaces:
AutoCloseable

public class Log extends Object implements AutoCloseable
A log is used to record system messages, server status, and session data.

A server log has three basic functions:

  • Write system messages to standard output and (optionally) a file.
  • Update a file reflecting the current server status.
  • Record completed storytelling sessions.

To avoid creating huge files, a new system log file is created for each day the server is running. The files are named according to the date they were created so they can be easily located, searched, and archived.

Author:
Stephen G. Ware
  • Constructor Details

    • Log

      public Log(File logs, File status, File sessions) throws IOException
      Constructs a new server log.
      Parameters:
      logs - the directory to which system log files should be written, or null if system log messages should not be recorded
      status - the file to which the current server status will be written, or null if the current server status should not be recorded
      sessions - the directory to which completed session files should be written, or null if sessions should not be recorded
      Throws:
      IOException - if a problem occurs when creating or opening the log files or session directory
  • Method Details

    • append

      public static void append(String message, Log log)
      Writes a system message to standard output and to the given server log, if it exists.
      Parameters:
      message - the message to write
      log - the server log to which the message will be written, or null if the message should only be written to standard output
    • append

      public static void append(String message, Throwable throwable, Log log)
      Writes a system message and the details of a throwable to standard error output and to the given server log, if it exists.
      Parameters:
      message - the message explaining the context of the throwable
      throwable - the throwable whose details and stack trace will be written
      log - the server log to which the message and details will be written, or null if they should only be written to standard error output
    • append

      public void append(String message)
      Writes a system message to standard output and to the log file (if a log file is being used).
      Parameters:
      message - the message to write
    • append

      public void append(String message, Throwable throwable)
      Writes a system message and the details of a throwable to standard error output and to the log file (if a log file is being used)
      Parameters:
      message - the message that gives context for the throwable
      throwable - the throwable whose details and stack trace will be written
    • update

      public void update(ServerStatus status) throws IOException
      Updates the server status file with the server's current status, if a server status file is being maintained.
      Parameters:
      status - an object summarizing the current state of the server
      Throws:
      IOException - if a problem occurs while writing the status file
    • append

      public String append(Session session) throws IOException
      Generates a random name and writes the details of a completed storytelling session to a file with that name in the session file directory (if sessions are being recorded). If sessions are not being recorded, this method does nothing and returns null.
      Parameters:
      session - a completed session
      Returns:
      the randomly generated session name
      Throws:
      IOException - if a problem occurs while writing the session to file
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException