/*****************************************************************************/
/* File name: LogToFile.java                                                 */
/* Purpose: to log events to a log file                                      */
/*                                                                           */
/* Last modified: 31.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*                                                                           */
/* This program is distributed in the hope that it will be useful, but       */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General */
/* Public License for more details.                                          */
/*                                                                           */
/* This software is distributed under the terms                              */
/* of the GNU General Public License as published                            */
/* by the Free Software Foundation; either version 2, or (at your option)    */
/* any later version.                                                        */
/* See file COPYING for your rights (GNU GPL)                                */
/*****************************************************************************/

package io;


import java.io.*;
import java.util.*;


public
class

LogToFile implements Logable
{

  private final static java.lang.String RCSID =
"$Header: /d/NFS/io/RCS/LogToFile.java,v 1.1 1999/08/02 16:49:00 vklimov Exp $";


  static private OutputStream logOutputStream = null;

  static private java.text.DateFormat dateFormatter =
                          java.text.DateFormat.getDateTimeInstance(
                                                java.text.DateFormat.FULL,
                                                java.text.DateFormat.FULL  );


  public

  LogToFile( OutputStream logOutputStream,
             String timeZoneString         )
/*****************************************************************************/
/* Function name: LogToFile                                                  */
/* File name: LogToFile.java                                                 */
/* Purpose: constructor for the class                                        */
/* Parameters: logOutputStream - the stream where to log the operations to   */
/*             timeZoneString                                                */
/* Returns:                                                                  */
/* Last modified: 30.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      TimeZone timeZone = null;
                                                       /* LogToFile */
/*---------------------------------------------------------------------------*/

      this.logOutputStream = logOutputStream;
      if (timeZoneString != null)
        {
          timeZone = TimeZone.getTimeZone( timeZoneString );
          dateFormatter.setTimeZone( timeZone );
        }
      else
        ;
    /*end of if*/

    }
/*end of LogToFile*/


  public
  synchronized
  void

  write( java.lang.String string )
/*****************************************************************************/
/* Function name: write                                                      */
/* File name: LogToFile.java                                                 */
/* Purpose: to write a string to log file                                    */
/* Parameters: string                                                        */
/* Throws: IOException                                                       */
/* Returns:                                                                  */
/* Last modified: 31.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      Thread currentThread = null;
      byte[] byteArrayToPrintOut = null;
      Date date;
      String stringToPrintOut = "";
      Calendar calendar = null;
                                                              /* write */
/*---------------------------------------------------------------------------*/

      try
        {
          if (logOutputStream != null)
            {
              currentThread = Thread.currentThread();
              calendar = Calendar.getInstance();
              date = calendar.getTime();
              stringToPrintOut += "\n" +
                dateFormatter.format( date ) +
                " " + currentThread.getName() +
                "\n";
              stringToPrintOut += string;
              byteArrayToPrintOut =
                stringToPrintOut.getBytes();
              logOutputStream.write( byteArrayToPrintOut,
                                     0, byteArrayToPrintOut.length );
              logOutputStream.flush();
            }
          else
            ;
        /*end of if on null*/
        }
      catch( IOException e )
        {
          System.out.println( "LogToFile, write: " +
                              "IOException!\n" +
                              e.getMessage()        );
        }
    /*end of try-catch*/
    }
/*end of write*/

} /*end of LogToFile */

/*****************************************************************************/
/* End of file: LogToFile.java                                               */
/*****************************************************************************/
