Jump to content

My debug function


sugi
 Share

Recommended Posts

I'm trying to write a general debug function for my scripts that can be controlled by command line and directs the output either to a file or call the kernel32 OutputDebugString function.

The function only initializes its variables if they are required and parses the command line to figure out if debugging is wanted at all. Also nice is that I can just set a few variables at the top of the script to enforce debugging, even if I cannot change the command line.

The function requires that the script does not mess around with the $CmdLine variable, but I never do that anyway.

Currently there are two things I do not really like or are not sure if they are a good idea:

- Global variables. I have not been able to figure out a better way. Are there persistent variables like in C that are local to the function but are kept when the function returns?

- Keeping the kernel32.dll loaded for a long time, probably days or weeks if the script is running that long. The fact that it takes up one of the filehandles is no concern for me. But I do not have much experience with DLLs.

CODE
Func Debug($msg)

Local $i

If Not IsDeclared('DebugEnable') Then

Global $DebugEnable = 0

For $i = 1 To $CmdLine[0]

If $CmdLine[$i] = '/debug' Then $DebugEnable = 1

Next

EndIf

If $DebugEnable Then

If Not IsDeclared('DebugOut') Then

Global $DebugOut = 'KERNEL'

For $i = 1 To $CmdLine[0]

If $CmdLine[$i] = '/debugout' And ($CmdLine[0] >= ($i + 1)) Then

$DebugOut = $CmdLine[$i + 1]

EndIf

Next

EndIf

Switch $DebugOut

Case 'KERNEL'

If Not IsDeclared('KERNEL32DLL') Then

Global $KERNEL32DLL = DllOpen('kernel32.dll')

If @error Then Exit 1 + 0 * MsgBox(16, 'DEBUG', 'Failed to load kernel32.dll. Dieing.')

EndIf

DllCall($KERNEL32DLL, 'none', 'OutputDebugString', 'str', @ScriptName & '[' & @AutoItPID & ']: ' & $msg)

Case Else

If Not IsDeclared('DebugFileOut') Then

Global $DebugFileOut = FileOpen($DebugOut, 1)

If @error Then Exit 1 + 0 * MsgBox(16, 'DEBUG', 'Failed to open debug output file: ' & $DebugOut)

EndIf

FileWriteLine($DebugOut, @HOUR & @MIN & @SEC & ' ' & $msg)

EndSwitch

EndIf

EndFunc ;==>Debug

Link to comment
Share on other sites

- Global variables. I have not been able to figure out a better way. Are there persistent variables like in C that are local to the function but are kept when the function returns?

AFAIK AutoIt does not support the concept of static variables or file based scope boundaries. I would love to see static variables supported within functions and variables that could be scoped to a particular file rather than program-wide but suspect the internal workings would make support of either difficult (just a hunch).

- Keeping the kernel32.dll loaded for a long time, probably days or weeks if the script is running that long. The fact that it takes up one of the filehandles is no concern for me. But I do not have much experience with DLLs.

You need not worry about taking up resources with a long running script -- those same resources would be consumed anyways since this is a core system dll and is kept in memory by the OS most of the time anyways.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...