// clsTextWriterListener.csb
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// 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.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Diagnostics;
using System.IO;

namespace GUIControlLogic.Error
{
    /// <summary>
    /// clsEmailListener is a trace listener that sends email.
    /// </summary>
    /// <remarks>None</remarks>
    public class clsTextWriterListener : TraceListener
    {
        #region Variable declaration

        /// <summary>
        /// Constant for the logfilename
        /// </summary>
        /// <remarks>To move to config file and not to hardcode</remarks>
        private const string LOG_FILE = "ZANZIClient_ErrorLog.txt";

        /// <summary>
        /// A reference to a stream writer
        /// </summary>
        /// <remarks>None</remarks>
        private static StreamWriter sw;

        //	private static bool instance_flag= false; 

        #endregion

        #region Functions

        /// <summary>
        /// Constructor for the textwriter listner class
        /// </summary>
        /// <remarks>None</remarks>
        public clsTextWriterListener()
        {
            //	fs = File.Open(LOG_FILE, FileMode.Open, FileAccess.Read,FileShare.ReadWrite);

            try
            {
                sw = new StreamWriter(LOG_FILE, true);

                sw.AutoFlush = true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                Debugger.Break();
            }
        }

        /// <summary>
        /// Write string to file
        /// </summary>
        /// <param name="str">String to write</param>
        /// <remarks>None</remarks>
        public override void Write(string str)
        {
            try
            {
                //base.Write (str);
                sw.Write(str);
                //CloseFile();
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                Debugger.Break();
            }
        }

        /// <summary>
        /// Writes line to file
        /// </summary>
        /// <param name="str">String to write</param>
        /// <remarks>None</remarks>
        public override void WriteLine(string str)

        {
            try
            {
                //base.Write (str);
                sw.WriteLine(str);
                //CloseFile();
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                Debugger.Break();
            }
        }

        /// <summary>
        /// Close the file after write
        /// </summary>
        /// <remarks>None</remarks>
        public void CloseFile()
        {
            sw.Flush();
            sw.Close();
        }

        #endregion
    }
}