Announcement

Collapse
No announcement yet.

How to create a debug function?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a debug function?

    I'm testing out some code I'm in the process of writing and basically I'd like to have a function that is similar to the built-in print() function, but is controlled by a variable that I can set or clear based on whether I was trying to debug or not. So basically something like the following pseudo code:
    Code:
    var debug := 1; // 1 means we're in debug mode, 0 means we're in release mode
    
    func debugPrint(<parameters>)
      if(debug == 1)
        print(<parameters>);
      else
        // do nothing
      endif
    endfunc
    
    func main()
      debugPrint("test message");
      debugPrint("the value of x is ", x);
      debugPrint([HEX2]myHexValue);
    endfunc
    So note that I want debugPrint() to be able to take an arbitrary number of parameters, which is what I meant by the <parameters> part in there, just like the existing print() function does. Then I can just switch the
    Code:
      var debug := 1;
    to
    Code:
      var debug := 0;
    to toggle these print statements, without having to find all of them in my code and comment them all out. Is there already a function that does something like this? Or if not, what would be the syntax to write the function I want where it can take an arbitrary number of parameters and pass them along to print()?

  • #2
    I generally do something like have separate functions debugprint1(var p1), debugprint2(var p1, var p2), etc. They then all call the 'largest' print using dummy parameters.

    Also you can do this sort of thing
    Code:
    #constant debugprint
    ..
    #IF EXISTS debugprint
    func debugprint1(var p1)
        debugprint2(p1, "")
    endfunc
    func debugprint2(var p1, var p2)
        print(p1, [STR] p2)
    endfunc
    #ENDIF
    ..
    ..
    ..
    #IF EXISTS debugprint
        debugprint1(x) ;
    #ENDIF
    ..
    That way you turn on and off debugging by commenting out the #constant at the top. Plus you don't waste 'cycles' on debug code as it gets completely removed when you aren't debugging.
    Mark

    Comment


    • #3
      I guess if I'm using the #IF EXISTS debugprint / #ENDIF method, I really don't need all the debugprintX() functions at all, I can just wrap all my original print() calls within the #IF/#ENDIF brackets. Thanks, I think that will work for me.

      Sean

      Comment

      Working...
      X