运行我们的软件将在终端启动一个Tcl外壳,然后我们可以通过创建一个tcl脚本文件在这个Tcl shell中执行一系列tcl命令。但是tcl脚本文件中的tcl命令不会打印在Tcl shell上,因此,我们无法看到源tcl脚本执行了哪些命令。
所以我的问题是,当脚本是源的时候,是否有任何Tcl的配置可以打印脚本中的命令。
下面是一个示例tcl脚本
# sample.tcl, composed of 3 tcl commands:
read designfile
analyze
write output如果我们在tcl shell中以交互方式执行3个Tcl命令,结果如下:
Tcl> read designfile
Info: reading designfile
Info: size: 128k
...
...
Tcl> analyze
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
Tcl> write output
Info: analyzed data been written into file "output"如果我们在tcl shell中获取这个Tcl sript,结果将是:
Tcl> source sample.tcl
Info: reading designfile
Info: size: 128k
...
...
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
Info: analyzed data been written into file "output"我们期望当我们获取脚本时,结果将如下所示。
Tcl> source sample.tcl
> read designfile
Info: reading designfile
Info: size: 128k
...
...
> analyze
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
> write output
Info: analyzed data been written into file "output"发布于 2020-09-10 15:04:28
这是执行追踪的任务。这些可以应用于各种Tcl命令,而不仅仅是过程。
trace add execution source enterstep {apply {{cmd op} {
# Trim multiline commands down a bit; I find this helps me at least!
regsub {\n.*} $cmd ... cmd
# Print what's being run
puts "EXECUTE: $cmd"
}}}
source thefile.tcl请注意,这可能会打印出比您预期的多得多!这里有一个更复杂的版本,它只输出最外层的执行级别。
# Called before [source] starts running
trace add execution source enter {apply {args {
global SOURCE_LEVEL
if {![info exists SOURCE_LEVEL]} {
set SOURCE_LEVEL [info level]
}
}}}
# Called after [source] finishes running
trace add execution source leave {apply {args {
global SOURCE_LEVEL
if {$SOURCE_LEVEL == [info level]} {
unset SOURCE_LEVEL
}
}}}
# Called for every command called while [source] is running
trace add execution source enterstep {apply {{cmd op} {
global SOURCE_LEVEL
if {$SOURCE_LEVEL == [info level]} {
regsub {\n.*} $cmd "..." cmd
puts "EXECUTE: $cmd"
}
}}}发布于 2020-09-11 08:46:11
非常感谢Donal!根据他的建议,最后我得到了如下跟踪命令,以满足我的要求。
proc print {args} {
set cmd [lindex $args 0]
puts stdout "$> $cmd"
}
trace add execution source enterstep printhttps://stackoverflow.com/questions/63832193
复制相似问题