001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: FileLoggingEndpoint.java,v $
023   Revision 1.6  2004/05/05 21:22:45  markl
024   Comment header updates.
025
026   Revision 1.5  2003/01/19 09:42:39  markl
027   Javadoc & comment header updates.
028
029   Revision 1.4  2001/03/12 02:57:39  markl
030   Source code cleanup.
031
032   Revision 1.3  1999/02/25 04:19:45  markl
033   Added flush() call after line is written to file.
034
035   Revision 1.2  1999/01/10 03:47:05  markl
036   added GPL header & RCS tag
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.util;
041
042import java.io.*;
043
044/** An implementation of <code>LoggingEndpoint</code> for ordinary files.
045  *
046  * @author Mark Lindner
047  */
048
049public class FileLoggingEndpoint implements LoggingEndpoint
050  {
051  private static final String types[]
052    = {"INFO   ", "STATUS ", "WARNING", "ERROR  " };
053  private BufferedWriter out;
054
055  /** Construct a new <code>FileLoggingEndpoint</code>.
056    *
057    * @param filename The name of the file to which log messages will be
058    * written.
059    *
060    * @exception java.io.IOException If the file could not be opened for
061    * writing.
062    */
063
064  public FileLoggingEndpoint(String filename) throws IOException
065    {
066    FileOutputStream f = new FileOutputStream(filename, true);
067    out = new BufferedWriter(new OutputStreamWriter(f));
068    }
069
070  /** Write a message to the log file.
071    *
072    * @param type The message type; one of the static constants defined in
073    * <code>LoggingEndpoint</code>.
074    * @param message The message to be written.
075    */
076
077  public void logMessage(int type, String message)
078    {
079    if((type < 0) || (type > 3)) type = 1;
080
081    try
082      {
083      out.write(types[type] + " - " + message);
084      out.newLine();
085      out.flush();
086      }
087    catch(IOException ex)
088      {
089      }
090    }
091
092  /** Close the log file. Once the file is closed, this logging endpoint can no
093    * longer be used.
094    */
095
096  public void close()
097    {
098    try
099      {
100      out.close();
101      }
102    catch(IOException ex) {}
103    out = null;
104    }
105
106  }
107
108/* end of source file */