Debug.cs :  » Persistence-Frameworks » csopf » Debugger » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » csopf 
csopf » Debugger » Debug.cs
/*
    C# Object Persistent Framework.
    Copyright (C) 2004  Yoon-Kit Yong

    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; either
    version 2.1 of the License, or (at your option) any later version.

    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
    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Yoon Kit:  yoonkit@users.sourceforge.net (yoonkit@yahoo.com)
*/

using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;


namespace Debugger{

    /// <summary>
    /// 040106  yky  Created
    ///                 Gosh, static on all items. Im not sure its the right 
    ///                 way to do it. All I want is just a global function
    ///                 visible to all... 
    /// </summary>
    public class Debugger
    {
        // 041008  yky  Created - Gets the Working Directory.
        public delegate string DelegateGetWorkingPath();
        static public DelegateGetWorkingPath GetWorkingPath;
        static public string DefaultGetWorkingPath() 
        {
            return "";  // 041015  yky  The Default Working Path. here!
        }

        // 040429  yky  Created - Delegate for different UI to show debug info.
        public delegate void DelegateShowDebug( DebugObj pDebugObj );
        static public DelegateShowDebug ShowDebug = null;

        //public const int WM_DEBUGMSG = 0x401; //WM_USER + 1;
        static private DebugList mList;
        /// <summary>
        /// Static constructor for the Debugger
        /// 040106  yky  Created
        ///                mList must be created here
        /// 040502  yky  Delegate
        ///                the Default ShowDebug delegate is linked here
        /// 041014  yky  Added Delegate for DoAdd               
        /// 041015  yky  Default Delegate for WorkingPath.               
        /// </summary>
        static Debugger()
        {
            mList = new DebugList();
            ShowDebug += new DelegateShowDebug( DefaultShowDebug );
            DoAdd += new DelegateDoAdd( DefaultDoAdd );
            GetWorkingPath += new DelegateGetWorkingPath( DefaultGetWorkingPath );
            Add( "===================  Starting Debug  ==================== " + DateTime.Now.ToString(), 0, "Debugger");
        }

        /// <summary>
        /// The default event handler when a new Debug is added
        /// 040502  yky Created
        ///                 Report() removed now.
        ///                 Just appends a line to the debug file.
        /// 041008  yky Trying to make use of GetWorkingPath so that ASP.Net apps can write to a good dir.                
        /// </summary>
        /// <param name="pDebugObj"></param>
        static public void DefaultShowDebug( DebugObj pDebugObj )
        {
            string vFName = "";
            vFName = GetWorkingPath();
            vFName += "debug.txt";
            //Report(); // 040111 yky  this is for debugging.. writes to file all the time.
            try
            {
                StreamWriter vFile = File.AppendText( vFName );
                vFile.WriteLine( String.Format("{0:00000}:{1}", mList.Count, pDebugObj.ToString() ));
                vFile.Flush();  // 040502  yky  not sure if this is necessary. Close might do it already.
                vFile.Close();
            }
            catch (UnauthorizedAccessException)
            {}
        }

        /// <summary>
        /// Add( a, b, c) is the crux of this static object
        /// 040106  yky  Created
        /// 040429  yky  Created 
        ///                the DelegateShowDebug and assigned it here.
        ///                 Calling function also from here.
        ///                 Calling Check just before return.
        /// 041014  yky  Now Calls the delegate DoAdd.                
        /// </summary>
        static public DebugObj Add( string pMsg, int pLevel, string pSrc )
        {
            if ((pLevel > 30) && (pLevel !=100 )) return null; // 041027  yky filtering out rubbish.
            DebugObj vDeb = DoAdd( pMsg, pLevel, pSrc );
            //DebugObj vDeb = mList.Add( pMsg, pLevel, pSrc );  // 041014  this is now called in the DefaultDoAdd
            ShowDebug( vDeb );  // 040429  yky  Calling the delegate here (thanks wei min)
            return vDeb;
        }

        // 041014  yky  Delegates defined which can change the way Debug records the objs.
        public delegate DebugObj DelegateDoAdd( string pMsg, int pLevel, string pSrc );
        static public DelegateDoAdd DoAdd;
        static public DebugObj DefaultDoAdd( string pMsg, int pLevel, string pSrc )
        {   // 041014  yky  DefaultDoAdd will just add it to the list.
            DebugObj vDeb = mList.Add( pMsg, pLevel, pSrc );
            return vDeb;
        }

        static public DebugObj Add( string pMsg, int pLevel )
        {
            return Add( pMsg, pLevel, "");
        }
        static public void Report()
        {
            mList.SaveToFile("debug.txt");
        }
        static public DebugObj GetLast()
        {
            return mList.GetLast();
        }
        static public DebugObj GetCurrent()
        {
            return mList.CurrentObj;
        }
    }
    /// <summary>
    /// 040106  yky  Created
    ///               This is the debug object which contains the message,
    ///               level of severity and Source of problems.
    /// </summary>
    public class DebugObj
    {
        private int fLevel;
        private DateTime fTime;
        private string fMsg, fSrc;

