Debug window request (and practical example)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

One feature I like in Visual Basic is the Debug.Print command,
which lets you print anything (a variable's contents, a message, etc)
to a window for viewing during your app's execution. This would be
great to see in PureBasic's debugger (hint, hint) because it really
aids in debugging an app. Yes, PureBasic has a variable viewing option
but the need to print messages during execution is essential, IMO.

Anyway, here's an example procedure that does exactly this, but prints
it to a text file (c:\debug.txt) instead of a window. Fred, if you could
add something like this to the debugger in a "Debug" window,
it would be truly appreciated!

Code: Select all

; Debug procedure for PureBasic (by PB; feel free to use).
; Usage: Debug(msg$)
; msg$ can be anything: a variable's contents, a message, etc.
; Output of msg$ is written to c:\debug.txt, which is always deleted
; whenever you compile+run your application.
; To show a message during program execution: Debug("message")
; To show the contents of a string variable : Debug("var$="+var$)
; To show the contents of a numeric variable: Debug("var="+str(var))
;
Global debug
Procedure Debug(msg$)
  if debug=0 : debug=1 : DeleteFile("c:\debug.txt") : endif
  f=FileSize("c:\debug.txt")
  If f=-1 : f=0 : EndIf
  r=OpenFile(0,"c:\debug.txt")
  If r0
    FileSeek(f)
    WriteStringN(msg$)
    CloseFile(0)
  EndIf
EndProcedure
Edited by - PB on 13 December 2001 13:17:07
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Wow, re-visiting my old thread from 8 years ago. :)

When I first requested this command, I had no idea it would actually be tied
to the debugger itself. I envisioned it working standalone to print something
at any time. However, if the debugger is off, the Debug command is off too.

My request: free it from the debugger (if possible). Because sometimes you
need to Debug some info when the debugger is off, and you can't, so you
have to use a console window or messagerequester. Both methods add a
lot of extra code where a quick Debug command would be.

Here is what I'm using in the meantime. It's a macro where I replace the
Debug command with Debugg (double G on the end). It means I can debug
some text when the debugger is off, by enabling and disabling the debugger.
It may look silly, but the speed gains are enormous! Look at both examples
to see what I mean.

Code: Select all

; Debugger is enabled because I need to see all Debugs.
; However, this comes at a cost -- my loop is slow! :(

; Loop takes a long 10 seconds to execute on my PC.

s=GetTickCount_()

For a=1 To 20000000
  b.f+0.1
  If b/1000=Int(b/1000)
    Debug(b)
  EndIf
Next

Debug("Done in "+Str(GetTickCount_()-s)+" ms")
My enhanced version:

Code: Select all

; Debugger is disabled for faster speed,
; but I still get to see all Debugs! :)

; Loop takes only 3 seconds now to execute on my PC.

Macro Debugg(text)
  EnableDebugger
  Debug text
  DisableDebugger
EndMacro

DisableDebugger

s=GetTickCount_()

For a=1 To 20000000
  b.f+0.1
  If b/1000=Int(b/1000)
    Debugg(b)
  EndIf
Next

Debugg("Done in "+Str(GetTickCount_()-s)+" ms")
And before anyone says anything: this is just a proof of concept, so I'm
not concerned about my macro checking the state of the debugger at
this time. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

I wrote a bunch of 'log' procedures just for this purpose. And whenever I want to 'debug' something, I instead 'log' it. The log function itself figures out if it should actually create a real logfile, pop up a window and show the entry etc. etc. etc.

What you are requesting looks like, well, a Debug command that just outputs information to a dedicated window, and that's it.

Hmmm.

What's the advantage over writing something yourself?

(Don't get me wrong, I'm just trying to understand!)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> What you are requesting looks like, well, a Debug command that just
> outputs information to a dedicated window, and that's it

That's exactly right. That's what I assumed the Debug command would do,
but it doesn't -- it requires the debugger to do it. I want it freed from the
debugger because sometimes you disable the debugger for speed reasons.

> What's the advantage over writing something yourself?

Native functions are always faster because Fred writes them in ASM. :)
Have you seen how fast you can Debug 1000 items to the Debug Output
window? :shock: Also, writing something myself means I need to add code to
my app to open a window, create a ListIconGadget, and so on. It all adds
bloat and it will be included in the final exe too, which is not the idea.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

To do timing, you should turn off the debugger for the entire file. DisableDebugger is not enough.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@Trond: That's precisely why we need a Debug command that can still work
with the debugger off. There'll be times we need fast speed for testing but
still be able to print out values.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

maybe you could use copydata and post messages to a monitor app
Post Reply