Page 1 of 1

DebugOnce - Only shows the debug message the first time encountered

Posted: Thu Oct 07, 2021 10:00 am
by Axeman
It would be useful to have a 'DebugOnce' command for tracking down bugs where you believe certain sections of code are not executing, but the code is in a loop and you don't want to show the message each time the loop is run.

The 'DebugOnce' command should basically work the same way as the Debug command, but a given instance of it should be ignored after it has run once.

eg.

Code: Select all

For a = 1 to 10
DebugOnce "a"
For b = 1 to 10
DebugOnce "b"
For c = 1 to 10
DebugOnce "c"
Next
Next
Next
Should show:-
a
b
c

Re: DebugOnce - Only shows the debug message the first time encountered

Posted: Thu Oct 07, 2021 11:35 am
by luis
Incidentally I've put this functionality in my debug routines for the same reason, but instead of printing just once I set a timeout. For example 1000 ms means a debug statement will not be executed again until 1 second is passed.
That way you can still get updates but the debug output is not flooded.

Re: DebugOnce - Only shows the debug message the first time encountered

Posted: Thu Oct 07, 2021 1:15 pm
by BarryG
Axeman wrote: Thu Oct 07, 2021 10:00 amthe code is in a loop and you don't want to show the message each time the loop is run
Here's a crappy workaround I made... does it do what you want, and help for now? Obviously it's not meant for high-speed apps, lol.

To use it, call DebugOnce() with the text to show, and the loop name as a string.

Code: Select all

Global debugonce$,debugloopname$
Procedure DebugOnce(text$,loopname$)
  If debugloopname$<>loopname$
    debugloopname$=loopname$
    t$=Chr(1)+loopname$+Chr(255)
    If FindString(debugonce$,t$)=0
      debugonce$+t$
      Debug text$
    EndIf
  EndIf
EndProcedure

For a = 1 To 10
  DebugOnce("a = "+Str(a),"a")
  For b = 1 To 10
    DebugOnce("b = "+Str(b),"b")
    For c = 1 To 10
      DebugOnce("c = "+Str(c),"c")
    Next
  Next
Next

Re: DebugOnce - Only shows the debug message the first time encountered

Posted: Thu Oct 07, 2021 4:43 pm
by HeX0R

Code: Select all

CompilerIf #PB_Compiler_Debugger
	NewMap __L.i()
	
	Macro DebugOnce(text)
		If FindMapElement(__L(), Str(#PB_Compiler_Line) + #PB_Compiler_Filename) = 0
			__L(Str(#PB_Compiler_Line) + #PB_Compiler_Filename) = #True
			Debug text
		EndIf
	EndMacro
CompilerElse
	Macro DebugOnce(text) : EndMacro
CompilerEndIf

For a = 1 To 10
	DebugOnce("a = "+Str(a))
	For b = 1 To 10
		DebugOnce("b = "+Str(b))
		For c = 1 To 10
			DebugOnce("c = "+Str(c))
		Next
	Next
Next

Re: DebugOnce - Only shows the debug message the first time encountered

Posted: Thu Oct 07, 2021 9:59 pm
by BarryG
Beautiful, HeX0R! That's the sort of thing I was hoping for but didn't have the skills to do. Thanks!