Jump to content

Nice little function for testing and debugging.


AcidCorps
 Share

Recommended Posts

Not the fanciest thing in the world but it's very helpful while I'm debugging some of my programs.

Displays wither the input variable was an Array or Variable, Tells you the line number as well as variable name, and displays contents either in msgbox or _arraydisplay

#include
#include

$Example = 'Hello World'
Test($Example)

Dim $ExampleA[3]
$ExampleA[0] = 2
$ExampleA[1] = 'Hello'
$ExampleA[2] = 'World'

Test($ExampleA)


Func Test($iVar = '', $iLine = @ScriptLineNumber)
If IsArray($iVar) Then
$xBetween = _StringBetween(FileReadLine(@ScriptFullPath, $iLine), 'Test(', ')')
$xBetween = $xBetween[0]
_ArrayDisplay($iVar, 'Array: ' & $xBetween & ' Line: ' & $iLine)
Else
$xBetween = _StringBetween(FileReadLine(@ScriptFullPath, $iLine), 'Test(', ')')
$xBetween = $xBetween[0]
MsgBox(0, 'Variable: ' & $xBetween & ' Line: ' & $iLine, $iVar)
EndIf
EndFunc ;==>Test
Link to comment
Share on other sites

$xBetween = _StringBetween(FileReadLine(@ScriptFullPath, $iLine), 'Test(', ')')
$xBetween = $xBetween[0]

Nice one.

VarGetType() could be useful to add. Sometimes you can go crazy getting unexpected results while all the time you're passing around the wrong variable types.

I don't know if you work with DLLStructs a lot, I know I do lately :) so here you go:

#include <String.au3>
#include <Array.au3>

$Example = 'Hello World'
Test($Example)

Dim $ExampleA[3]
$ExampleA[0] = 2
$ExampleA[1] = 'Hello'
$ExampleA[2] = 'World'
Test($ExampleA)

$tagEXAMPLE = DllStructCreate('char[128]')
DllStructSetData($tagEXAMPLE, 1, 'With AutoIt I will rule the world! HahahaHAHA!')
Test($tagEXAMPLE)

Func Test($iVar = '', $iLine = @ScriptLineNumber)
    $xBetween = _StringBetween(FileReadLine(@ScriptFullPath, $iLine), 'Test(', ')')
    $xBetween = $xBetween[0]
    If IsArray($iVar) Then
        _ArrayDisplay($iVar, 'Array: ' & $xBetween & ' Line: ' & $iLine)
    ElseIf 'DLLStruct' = VarGetType($iVar) Then
        MsgBox(0, 'Variable: ' & $xBetween & ' Line: ' & $iLine, VarGetType($iVar) & @CRLF & 'size:' & DllStructGetSize($iVar) & @CRLF & 'data:' & DllStructGetData($iVar, 1))
    Else
        MsgBox(0, 'Variable: ' & $xBetween & ' Line: ' & $iLine, VarGetType($iVar) & ':' & @CRLF & $iVar)
    EndIf
EndFunc ;==>Test

[edit] of course, your mileage may vary. [/edit]

Edited by dany

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

Heh yea that stringbetween was a nice add on, started off as a simple display array or string type function but I like where it's heading, also nice to be able to add a simple Test($Variable) and see everything you need to know about it.

Also added your addition to my function list now, thanks for the idea I don't use DLLStructs often myself so probably wouldn't have thought of that

Link to comment
Share on other sites

...also nice to be able to add a simple Test($Variable) and see everything you need to know about it.

Indeed, very useful to find out where, how and why things go wrong. I got the DLLStruct idea when I couldn't get WM_COPYDATA to work. I didn't find out why until I dumped the DLLStruct info on screen and saw I was typejuggling and that went south halfway through the function.

Mine's called _VarDump() by the way and apart from that _StringBetween part (nifty) pretty much the same as yours.

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

After adding your _StringBetween() trick here's what my _VarDump() looks like:

#include <String.au3> ;  _StringBetween
#include <Array.au3> ;  _ArrayDisplay
#include <Misc.au3> ; _Iif

Local $aArray[2] = [1, 'Example']
Local $bBinary = StringToBinary('Hello Mars')
Local $fBoolean = False
Local $hWnd = WinGetHandle('[CLASS:Shell_TrayWnd]')
Local $iInt = 1
Local $nFloat = 2.1
Local $sString = 'Some text'
Local $vKeyword = Default
Local $tagDLLSTRUCT = DllStructCreate('char[128]')
DllStructSetData($tagDLLSTRUCT, 1, 'With AutoIt I will rule the world! HahahaHAHA!')
Local $pPointer = DllStructGetPtr($tagDLLSTRUCT)

_VarDump($aArray)
_VarDump($bBinary)
_VarDump($fBoolean)
_VarDump($hWnd)
_VarDump($iInt)
_VarDump($nFloat)
_VarDump($sString)
_VarDump($vKeyword)
_VarDump($tagDLLSTRUCT)
_VarDump($pPointer)

