Page 1 of 1

Displaying your own debugger errors

Posted: Mon Jan 14, 2008 3:35 pm
by Dreamland Fantasy
Hi there,

Is there any way to get the PureBasic debugger to display custom errors?

For example say I have written a library of procedures where you need to initialise it with a InitMyLib(). If this InitMyLib() is missing from the code and one of the functions that require it has been called can I get the dubugger to display an error along the lines of "InitMyLib() needs to be called first".

Kind regards,

Francis.

Posted: Mon Jan 14, 2008 3:54 pm
by #NULL
that's how i do it currently:

Code: Select all

CompilerIf #PB_Compiler_Debugger
  
  
  Import "Debugger.lib"
    PB_DEBUGGER_SendError(error.s)
  EndImport
  
  Macro sgxERROR(_message_)
    PB_DEBUGGER_SendError("sgx ERROR: "+_message_)
  EndMacro
  
  ; [...]

Posted: Mon Jan 14, 2008 3:55 pm
by Rook Zimbabwe
Crap #NULL got there first! 8) Good Job #Null...

As an aside... you might also try the MessageRequester to generate a popup... :D

Posted: Mon Jan 14, 2008 3:58 pm
by gnozal
If your library is tailbiten, you may use TB_DebugError() like this :

Code: Select all

   ProcedureDLL MyDiv(a, b)
     ProcedureReturn a/b
   EndProcedure

   ProcedureCDLL MyDiv_DEBUG(a, b)
    If b=0
      TB_DebugError("Division by zero!")
    EndIf
   EndProcedure

Posted: Mon Jan 14, 2008 4:01 pm
by Dreamland Fantasy
Thanks for the quick answers guys! :D

Kind regards,

Francis.

Posted: Mon Jan 14, 2008 4:20 pm
by #NULL
it's very usefull.
for example you can encapsulate a *structure-array in macros to perform boundary-checks, or other things you need to verify, and to send debugger errors if necessary.
i.e. in my case here i filter invalid indices to index=0 (though the debugger stops anyway) which uses as a empty dummy element.

Code: Select all

;{ ---------------- MACRO: sgx()
CompilerIf #PB_Compiler_Debugger
  
  
  Import "Debugger.lib"
    PB_DEBUGGER_SendError(error.s)
  EndImport
  
  Macro sgxERROR(_message_)
    PB_DEBUGGER_SendError("sgx ERROR: "+_message_)
  EndMacro
  
  Macro sgx_debugger_check_array_bounds(index)
    index * bool( ((index>=0) And (index<=sgx_CURRENT_MAX_DYNAMIC_GADGETS)) Or sgxERROR("array out of bounds: sgx("+Str(index)+")") )
  EndMacro
  
  
  ; ****************************
  Macro sgx(index)
    *sgxID(sgx_debugger_check_array_bounds(index))
  EndMacro
  ; ****************************
  
  
;  ----------------
CompilerElse
  
  
  Macro sgxERROR(_message_)
    ; <empty>
  EndMacro
  
  ; ****************************
  Macro sgx(index)
    *sgxID(index)
  EndMacro
  ; ****************************
  
  
CompilerEndIf ;}
;  ----------------

Code: Select all

; the actual array is: Dim *sgxID.<structure>(..)

; boundary check if debugger enabled
sgx(id)\text = "bla"