//
// Log.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//    http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is Log.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
//
// $Id: Log.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: Log.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:16:59  karl
// import of SNIA 0_7
//
// Revision 1.6  2000/08/16 05:59:59  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.5.6.1  2000/08/10 00:03:35  shawn
// head merge
//
// Revision 1.5  2000/07/03 13:54:25  davidsi
// Fixed problem whereby stderr was being closed.
//
// Revision 1.4  2000/05/11 10:12:07  davidsi
// - Fixed tabs.
//
// Revision 1.3  2000/05/06 17:16:22  davidsi
// - Remove \n from strings.
//
// Revision 1.2  2000/04/09 14:24:20  davidsi
// - Add support for specifying a log file name.
// - Add support for transactions (multiple log messages with the same
//   timestamp).
//
// Revision 1.1.1.1  2000/02/06 16:41:02  davidsi
// Initial version.
//
//
//
package org.snia.wbemcmd.utils;

import java.io.*;
import java.util.*;
import org.snia.wbemcfg.GlobalConfig;

/**
 * A simple logging utility class, mostly used while in development 
 * stage. This class is not fully thread-safe. Is is not recommended
 * for the use in production environment.
 */
public class Log 
{
    static private boolean dateWritten=false;
    static private String pathName=null;
    
    /**
     * set the name of the file to log the messages
     *
     * @param name   
     *      name of the file
     */
    static public void assignLogFileName(String name) 
    {
        pathName=name;
    }

    /**
     * writes the specified string to the log file
     *
     * @param instr
     *      string to write to the file
     */
    static public void println(String instr) 
    {
        if (GlobalConfig.DEBUG_XML || GlobalConfig.DEBUG_XMLDECODE)
        {
            PrintWriter fileStream;
            boolean doClose=false;
            try 
            {
                if (pathName==null)
                {
                    pathName=GlobalConfig.defaultLogFile;
                }
                fileStream=new PrintWriter( new FileOutputStream( pathName, true));
                doClose=true;
            } 
            catch (IOException io) 
            {
                fileStream=new PrintWriter(System.err);
            }

            Date date = new Date( System.currentTimeMillis() );
            fileStream.println(date.toString()+":");
            fileStream.println(instr);
            if (doClose)
            {
                fileStream.close();
            }
        }
    }
    
    /**
     * beginning of the transaction - puts the timestamp
     */
    static public void startTransaction() 
    {
        dateWritten=false;
    }
    
    /**
     * writes the transaction string to the file
     *
     * @param instr
     *      string to write to the file
     */
    static public void printlnTransaction(String instr) 
    {
        if (GlobalConfig.DEBUG_XML || GlobalConfig.DEBUG_XMLDECODE)
        {
            PrintWriter fileStream;
            boolean doClose=false;

            try 
            {
                if (pathName==null)
                {
                    pathName=GlobalConfig.defaultLogFile;
                }
                fileStream=new PrintWriter( new FileOutputStream( pathName, true));
                doClose=true;
            } 
            catch (IOException io) 
            {
                fileStream=new PrintWriter(System.err);
            }

            if (!dateWritten) 
            {
                Date date = new Date( System.currentTimeMillis() );
                fileStream.println(date.toString()+":");
                dateWritten=true;
            }
            fileStream.println(instr);
            if (doClose)
            {
                fileStream.close();
            }
        }
    }
    
    /**
     * write the transaction - puts the timestamp
     */
    static public void printlnTransaction() 
    {
        if (GlobalConfig.DEBUG_XML || GlobalConfig.DEBUG_XMLDECODE)
        {
            PrintWriter fileStream;
            boolean doClose=false;

            try 
            {
                if (pathName==null)
                {
                    pathName=GlobalConfig.defaultLogFile;
                }
                fileStream=new PrintWriter( new FileOutputStream( pathName, true));
                doClose=true;
            } 
            catch (IOException io) 
            {
                fileStream=new PrintWriter(System.err);
            }

            if (!dateWritten) 
            {
                Date date = new Date( System.currentTimeMillis() );
                fileStream.println(date.toString()+":");
                dateWritten=true;
            }
            fileStream.println();

            if (doClose)
            {
                fileStream.close();
            }
        }
    }
   
}