#cs
Name:               _VarDump
Description:        Dumps a variable type, script line number and value on screen.
Syntax:             _VarDump($vVar)
Parameters:         $vVar       - Mixed: Any variable type.
Return values:      String: Variable type as in VarGetType.
Author:             dany
Modified:           AcidCorps
Example:            Yes
#ce
Func _VarDump($vVar, $iLineNo = @ScriptLineNumber)
    Local $sVarType = VarGetType($vVar)
    Local $sVarName = FileReadLine(@ScriptFullPath, $iLineNo)
    $sVarName = _StringBetween($sVarName, '_VarDump(', ')') ; Idea by AcidCorps
    Local $sTitle = $sVarType & ' (line ' & $iLineNo & '): ' & $sVarName[0]
    Switch $sVarType
        Case 'Array'
            _ArrayDisplay($vVar, $sTitle)
        Case 'DLLStruct'
            MsgBox(0, $sTitle, 'Size: ' & DllStructGetSize($vVar) & @CRLF & 'Data: ' & DllStructGetData($vVar, 1))
        Case 'Binary'
            MsgBox(0, $sTitle, 'Size: ' & BinaryLen($vVar) & @CRLF & 'Hex: ' & String($vVar) & @CRLF & 'Data: ' & BinaryToString($vVar))
        Case 'Ptr'
            MsgBox(0, _Iif(IsHWnd($vVar), 'HWnd', 'Ptr') & ' (line ' & $iLineNo & '): ' & $sVarName[0], String($vVar))
        Case Else
            MsgBox(0, $sTitle, String($vVar))
    EndSwitch
    Return _Iif(IsHWnd($vVar), 'HWnd', $sVarType)
EndFunc

Although I guess we're just reinventing Debug.au3... :)

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

  • 2 years later...

Came across this topic today, figured I'd upload the current version. I added a couple things such as being able to copy to the clipboard. I have also distributed it to a couple friends so it's in more of a UDF form now.

Examples:

$SimpleExample = 'Hello World'
Test($SimpleExample)

Dim $ArrayExample[4]
$ArrayExample[0] = 3
$ArrayExample[1] = 'Hello'
$ArrayExample[2] = 'Array'
$ArrayExample[3] = 'World'
Test($ArrayExample)

$DllStructExample = DllStructCreate('char[128]')
DllStructSetData($DllStructExample, 1, 'Hello DllStruct World!')
Test($DllStructExample)

$BinaryExample = StringToBinary('Hello Binary World')
Test($BinaryExample)

$PtrExample = DllStructCreate('char[128]')
DllStructSetData($PtrExample, 1, 'Hello Pointer World')
$PtrExampleDisplay = DllStructGetPtr($PtrExample)
Test($PtrExampleDisplay)

UDF:

#include-once
#include <String.au3>
#include <Array.au3>

; ===============================================================================================================================
; Name...........: Test
; Description ...: Display variable with data
; Syntax.........: Test( $iVar, $iClip, $iLine )
; Parameters ....: $iVar - Variable to display.
;                  $iClip - Copy data to clipboard
;                      If iVar is Array: $iClip = Element to copy to clipboard.
;                      If $iVar is anything else: 1 = Copy $iVar to clipboard.
;                  $iLine - ##Internal Use Only### do not change this paramater, it is used to find $iVar's name
; Return values .: Return from _ArrayDisplay or MsgBox
; Author ........: LacWare
; Example .......: Test($Variable, 1)
; ===============================================================================================================================
Func Test($iVar = '', $iClip = '', $iLine = @ScriptLineNumber)
    Local $xTitle, $xOpen, $xLine, $xType, $xMsg

    ;Reading script file
    $xOpen = FileOpen(@ScriptFullPath)
    $xLine = FileReadLine($xOpen, $iLine)
    FileClose($xOpen)

    ;Getting variable name
    $xTitle = _StringBetween($xLine, 'Test(', ')')
    If Not IsArray($xTitle) Then $xTitle = _StringBetween($xLine, 'Test(', ',')

    ;Creating window title
    $xTitle = 'Var: ' & $xTitle[0] & ' Line: ' & $iLine

    ;Getting variable type
    $xType = VarGetType($iVar)

    ;Displaying variable
    If IsArray($iVar) Then
        ;If variable is an array display in _ArrayDisplay
        If $iClip <> '' Then ClipPut($iVar[$iClip])
        Return _ArrayDisplay($iVar, $xTitle)
    ElseIf $xType = 'DLLStruct' Then
        ;If variable DLLStruct generate data and display
        $xTitle &= ' Type: ' & $xType
        $xMsg = 'Size: ' & DllStructGetSize($iVar) & @CRLF & @CRLF & 'Data: ' & DllStructGetData($iVar, 1)
        If $iClip = 1 Then ClipPut(DllStructGetData($iVar, 1))
        Return MsgBox(64, $xTitle, $xMsg)
    ElseIf $xType = 'Binary' Then
        ;If variable Binary generate data and display
        $xTitle &= ' Type: ' & $xType
        $xMsg = 'Size: ' & BinaryLen($iVar0) & @CRLF & @CRLF & 'Hex: ' & String($iVar) & @CRLF & @CRLF & 'Data: ' & BinaryToString($iVar)
        If $iClip = 1 Then ClipPut(BinaryToString($iVar))
        Return MsgBox(64, $xTitle, $xMsg)
    ElseIf $xType = 'Ptr' Then
        ;If variable Ptr generate data and display
        If IsHWnd($iVar) Then
            $xTitle = 'HWnd: ' & $xTitle
        Else
            $xTitle = 'Ptr: ' & $xTitle
        EndIf
        If $iClip = 1 Then ClipPut(String($iVar))
        Return MsgBox(64, $xTitle, String($iVar))
    Else
        ;If variable is anything else display in MsgBox
        $xTitle &= ' Type: ' & $xType
        If $iClip = 1 Then ClipPut($iVar)
        MsgBox(64, $xTitle, String($iVar))
    EndIf
EndFunc   ;==>Test

Thanks dany for the extra features, been using them for a couple years now.

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...