Jump to content

get Func name


JoGa
 Share

Recommended Posts

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

Link to comment
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 ;)
Link to comment
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.

Link to comment
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
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...