Archive for November, 2007

Debug Class

November 30, 2007
Still have some trouble with remote debugging on XPe.
 
I decided to formalise the writeline technique with a debug class called Debugg.
This borrows from the debug macros/API in Windows CE where messages are conditional and local.
 
Each class instantiates the class in its constructor
The first class opens the file to write to.
You place a Debugg method at the start and end of each function that you want to debug.
There are log messages that you then use.
 
Its posted below:
=================

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using System.Collections;

 

namespace Debugg

{

    public class logit

    {

        /// <summary>

        /// Constructors

        /// Place in paraent class Initialiser/contructor at end.

        /// </summary>

        public logit()

        {

            _logit( "VOID");

        }

        public logit(String ClassFile)

                    {

            _logit( ClassFile);

        }

        private void _logit(String ClassFile)

        {            

            if (!db) return;

 

            _ClassFile = ClassFile;

            if (_classCount++ == 0) firstClass = true;

            else firstClass = false;

 

            if (tw == null)

            {

                try

                {

                    if (_logFile != "") tw = new StreamWriter(_logFile);

                }

                catch { }

 

            }

            if (tw != null)  tw.WriteLine((_index++).ToString() + _ClassFile + " Entered");

        }

 

        /// <summary>

        /// Properties/Fields

        /// </summary>

 

        private string _ClassFile = "";

        private int _classCount =0;

        private Boolean firstClass = false;

 

        private static Boolean  _db = true;

        public static Boolean db

        {

            get { return _db; }

            set {

                _db =                     value;

            }

        }

 

        private  Boolean _mydb = true;

        public  Boolean mydb

        {

            get { return _mydb; }

            set

            {

                _mydb = value;

            }

        }

 

        private static int _index=1;

        public static int index

        {

            get { return _index; }

            set { _index = value ; }

        }

 

        private static TextWriter tw=null;

        private static String _logFile="C:\\temp\\loggfile.txt";

        public static String logFile

        {

            get { return _logFile; }

            set {

                if (tw != null) tw.Close();

                _logFile = value;

            }

        }

 

        /// <summary>

        /// Logging methods

        /// </summary>

        /// <param name="num"></param>

        /// <param name="message"></param>

        public void logg(int num,String message)

        {

            if (!db) return;

            try

            {

                if (_db) tw.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + num.ToString() + " " + message);

            }

            catch { }

            }

        public void logg(String message)

        {

            if (!db) return;

 

            try

            {

                if (_db) tw.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + message);

            }

            catch{}

        }

        public void logg()

        {

            if (!db) return;

 

            try

            {

                if (_db) tw.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + "Closing");

            }

            catch { }

            }

 

        /// <summary> 
        /// Function enter/exit logging 
        /// Place as first loc in function and as last loc in function 
        /// </summary> 
        private string _currentFunctionName =""; 
        private  Stack myStack = new Stack(); 
        public void enterr(String FunctionName) 
        { 
            if (!db) return; 
              try 
            { 
                myStack.Push(_currentFunctionName); 
                _currentFunctionName = FunctionName; 
                if (_db) 
                { 
                    tw.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + "Entered"); 
                    System.Diagnostics.Debug.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + "Entered");                 } 
             } 
            catch{}{}; 
        }

        public void exitt( ) 
        {  
            if (!db) return; 

              try{ 
                _currentFunctionName = myStack.Pop().ToString(); 
                if (_db) { 
                    tw.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + "Exited"); 
                     System.Diagnostics.Debug.WriteLine((_index++).ToString() + _ClassFile + " " + _currentFunctionName + " " + "     " + " " + "Exited"); 
                }

            } 
            catch{}{}; 
        }

 

        /// <summary> 
        /// Place in parent class Dispaose method: 
        /// </summary> 
        public void done() 
        { 
            if (!db) return; 

            logg(); 

            { 
                try 
                { 

                    if (firstClass) 
                    { 
                        tw.Flush(); 
                        tw.Close(); 
                    } 
                } 
                catch   {   } 
            } 
        }   
    }
}

 

=================
Enjoy!

Advertisements