: <i>how to log Tcl commands that are executed via the source command using the Tcl C API</i> plus 5 other changes
by
.
D 2025-07-24T10:29:12.351
J closedate 2460880.93694851
J closer sebres
J icomment At\stop\slevel\sin\ssource,\syou\scan\suse\s`[info\sscript]`\sor\sat\sC-level\s`((Interp*)interp)->scriptFile`.\r\n\r\nEverywhere\sin\sscript\s(not\sat\stop\slevel\sonly,\sbut\salso\sin\sproc\sor\snamespace),\syou\scan\sget\sa\sdict\swith\sthe\sfile\sand\sline\swith\s`[info\sframe\s0]`\sor\sat\sC-level\s`TclInfoFrame`\s(internal\sonly).\r\n\r\nYou\scan\stry\sthis\sto\ssee\show\sit\sworks:\r\n\r\n```\r\n\s\sputs\s[info\sscript]\r\n\s\sputs\s[info\sframe\s0]\r\n\s\sproc\stest\s{}\s{\r\n\s\s\s\sputs\s[info\sframe\s0]\r\n\s\s}\r\n\s\stest\r\n\s\snamespace\seval\stest\s{\r\n\s\s\s\sputs\s[info\sframe\s0]\r\n\s\s}\r\n\s\seval\s[concat\sapply\s{{{}\s{puts\s[info\sframe\s0];\sputs\s[info\sframe\s1]}}}]\r\n\s\s{*}[list\sif\s1\s{puts\s[info\sframe\s0];\sputs\s[info\sframe\s1]}]\r\n\s\safter\s0\s{puts\s[info\sframe\s0];\sputs\s[info\sframe\s1]}\r\n\s\supdate\r\n```\r\n\r\nCalling\sfrom\sC-level\sof\scommand,\syou\scan\sobtain\sthe\sframe\sinfo\seither\swith\sTclInfoFrame\s(if\syou\shave\sTcls\sinternals\sand\sit\sis\sa\sbit\scomplex,\ssee\sTcls\ssource\scode),<br/>\r\nor\susing\s`objProc()`\sof\sCommand\s`::tcl::info::frame`\scalled\swith\sint-object\s0,\s1,\sor\swhatever\slevel\syou\sneed.\r\n\r\nOr\swith\ssomething\slike\sthis:\r\n\r\n```\r\n\s\sif\s(Tcl_Eval(interp,\s"info\sframe\s1")\s!=\sTCL_OK)\sreturn\sTCL_ERROR;\r\n\s\sTcl_Obj\s*res\s=\sTcl_GetObjResult(interp);\r\n```\r\nAnd\sthen\suse\s`Tcl_DictObjGet`\sto\sobtain\s`file`\sand\s`line`\sfrom\sresult\sdict.<br/>\r\nNote\sthat\sit\suses\s1\sas\slevel\sto\sobtain\sframe\sfrom\sparent\s(caller),\sbecause\sTcl_Eval\swould\salso\sinsert\sa\sframe\sduring\sits\sexecution.\r\n\r\nAlso\snote\sthat\sthis\sframe\sinfo\smay\snot\scontain\sthe\ssource\sfile\sand\sline\sinformation,\sif\sthe\scommand\sis\sinvoked\snot\sdirectly\sfrom\ssource,\sbut\sasynchronously\sas\sevents\sor\sfrom\scode\sdynamically\sbuilt\sand\sevaluated\shereafter\s(see\slast\s3\sexamples\sabove),\sso\syou\smay\sneed\sto\straverse\sover\supper\slevels\s(e.\sg.\s`info\sframe\s2`\setc)\still\syou'd\sfind\sthe\scaller\sframe\scontaining\sthe\sinfo.\r\nHowever\sthere\sare\scases\swhere\sobtaining\sof\sorigin\smay\sbe\simpossible\sat\sall,\sbecause\snot\severy\s"evaluable"\stcl-object\scontains\sline\sinformation\swith\sits\sorigin\s(since\sthe\sTcl\sis\sJIT\scompiler,\sand\sTEBC\scompilation,\sNRE\strampoline,\setc\smay\smake\sit\stoo\sdifficult\sor\salmost\simpossible\sfor\scertain\sconstellations).
J login sebres
J mimetype text/x-markdown
J status Closed
K 008773b7a3cdd530a184b2e8902d0679e246de74
U sebres
Z b63bd33466a35e8e9662c939f6a772e2