Monday, 20 January 2014

Debug logging example for Mapbasic

In this post I will demonstrate an example debugging method that allows developers to enable logging to the message window and switch it off when released to the users.

I personally have used similar implementations for development and find it very useful for debugging errors on a client machine.

The example code can be downloaded from here DebugLoggingExample.zip

INSTRUCTIONS:

The mbx will run and create a toolbar like this


To enable degug logging create a text file in the mbx directory called 'DEBUG' with no file extension (see download for example)

To initialise debug mode there is a call to the EnableDebugMode sub routine. If the DEBUG file exists, the global variable gDebugModeEnabled is set to true.

Every time you call the DebugLog() sub routine, if gDebugModeEnabled is equal to true, the string message parsed to the sub routine will be printed to the message window.

This example iterates through a for loop counting to 3. If debug logging is enabled the message window will look something like this.





















Example code below:

Include "MAPBASIC.DEF"
Include "ICONS.DEF"
Include "MENU.DEF"

Declare Sub Main
Declare Sub TestDebugLogging
Declare Sub EnableDebugMode
Declare Sub DebugLog(ByVal strMsg as String)

Define APP_BUTTONPAD_NAME "Debug Logging Example"

Global gDebugModeEnabled as Logical
'--------------------------------------------------------------------------------
Sub Main
OnError goto CatchEx
  
   Call EnableDebugMode
  
   Create ButtonPad APP_BUTTONPAD_NAME As
      PushButton HelpMsg "\nExample of Debug Logging"
      Calling TestDebugLogging
      Icon MI_ICON_WRENCH_2
      Position(3,3)
      Show

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'-------------------------------------------------------------------------------
Sub TestDebugLogging
OnError goto CatchEx
   Call DebugLog("Entered Sub TestDebugLogging")
  
   Dim i as Integer
   Dim strMsg as String
  
   Call DebugLog("Begin counting to 3")
 
   For i = 1 to 3
      Note i
      Call DebugLog("i = " & i)
   Next
  
   Call DebugLog("Completed counting to 3")

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'------------------------------------------------------------------------------
Sub EnableDebugMode
OnError Goto CatchEx
  
   gDebugModeEnabled = false

   If FileExists(ApplicationDirectory$() & "DEBUG") then 'Check if DEBUG file exists
      gDebugModeEnabled = true 'Set the global variable
      Print Chr$(12) 'Clear the message window
   End If

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'------------------------------------------------------------------------------
Sub DebugLog(ByVal strMsg as String)
OnError Goto CatchEx
   If gDebugModeEnabled then
      Print FormatDate$(CurDate()) & " @ " & Time(12) & ": " & strMsg
   End If

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'-----------------------------------------------------------------------------

3 comments:

  1. Very nice, James.
    This is quite similar to my approach.
    You can even add a DebugNote procedure that will only show the dialog if you are running in debug mode.

    I also like that you have error handlers in all your procedures. Very tidy!

    ReplyDelete
  2. Thanks Peter,

    I have actually developed a procedure that also takes into account logging levels. Sort of like a simplified version of log4net.

    Sub MbDebug(strLogLevel, strMsg)

    I have 3 levels of logging, Debug, Warning, Error. So for example, if you only want to log when an error occurs you can.

    If I have a DEBUG file in the app directory, it will print everything to the message window and log warnings and errors to a log file.
    If I have a WARNING file in my app directory it will only print and log Warning and Errors to message window and log file

    If I have nothing in the app directory then only errors will be logged.

    Also if you call MbDebug("ERROR", err() & " An error msg" ) it will show the error also in a dialog.

    For this post I kept it simple but I could post the full blown implementation if anyone is every interested?

    Thanks

    James

    ReplyDelete
  3. This territory is renowned for being the zone close where a considerable measure of history of the United States resulted. North America took a huge part in the historical backdrop of Log Houses - this is where they wound up common, were enhanced and created.https://howdoesyourgardenmow.com/chicago-electric-chainsaw/

    ReplyDelete