Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[TIP] How to use OutputDebug


  • Please log in to reply
9 replies to this topic
PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
OutputDebug

Sends a string to the debugger (if any) for display.
If the script's process has no debugger, the system debugger displays the string. If the system debugger is not active, this command has no effect.

One example of a debugger is DebugView, which is free and available at http://www.sysinternals.com/Utilities/DebugView.html

The usage is very simple: just put the string you want to see in the DebugView window. Note that if you put a `n in the string, it will made two lines in the window.

The advantages over using traditionnal MsgBoxes: no annoying sound, no need to click OK, process isn't interrupted so goes quickly, you get a log trace to analyze quietly within context.

The advantages over using some FileAppends to a log file: no file cluttering, easier syntax, immediate view.

Plus your lines get a time stamp, so you can analyse performance too.

Inconvenient: very long lines or multiline text are annoying to view.

Example of use, to see better how the URL extractor in the LoopReadFile page works:

URLSearchString =
( Join`s
<a href="http://www.autohotkey.com/docs/commands/URLDownloadToFile.htm"
title="URLDownloadToFile"><img
src="http://www.autohotkey.net/~PhiLho/KM64.png"
alt="A stylized man made of Celtic knots" title="KnotMan" />URLDownloadToFile</a>
)
MsgBox %URLSearchString%
Gosub URLSearch
MsgBox %result%
Return

URLSearch:
; It's done this particular way because some URLs have other URLs embedded inside them:
StringGetPos, URLStart1, URLSearchString, http://
StringGetPos, URLStart2, URLSearchString, ftp://
StringGetPos, URLStart3, URLSearchString, www.

; Find the left-most starting position:
URLStart = %URLStart1%  ; Set starting default.
Loop
{
   ; It helps performance (at least in a script with many variables) to resolve
   ; "URLStart%A_Index%" only once:
   StringTrimLeft, ArrayElement, URLStart%A_Index%, 0
   if ArrayElement =  ; End of the array has been reached.
      break
   if ArrayElement = -1  ; This element is disqualified.
      continue

[color=darkred]OutputDebug ArrayElement: %ArrayElement% -- URLStart: %URLStart%[/color]

   if URLStart = -1
      URLStart = %ArrayElement%
   else ; URLStart has a valid position in it, so compare it with ArrayElement.
   {
      if ArrayElement <> -1
         if ArrayElement < %URLStart%
            URLStart = %ArrayElement%
   }
[color=darkred]OutputDebug URLStart: %URLStart%[/color]
}

if URLStart = -1  ; No URLs exist in URLSearchString.
   return

; Otherwise, extract this URL:
StringTrimLeft, URL, URLSearchString, %URLStart%  ; Omit the beginning/irrelevant part.

[color=darkred]OutputDebug After trimming: %URL%[/color]

Loop, parse, URL, %A_Tab%%A_Space%<>  ; Find the first space, tab, or angle (if any).
{
   URL = %A_LoopField%
   break  ; i.e. perform only one loop iteration to fetch the first "field".
}

[color=darkred]OutputDebug After loop: %URL%[/color]

; If the above loop had zero iterations because there were no ending characters found,
; leave the contents of the URL var untouched.

; If the URL ends in a double quote, remove it.  For now, StringReplace is used, but
; note that it seems that double quotes can legitimately exist inside URLs, so this
; might damage them:
StringReplace, URLCleansed, URL, ",, All

[color=darkred]OutputDebug URLCleansed: %URLCleansed%[/color]

; Replaced the FileAppend with this
result := result . URLCleansed . "`n"

LinkCount += 1

; See if there are any other URLs in this line:
StringLen, CharactersToOmit, URL
CharactersToOmit += %URLStart%
StringTrimLeft, URLSearchString, URLSearchString, %CharactersToOmit%

[color=darkred]OutputDebug We recurse![/color]

Gosub, URLSearch  ; Recursive call to self.
return
Result (copy/paste from DebugView to here):
[3244] ArrayElement: 9 -- URLStart: 9
[3244] URLStart: 9
[3244] ArrayElement: 16 -- URLStart: 9
[3244] URLStart: 9
[3244] After trimming: http://www.autohotkey.com/docs/commands/URLDownloadToFile.htm" title="URLDownloadToFile"><img src="http://file.autohotkey.net/PhiLho/KM64.png" alt="A stylized man made of Celtic knots" title="KnotMan" />URLDownloadToFile</a>
[3244] After loop: http://www.autohotkey.com/docs/commands/URLDownloadToFile.htm"
[3244] URLCleansed: http://www.autohotkey.com/docs/commands/URLDownloadToFile.htm
[3244] We recurse
[3244] ArrayElement: 37 -- URLStart: 37
[3244] URLStart: 37
[3244] After trimming: http://file.autohotkey.net/PhiLho/KM64.png" alt="A stylized man made of Celtic knots" title="KnotMan" />URLDownloadToFile</a>
[3244] After loop: http://file.autohotkey.net/PhiLho/KM64.png"
[3244] URLCleansed: http://file.autohotkey.net/PhiLho/KM64.png
[3244] We recurse
Any question?

