When using Tcl C++ API Tcl_Eval, if it returns TCL_ERROR, the error message can be retrieved from Tcl_GetStringResult(interp). However, when executing a bunch of tcl script, the error message doesn’t indicate which line the script fails.
Eg:
can't find package foobar
while executing
"package require foobar"
(file "./test.tn" line 5)
Tcl_GetStringResult(interp) doesn’t provide this information: (file "./test.tn" line 5). Is there a way to print out the call stack like in tcl interpreter so that I know which line the script fails?
The information you are looking for, the error info (i.e., stack trace), is in the global
errorInfovariable. This information may be retrieved withTcl_GetVaror one of its related functions. One of the best to choose isTcl_GetVar2Ex(the name being a product of a slowly evolving API) which is highly efficient:Then you use
Tcl_GetStringto extract the human-readable part as achar *(treat asconst).