Jump to content

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

simple Debug console Output


  • Please log in to reply
10 replies to this topic
dorfl68
  • Members
  • 92 posts
  • Last active: Sep 03 2013 08:03 PM
  • Joined: 11 Jun 2010
As a beginner, I was struggling a lot with debug messages. MSGBOX interrupts the script, TrayTip or similar is slow or only temporary, I was never happy.

The attached extremely short code has two simple functions. Essentially, it creates a console window the first time you send a debug message and traces from there, It's darn fast compared to all the other fancy methods, and it's fully scrollable. When I trace, I routinely have several hundred lines here with no issue.

usage: DebugMessage(text) ; text without line feed

The first time it's called, the console initializes. No check for errors since I assume only developers use it. The console is kept in the background so any check for active window works. Some specific details that you can adapt.

dorfl

DebugMessage(str)
{
 global h_stdout
 DebugConsoleInitialize()  ; start console window if not yet started
 str .= "`n" ; add line feed
 DllCall("WriteFile", "uint", h_Stdout, "uint", &str, "uint", StrLen(str), "uint*", BytesWritten, "uint", NULL) ; write into the console
 WinSet, Bottom,, ahk_id %h_stout%  ; keep console on bottom
}

DebugConsoleInitialize()
{
   global h_Stdout     ; Handle for console
   static is_open = 0  ; toogle whether opened before
   if (is_open = 1)     ; yes, so don't open again
     return
	 
   is_open := 1	
   ; two calls to open, no error check (it's debug, so you know what you are doing)
   DllCall("AttachConsole", int, -1, int)
   DllCall("AllocConsole", int)

   dllcall("SetConsoleTitle", "str","Paddy Debug Console")    ; Set the name. Example. Probably could use a_scriptname here 
   h_Stdout := DllCall("GetStdHandle", "int", -11) ; get the handle
   WinSet, Bottom,, ahk_id %h_stout%      ; make sure it's on the bottom
   WinActivate,Lightroom   ; Application specific; I need to make sure this application is running in the foreground. YMMV
   return
}


Nader
  • Guests
  • Last active:
  • Joined: --
could you give an example, please?

Lexikos
  • Administrators
  • 9394 posts
  • Last active:
  • Joined: 17 Oct 2006
; Example
DebugMessage("hello, world!")
Sleep 3000
:roll:

If you replace the WriteFile call with the following, it will work on AutoHotkey_L Unicode builds:
FileAppend %str%, CONOUT$
Unfortunately if you run the script from SciTE, the console window will never appear and the above won't have any apparent effect. If you WriteFile to h_Stdout, the text appears in SciTE's output pane. I'd generally use SciTE's output pane for debugging anyway, so I use my own debugging function which basically just does this:
FileAppend  %str%`n, *  ; Works with SciTE and similar editors.
OutputDebug %str%`n     ; Works with Visual Studio and DbgView.
... but if you use that, the output won't appear in the console (even if it is visible).

dorfl68
  • Members
  • 92 posts
  • Last active: Sep 03 2013 08:03 PM
  • Joined: 11 Jun 2010
Can you explain that? I am desperately trying to get a better debugging going. I am using Notepad++ - is sciTe similar

Also, what's a Unicode build? Beginner with too big a project here, so thanks!

Lexikos
  • Administrators
  • 9394 posts
  • Last active:
  • Joined: 17 Oct 2006

Can you explain that?

Explain what? CONOUT$ refers to the active console output buffer. See CreateFile.

I am using Notepad++ - is sciTe similar

I suggest you follow the link in my previous post and find out for yourself. :)

If you run the script via NppExec, its console window will catch stdout. It has to be set up, though, whereas SciTE4AutoHotkey is (as the name suggests) already set up for scripting. SciTE4AutoHotkey also supports interactive debugging via AutoHotkey_L. (Come to think of it, Notepad++ does too if you have the DBGp plugin; but it's not set up by default.)

Also, what's a Unicode build?

It's the default for AutoHotkey_L installations. AutoHotkey Basic and other ANSI builds only support a limited number of characters. Unicode allows a much larger range of characters, so is essential for scripts that might be used in multiple locales/with multiple languages.

dorfl68
  • Members
  • 92 posts
  • Last active: Sep 03 2013 08:03 PM
  • Joined: 11 Jun 2010
Thanks. I have top look into that Unicode build if I ever support Eastern Languages (right now, Paddy supports only ANSI, now that I know what it is)

Thanks for all the links! I definitely read on this, need to learn more about debugging - my practices are slow and horrible, it seems


CONOUT$ refers to the active console output buffer.


A, cool. So is that faster than the DLL call? I have no idea how AHK works internally..What better/faster/insert criteria here?

Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
Also take a look at my debugging library (in the sig) for how I debug.

I never thought of interacting with a console window... I'll look into adding support for that in my own library.

dorfl68
  • Members
  • 92 posts
  • Last active: Sep 03 2013 08:03 PM
  • Joined: 11 Jun 2010
Cool tool, I will try.
The advantage of the console is that is FAST. I was tracking incoming Midi Messages, and most other debug stuff into specialized GUIs etc was too slow so messages were skipped.

Never looked into this OutputDebug stuff, probably also nice and fast

  • Guests
  • Last active:
  • Joined: --
Hi, this script doesn't seem to work for me (nothing happens when I run the example, no window, no console, just the tray icon)

Using AHK basic... What am I doing wrong?

Leef_me
  • Moderators
  • 8125 posts
  • Last active: Today, 12:39 PM
  • Joined: 08 Apr 2009

Hi, this script doesn't seem to work for me (nothing happens when I run the example, no window, no console, just the tray icon)

Using AHK basic... What am I doing wrong?


We have no clue what is in your version of the script therefore we can't tell what you did wrong.

I put the following code in a script, and then added the code in "dorfl68" first post.
I get a console window titled "Paddy Debug Console" with the various messages I sent to it.
; Example
DebugMessage("hello, world!")
Sleep 3000

loop, 100
{
DebugMessage("this is a test " A_Sec)
sleep, 1000
}

return

#singleinstance force
#Persistent


Slovan
  • Guests
  • Last active:
  • Joined: --

Hi, this script doesn't seem to work for me (nothing happens when I run the example, no window, no console, just the tray icon)

Using AHK basic... What am I doing wrong?


It didn't work for me either until I commented out this line
WinActivate,Lightroom   ; Application specific; I need to make sure this application is running in the foreground. YMMV

After that, it worked, but only with the example provided by Lexikos.