Golum
  • Guests
  • Last active:
  • Joined: --
Just a side note and I'm sure everyone's aware that sysinternals was bought by M$ just a few days ago.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Yes, I just saw that when writing the message... I hope that's for the best (like releasing for free the payware they did). It was a smart move, even if they had no access to Windows source code, they know probably more on Windows than lot of Microsoft programmers...

polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012
I knew about OutputDebug but never tried it till today - it's a great help! It makes debugging a whole load faster. I would still use the MsgBox and ListView combo as a step in/out method but for relatively small logging OutputDebug is the best. Thanks for your post :)

btw I think this fits best in Scripts & Functions because it's more of a tutorial/guide, not a request of help.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I knew about OutputDebug but never tried it till today - it's a great help! It makes debugging a whole load faster. I would still use the MsgBox and ListView combo as a step in/out method but for relatively small logging OutputDebug is the best. Thanks for your post :)

Of course, I do that too. Except for the ListView combo? Can you explain?
Indeed, the OutputDebug is very useful to trace complex scripts, like XML parsing, for example... ;-)

I hope someday Chris will be able to make a full debugger with step by step, breakpoints, variable dumps and setting, and so on. I know it is on the to do list...
Of course, that won't make these tricks obsolete, but they will be just a complement, not the only way to debug.

btw I think this fits best in Scripts & Functions because it's more of a tutorial/guide, not a request of help.

Pondered about that too, fearing the topic to be quickly lost in a sea of requests for games... But:

Ask for Help
Ask questions and (hopefully) get answers. Post helpful tips and tricks.

:)

polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012

Of course, I do that too. Except for the ListView combo?

Why did I say ListView lol - I meant ListVars sorry :p

Ask for Help
Ask questions and (hopefully) get answers. Post helpful tips and tricks.

I think Chris should leave tips and tricks to Scripts & Functions (like Goyyah decided to do) because such threads contain a script and/or function with some sort of explanation or guide - it's just more logical imo.

jballi
  • Members
  • 1023 posts
  • Last active:
  • Joined: 01 Oct 2005

Pondered about that too, fearing the topic to be quickly lost in a sea of requests for games... But:

Ask for Help
Ask questions and (hopefully) get answers. Post helpful tips and tricks.

:)

Although the description for the forum says it, posting helpful tips isn't so helpful because the Help forum is ridiculously (good and bad) active. Most postings get shoved to page 2 in a matter of hours.

A good alternative might be to create a new forum specifically for Tips and Tricks (Tips 'N Tricks?) Personally, I would hate for all of this quality information to get "lost in a sea of requests for games...".

Them be my thoughts...

Terrapin
  • Members
  • 107 posts
  • Last active: Feb 06 2007 03:38 PM
  • Joined: 15 Aug 2005
Thank You, PhiLho, for showing an example of this. I downloaded the "DebugView" utility a while back, but have not stopped to figure out how to use it. Lazy. I have a better idea now, of how.

Bob

Chris
  • Administrators
  • 10727 posts
  • Last active: Nov 15 2014 07:47 AM
  • Joined: 02 Mar 2004
Thanks for posting this debugging tip.

Concerning the forum/topic ideas raised above: I prefer that tips be kept in this forum rather than in Scripts & Functions. I feel that the goal for such tips shouldn't be to stay on page #1 of a topic listing; but instead to have good keywords to be findable via search. In addition, future posts can link to these tips to bring more attention to them.

Generally, I like having fewer forum areas rather than more because it's simpler and makes the site more accessible and navigable for casual visitors. When I visit another site's forum and see about 30 sub-forums in it (or even just a dozen), it's kind of a turn-off -- but maybe it's a matter of taste.

Murx
  • Guests
  • Last active:
  • Joined: --
My company won't allow to use external tools like DebugView. Because its said that the OS? provides a system debugger (and a check on google pointed out that its handled via IE (for XP)) how to use that with AHK?

You can help? Well, a tiny step2step tutorial would be much appreciated ! :(