hoppfrosch
11/27/2013 - 7:45 AM

Get the current AHK-callstack #ahk #script #function #debug

Get the current AHK-callstack #ahk #script #function #debug

foo1()
return

foo1()
{
  foo2()
}
foo2()
{
  foo3()
}
foo3()
{
  foo4()
}
foo4()
{
  foo5()
}
foo5()
{
  foo6()
}
foo6()
{
  foo7()
}
foo7()
{
  throw Exception( "`n" CallStack() )
}


FileReadLine( file, line )
{
  FileReadLine, data,% file,% line
  return data
}

#include CallStack.ahk
CallStack(deepness = 5, printLines = 1)
{
	loop % deepness
	{
		lvl := -1 - deepness + A_Index
		oEx := Exception("", lvl)
		oExPrev := Exception("", lvl - 1)
		FileReadLine, line, % oEx.file, % oEx.line
		if(oEx.What = lvl)
			continue
		stack .= (stack ? "`n" : "") "File '" oEx.file "', Line " oEx.line (oExPrev.What = lvl-1 ? "" : ", in " oExPrev.What) (printLines ? ":`n" line : "") "`n"
	}
	return stack
}