/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library 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
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/

namespace Novell.CASA.GUI
{
	using System;
	using System.Text;
	using System.Diagnostics;
	using System.IO;    
	using System.Threading;

	using Gtk;

    class Logger
    {
        private static string debugLog = null;
        private static Stream debugStream= null;
        private static Mutex dbgmutex = new Mutex(); 

		static Gtk.TreeStore tsLog;

        static Logger()
        {
#if DEBUG
             debugLog = GetHomeDir() + "/.CASAManager.logs";

             // Set up for Debug

             if( File.Exists( debugLog ) )
             {
                 File.Delete( debugLog );
             }

             debugStream = File.Create(debugLog);

             Debug.Listeners.Add(new TextWriterTraceListener(debugStream));
             Debug.AutoFlush = true;
             Debug.Indent();
             Debug.WriteLine("Debug Log created");
#endif
          }


		public static void ActivateLogWindow(Gtk.TreeView tvLog)
		{
			tsLog = new TreeStore(typeof(string));
			tvLog.Model = tsLog;						
		}

		public static void StopLogWindow()
		{
			tsLog = null;
		}
      
        public static void logbreak()
        {
            dbgmutex.WaitOne();
            Debug.WriteLine("  ") ;
            Debug.WriteLine("----------------------------------------------------") ;
            Debug.WriteLine("  ") ;
            dbgmutex.ReleaseMutex();          
          }


       // The log format is Time stamp :Component name: Error description 
        public static void DbgLog(string message)
        {				
			if (tsLog != null)
			{				
				tsLog.AppendValues(DateTime.Now.ToLongTimeString()
					+ ":"
					+ message);
			}

#if DEBUG
            dbgmutex.WaitOne();

            Debug.Write(DateTime.Now.ToLongTimeString());
            Debug.Write(" " + DateTime.Now.ToLongDateString());
            Debug.Write(":");
            Debug.WriteLine(message);

            dbgmutex.ReleaseMutex();
#endif
        }    
       
        public static void ExpLog(string message)
        {
			if (tsLog != null)
			{
				tsLog.AppendValues(DateTime.Now.ToLongTimeString()
					+ ": Exception encountered - ");
				tsLog.AppendValues(message);

				StackTrace st1 = new StackTrace();
				tsLog.AppendValues(st1.ToString());				
			}

#if DEBUG
            dbgmutex.WaitOne();

            Debug.Write(DateTime.Now.ToLongTimeString());
            Debug.Write(" " + DateTime.Now.ToLongDateString());
            Debug.Write(": Exception encountered - ");
            Debug.WriteLine(message);
            Debug.WriteLine("  ") ;
            StackTrace st = new StackTrace();
            Debug.WriteLine(st.ToString());

            dbgmutex.ReleaseMutex();
#endif
        }
        static string GetHomeDir()
        {
             int platform = (int)Environment.OSVersion.Platform;

             if ( (platform == 128) || ( platform == 4) )
             {
                  return System.Environment.GetEnvironmentVariable("HOME");
             }
             else
             {
                 return (System.Environment.GetEnvironmentVariable("HOMEDRIVE")) +
                        (System.Environment.GetEnvironmentVariable("HOMEPATH"));
             }
        }

    }   
}