DebugOnce - Only shows the debug message the first time encountered

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

DebugOnce - Only shows the debug message the first time encountered

Post 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
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

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

Post 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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

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

Post by BarryG »

Beautiful, HeX0R! That's the sort of thing I was hoping for but didn't have the skills to do. Thanks!
Post Reply