Tcl_Eval在出现错误时返回调用堆栈 使用TCL C++ ++ API >强> Tcl→Evr>强>,如果返回 Tcl错误> /St>,可以从 >检索错误消息。但是,当执行一组tcl脚本时,错误消息不会指示脚本失败的行

Tcl_Eval在出现错误时返回调用堆栈 使用TCL C++ ++ API >强> Tcl→Evr>强>,如果返回 Tcl错误> /St>,可以从 >检索错误消息。但是,当执行一组tcl脚本时,错误消息不会指示脚本失败的行,c++,tcl,tcl-api,C++,Tcl,Tcl Api,例如: Tcl\u GetStringResult(interp)不提供以下信息:(文件“/test.tn”第5行)。是否有一种方法可以像在tcl解释器中那样打印出调用堆栈,以便我知道脚本失败的是哪一行?我使用以下代码从解释器中提取并报告可用的errorinfo Tcl_Obj *top_interpInfoName ; Tcl_Obj *top_interpInfo ; top_interpInfoName = Tcl_NewStringObj("errorInfo", -1) ;

例如:


Tcl\u GetStringResult(interp)
不提供以下信息:
(文件“/test.tn”第5行)
。是否有一种方法可以像在tcl解释器中那样打印出调用堆栈,以便我知道脚本失败的是哪一行?

我使用以下代码从解释器中提取并报告可用的errorinfo

Tcl_Obj *top_interpInfoName ;
Tcl_Obj *top_interpInfo ;

top_interpInfoName = Tcl_NewStringObj("errorInfo", -1) ;
    Tcl_IncrRefCount(top_interpInfoName) ;
    top_interpInfo =  Tcl_ObjGetVar2(tcl_interp,
                                     top_interpInfoName,
                                     NULL,
                                     TCL_LEAVE_ERR_MSG) ;
    Tcl_IncrRefCount(top_interpInfo) ;
    ERR_REP2("ERROR: %s", Tcl_GetString(top_interpInfo)) ;
    Tcl_DecrRefCount(top_interpInfoName) ;
    Tcl_DecrRefCount(top_interpInfo) ;

ERR_REP2是我的错误报告宏,您需要用自己的宏替换它。

您要查找的信息,即错误信息(即堆栈跟踪),位于全局
errorInfo
变量中。此信息可通过或其相关功能之一检索。最好的选择之一是
Tcl_GetVar2Ex
(名称是缓慢发展的API的产物),它非常高效:

Tcl_Obj *infoObj = Tcl_GetVar2Ex(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY);

然后您使用
Tcl\u GetString
将人类可读部分提取为
char*
(视为
const
)。

是否可以
Tcl\u GetErrorLine
作为您要查找的内容?@Tony:这将从上述消息中返回
5
(嗯,可能,它非常依赖于上下文)。
Tcl_Obj *infoObj = Tcl_GetVar2Ex(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY);