且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Tcl shell 中获取 Tcl 脚本时,如何让 Tcl 将脚本中的每个 Tcl 命令打印到 shell?

更新时间:1970-01-01 07:56:42

这是执行跟踪的工作.这些可以应用于各种 Tcl 命令,而不仅仅是过程.

That's a job for an execution trace. These can be applied to all sorts of Tcl commands, not just procedures.

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

请注意,这可能会打印出比您预期更多的内容!这是一个更复杂的版本,它只打印最外层的执行.

Note that this may print a lot more than you expect! Here's a more sophisticated version that prints only the outermost level of execution.

# 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"
    }
}}}