// clsSystemLog.cs
// 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;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for clsSystemLog.
    /// </summary>
    /// <remarks>None</remarks>
    public class clsSystemLog
    {
        //zzoi 3june2008
        //in order to prevent cross thread errors

        #region Delegates

        public delegate void UpdateUICallBack(string msg);

        #endregion

        private readonly ctrlLog ctrl = new ctrlLog();

        public clsSystemLog()
        {
            //
            // TODO: Add constructor logic here
            //

            //register an event handler for system events
            ZANZILib.clsMain.SystemLogMessageEvent += WriteSystemLog;
        }


        public ctrlLog GetSystemLogCtrl
        {
            get { return ctrl; }
        }

        /// <summary>
        /// Writes to the system log
        /// </summary>
        /// <param name="str">String to write</param>
        /// <remarks>None</remarks>
        private void WriteSystemLog(string str)
        {
            if (ctrl.txtLog.InvokeRequired)
            {
                ctrl.txtLog.Invoke(new UpdateUICallBack(WriteSystemLog_txtUpdate), (str));
            }

            else
            {
                WriteSystemLog_txtUpdate(str);
            }


            //ctrl.txtLog.Text  = ctrl.txtLog.Text  + "[" + DateTime.Now + "]  ";
            //ctrl.txtLog.Text  = ctrl.txtLog.Text  + str + "\r\n";
        }


        private void WriteSystemLog_txtUpdate(string str)
        {
            //ctrl.txtLog.Text = ctrl.txtLog.Text + "[" + DateTime.Now + "]  ";
            //ctrl.txtLog.Text = ctrl.txtLog.Text + str + "\r\n";


            ctrl.txtLog.Text = "[" + DateTime.Now + "]  " + str + "\r\n" + ctrl.txtLog.Text;
        }
    }
}