        public DebugObj( string pMsg, int pLevel, string pSrc )
        {
            fLevel = pLevel;
            fTime = DateTime.Now;
            fMsg = pMsg;
            fSrc = pSrc;
        }

        public DebugObj(): this( "", 0, "") {}

        public int Level  
          { get { return fLevel; }
            set { fLevel = value; } }
        
        public DateTime Time 
          { get { return fTime; }
            set { fTime = value; } }
        
        public string Msg 
          { get { return fMsg; } 
            set { fMsg = value; } }
        
        public string Src 
          { get { return fSrc; } 
            set { fSrc = value; } }

        public Color DebugColor
        {
            get 
            {   // 041119  yky  Returns the appropriate urgency colour.
                if (Level > 5) return Color.SkyBlue;
                else if (Level > -1) return Color.White;
                else if (Level > -3) return Color.Beige;
                else if (Level > -5) return Color.Yellow;
                else if (Level > -7) return Color.Orange;
                else if (Level > -11) return Color.OrangeRed;
                else if (Level > -16) return Color.Red;
                else return Color.Crimson;
            }
        }

        /// <summary>
        /// Shows the Debug info as a string
        /// 040502  yky  Created
        ///                Hacked from DebugList.SaveToFile. removed 'i'
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return  String.Format( "{0,3}: {2} .{1}",
                                   //{0:00000}: i, 
                                   Level.ToString(), // 040215  yky  PadLeft(3,'0') doesnt work for -neg numbers. -2 becomes "0-2"
                                   Path.GetExtension( "."+Src ), // 040215  yky Getting the important part of the object.
                                   Msg
                                 );
        }

        

    }

    /// <summary>
    /// 040106  yky  Created
    ///                 This is the List of all the DebugObjs
    ///                 Add, SaveToFile
    ///                   -todo Current bug, etc.
    /// </summary>
    public class DebugList
    {

        /*
        //  040426  yky  Need this PInvoke stuff to send messages to the form.
        //  040429  yky  This is not necessary anymore. Using delegates to do the same thing.
        [DllImport("User32.dll",EntryPoint="SendMessage")]
        private static extern int SendMessage(int hWnd,
            int Msg,
            int wParam,
            int lParam);
        */

        private ArrayList mList;
        public DebugList()
        {
            mList = new ArrayList();
        }

        ~DebugList()
        {
            //mList.Free();  // 040106  yky  What is the method to free the object?
      mList = null; //  040315  nick   Here's to hopping the GC will pick this up the next pass.
        }

        /// <summary>
        /// Adds the Message to the Debug List
        /// 040100  yky  Created
        /// 040426  yky  Added MessageBox if abs(pLev) >= 100
        ///              Also SendMessage to the forms if there is an active form.
        ///                 dont know why Message.Create doesnt work.
        /// 040429  yky  Removed Messages. Now using delegate to call the UI to update itself.
        ///                 neater solution. thanks weimin (marauderz)
        ///                 This in turn is called from the static Debugger object from now on.
        /// </summary>
        public DebugObj Add( string pMsg, int pLev, string pSrc )
        {
            DebugObj vDeb = new DebugObj(pMsg, pLev, pSrc);
            mList.Add( vDeb );
            _CurrentObj = vDeb;
            return vDeb;
        }
        /// <summary>
        /// 040106  yky  Created
        ///                 To save out the debug info into a text file.
        /// 040215  yky  Modified
        ///                 PadLeft doesnt work for negative numbers. -2 becomes 0-2. Changed to " "
        /// </summary>
        /// <param name="pFile">The Filename of the debug output</param>
        public void SaveToFile( string pFile )
        {
            int i = 1;
            StreamWriter vSW = new StreamWriter(pFile);
            foreach( DebugObj vDebug in mList )
            {
                vSW.WriteLine( 
                     String.Format("{0:00000}:{1}", i, vDebug.ToString() )
                             );
                /*
                vSW.WriteLine( 
                    String.Format( "{0:00000}:{1,3}: {3} .{2}",
                                   i, 
                                   vDebug.Level.ToString(), // 040215  yky  PadLeft(3,'0') doesnt work for -neg numbers. -2 becomes "0-2"
                                   Path.GetExtension( vDebug.Src ), // 040215  yky Getting the important part of the object.
                                   vDebug.Msg 
                                  ));*/
                i++;
            }
            vSW.Close();
        }

        public DebugObj GetLast()
        {
            return (DebugObj)(mList[ mList.Count-1 ]);
        }

        /// <summary>
        /// Gets the cached most current Debug Object
        /// 040426  yky  created
        /// </summary>
        private DebugObj _CurrentObj = null;
        public DebugObj CurrentObj
        {
            get
            {
                if (_CurrentObj==null) _CurrentObj = GetLast();
                return _CurrentObj;
            } 
            set
            {
                  _CurrentObj = value; 
            }
        }

        public int Count { get { return mList.Count; } }
    }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.