JoGa 0 Posted September 30, 2010 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
water 2,386 Posted September 30, 2010 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 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
Mat 376 Posted September 30, 2010 I'd do it like this: expandcollapse popup#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. AutoIt Project Listing Share this post Link to post Share on other sites
JoGa 0 Posted September 30, 2010 I'd do it like this: expandcollapse popup#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
Tvern 11 Posted September 30, 2010 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: expandcollapse popupGlobal $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
Mat 376 Posted September 30, 2010 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... AutoIt Project Listing Share this post Link to post Share on other sites
Tvern 11 Posted September 30, 2010 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: expandcollapse popupGlobal $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