This function returns an array of objects representing a stack trace entry. Each object has the following fields:
- offset - negative offset from the top of the call stack.
- file - the script file (I'm not sure if this is equivalent to A_LineFile or if it's the file that contains the function, I suspect the former)
- line - the line number at which the function is called
- caller - function name
If actual is true, the actual stack trace is returned which includes Traceback() itself
Example usage:
Code: Select all
#Include Traceback.ahk
f()
return
a() {
b()
}
b() {
c()
}
c() {
out := "Stack trace:"
for i, info in Traceback()
{
out .= Format("
(LTrim Join`r`n
`r`n
Offset: {}
File: {}
Line: {}
Caller: {}
)", info.offset, info.file, info.line, info.caller)
}
ListVars
WinWait ahk_id %A_ScriptHwnd%
ControlSetText Edit1, %out%
WinWaitClose
}
f() {
a()
}
Code: Select all
Traceback(actual:=false)
{
r := [], i := 0, n := actual ? 0 : A_AhkVersion<"2" ? 1 : 2
Loop
{
e := Exception(".", offset := -(A_Index + n))
if (e.What == offset)
break
r[++i] := { "file": e.file, "line": e.Line, "caller": e.What, "offset": offset + n }
}
return r
}