Sign in to follow this  
Followers 0
JoGa

get Func name

7 posts in this topic

Is there a way to get the name of the function autoit is currently executing?

Or even better: in a Func, can I get the name of the calling func?

Jo

Share this post


Link to post
Share on other sites



No, as far as I know.

But you can define two global variables that hold the name of the current and calling function and set them accordingly.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I'd do it like this:

#include <Array.au3>

Global $aStack[1] = [0]

_MyFun()


Func _MyFun()
    _EnterFunc("_MyFun")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    _MyFun2()

    Return _Return()
EndFunc

Func _MyFun2()
    _EnterFunc("_MyFun2")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    Return _Return()
EndFunc

Func _GetCurrentFunc()
    If $aStack[0] = 0 Then Return ""

    Return $aStack[$aStack[0]]
EndFunc

Func _GetCallingFunc()
    If $aStack[0] <= 1 Then Return ""

    Return $aStack[$aStack[0] - 1]
EndFunc

Func _EnterFunc($sName)
    _ArrayAdd($aStack, $sName)
    $aStack[0] += 1
EndFunc

Func _Return($sRet = 0, $iExt = 0, $iErr = 0)
    _ArrayDelete($aStack, $aStack[0])
    $aStack[0] -= 1

    Return SetError($iErr, $iExt, $sRet)
EndFunc

Whenever you enter a function that is to be included in the stack, use _EnterFunc() with the name of the function, and whenever you leave call the _Return function.

_ArrayDisplay($aStack) will show you the whole stack.

Share this post


Link to post
Share on other sites

I'd do it like this:

#include <Array.au3>

Global $aStack[1] = [0]

_MyFun()


Func _MyFun()
    _EnterFunc("_MyFun")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    _MyFun2()

    Return _Return()
EndFunc

Func _MyFun2()
    _EnterFunc("_MyFun2")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    Return _Return()
EndFunc

Func _GetCurrentFunc()
    If $aStack[0] = 0 Then Return ""

    Return $aStack[$aStack[0]]
EndFunc

Func _GetCallingFunc()
    If $aStack[0] <= 1 Then Return ""

    Return $aStack[$aStack[0] - 1]
EndFunc

Func _EnterFunc($sName)
    _ArrayAdd($aStack, $sName)
    $aStack[0] += 1
EndFunc

Func _Return($sRet = 0, $iExt = 0, $iErr = 0)
    _ArrayDelete($aStack, $aStack[0])
    $aStack[0] -= 1

    Return SetError($iErr, $iExt, $sRet)
EndFunc

Whenever you enter a function that is to be included in the stack, use _EnterFunc() with the name of the function, and whenever you leave call the _Return function.

_ArrayDisplay($aStack) will show you the whole stack.

Thanks for your suggestion ;)

Share this post


Link to post
Share on other sites

Keep in mind that ArrayAdd and ArrayDelete use ReDim. Redim is fairly slow, so if you are doing lots of ReDims per second it could slow down the script by a noticable amount.

As an alternative you could make the array fixed size. According to the helpfile the limit for recursive function calls is 5100 levels, so 5101 indices should be enough. It's a little wasted memory, but if you are calling allot of functions per second it might make a difference. This all depends on how often logged functions are called though.

Here is a modified example with fixed array size:

Global $aStack[5101]

_MyFun()

Func _MyFun()
    _EnterFunc("_MyFun")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    _MyFun2()

    Return _Return()
EndFunc

Func _MyFun2()
    _EnterFunc("_MyFun2")

    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())

    Return _Return()
EndFunc

Func _GetCurrentFunc()
    If $aStack[0] = 0 Then Return ""

    Return $aStack[$aStack[0]]
EndFunc

Func _GetCallingFunc()
    If $aStack[0] <= 1 Then Return ""

    Return $aStack[$aStack[0] - 1]
EndFunc

Func _EnterFunc($sName)
    $aStack[0] += 1
    $aStack[$aStack[0]] = $sName
EndFunc

Func _Return($sRet = 0, $iExt = 0, $iErr = 0)
    $aStack[$aStack[0]] = ""
    $aStack[0] -= 1
    Return SetError($iErr, $iExt, $sRet)
EndFunc

If you are just trying to trace the source of an error you can use "Trace: Add Func Trace Lines" from the tools menu in SciTE4AutoIt3, which will print funtion calls to the console.

Share this post


Link to post
Share on other sites

I'd place a smaller limit I think, 5101 seems a bit ott.

I might look at Trace: Add Func Trace Lines and see if I can get it to add all the stack bits like this, that would be interesting, or you provide the information to an external debugger...

Share this post


Link to post
Share on other sites

I just used 5101 because that's the limit for autoit recursion, but any sane scripter wouldn't go near that limit.

How about this as a hybrid solution? You keep the speed as long as you don't exceed a predicted maximum amount of recursion and you don't have a big nearly empty array in use:

Global $iRecuse = 10 ;expected maximum recursion
Global $aStack[$iRecuse + 1]

_MyFun()

Func _MyFun()
    _EnterFunc("_MyFun")
    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())
    _MyFun2()
    Return _Return()
EndFunc

Func _MyFun2()
    _EnterFunc("_MyFun2")
    MsgBox(0, _GetCurrentFunc(), "Called by: " & _GetCallingFunc())
    Return _Return()
EndFunc

Func _GetCurrentFunc()
    If $aStack[0] = 0 Then Return ""
    Return $aStack[$aStack[0]]
EndFunc

Func _GetCallingFunc()
    If $aStack[0] <= 1 Then Return ""
    Return $aStack[$aStack[0] - 1]
EndFunc

Func _EnterFunc($sName)
    $aStack[0] += 1
    If $aStack[0] > $iRecuse Then ReDim $aStack[$aStack[0]+1]
    $aStack[$aStack[0]] = $sName
EndFunc

Func _Return($sRet = 0, $iExt = 0, $iErr = 0)
    If $aStack[0] > $iRecuse Then
        ReDim $aStack[$aStack[0]]
    Else
        $aStack[$aStack[0]] = ""
    EndIf
    $aStack[0] -= 1
    Return SetError($iErr, $iExt, $sRet)
EndFunc

Share this post


Link to post
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
Sign in to follow this  
Followers 0