Using a Macro to Create Trace Statements

Provided by the FMS Development Team

While trying to profile code, if you are not using one of those expensive tools, you will find yourself writing lots of trace statements. Here is some code for a macro (more about macros here), which automates that. Just place the cursor on a procedure you want to add trace statements and run this macro.

Code

  'position cursor inside a proc and run this

  Public Sub FMS_InsertTraceForProc()

 

    Dim edtrWin As Window = Nothing

    Dim prjItm As ProjectItem = Nothing

    Dim txtDoc As TextDocument = Nothing

    Dim ts As TextSelection = Nothing

    Dim tpStart As TextPoint = Nothing

    Dim tpEnd As TextPoint = Nothing

 

 

    Try

      ' get the active editor

      edtrWin = DTE.ActiveDocument.ActiveWindow

 

      If edtrWin Is Nothing Then

        DTE.StatusBar.Text = "No text editor open."

        Return

      End If

 

      prjItm = edtrWin.ProjectItem

 

      ' this may happen if the document is

      ' not part of the project

      If prjItm Is Nothing Then

        DTE.StatusBar.Text = "Projectitem is null !!"

        Return

      End If

 

      Dim lineTerminator As String = String.Empty

 

      ' chk for Basic language

      If prjItm.FileCodeModel.Language = _

        "{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}" Then

 

        lineTerminator = String.Empty

 

      End If

 

      ' chk for C# language

      If prjItm.FileCodeModel.Language = _

         "{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}" Then

 

        lineTerminator = ";"

 

      End If

 

      ' get the textdocument associated with this item

      txtDoc = prjItm.Document.Object("TextDocument")

      ts = DTE.ActiveWindow.Selection

 

      ' get the start point of the procedure

      tpStart = ts.ActivePoint.CodeElement _

        (vsCMElement.vsCMElementFunction).GetStartPoint(vsCMPart.vsCMPartBody)

 

      ' get the end point of the procedure

      tpEnd = ts.ActivePoint.CodeElement _

        (vsCMElement.vsCMElementFunction).GetEndPoint(vsCMPart.vsCMPartBody)

 

      ' build logic for inserting code

      ' if needed expand this to insert profiling code

      '

      Dim doubleQuotes As Char = """c"

      Dim procName As String = ts.ActivePoint.CodeElement _

        (vsCMElement.vsCMElementFunction).FullName

      Dim txtTraceLin1 As String = _

        "System.Diagnostics.Trace.Indent()" + lineTerminator

      Dim txtTraceLin2 As String = "System.Diagnostics.Trace.WriteLine(" _

        + doubleQuotes + procName + doubleQuotes & ")" + lineTerminator

      Dim txtTraceLin3 As String = "System.Diagnostics.Trace.Unindent()" _

        + lineTerminator

 

      ' try and open the undo context

      If Not DTE.UndoContext.IsOpen Then

        DTE.UndoContext.Open("FMS Inc Insert Trace macro.")

      End If

 

      Dim firstProcLine As Integer = tpStart.Line

 

      ts.MoveToPoint(tpStart)

      ts.NewLine()

      ts.LineUp()

      ts.EndOfLine()

 

      ts.Insert(txtTraceLin1)

      ts.NewLine()

 

      ts.Insert(txtTraceLin2)

      ts.NewLine()

 

      ts.MoveToPoint(tpEnd)

      ts.NewLine()

      ts.NewLine()

      ts.LineUp()

      ts.Indent()

      ts.Insert(txtTraceLin3)

 

      ' select the inserted property block to auto-format

      txtDoc.Selection.MoveToLineAndOffset(firstProcLine, 1, True)

 

      DTE.UndoContext.Close()

 

      ' format selected inserted block and keep selected

      DTE.ExecuteCommand("Edit.FormatSelection")

 

 

    Catch ex As System.Exception

      DTE.StatusBar.Text = "Macro failed.."

      DTE.StatusBar.Highlight(True)

    End Try

  End Sub


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS