将tcl过程的输出重定向到文件和输出(如tee)



我需要在我的一个搜索中找到的redirect:

redirect -tee LogFile.log {include ../RunDemoTests.tcl}

其中include是一个TCLproc../RunDemoTests.tcl是一个参数的进程。是否有一个库,我需要能够使用重定向,还是这不是一般的TCL ?

我在一个同时在Windows和Linux下运行的EDA工具环境中工作,所以我需要一个仅仅是TCL的解决方案,而不依赖于来自操作系统的东西。

我试过很多次:

set ch [open |[list include "../OsvvmLibraries/UART/RunDemoTests.tcl"] r]
set lf [open build.log w]
puts "Starting"
puts $lf "Starting"
while {[gets $ch line]>=0} {
puts $line
puts $lf $line
}
close $lf

然而,这似乎只在来自操作系统环境的命令时才有效,例如:

set ch [open |[list cat ../tests.pro] r]

从这里打印可以是相当多的行,缓冲是可以的,但不能收集整个文件然后打印,因为文件可能很长(180K行)。

为了回答前一段时间关于comp.lang.tcl的问题,我创建了一个小的Tcl模块来提供类似于Tcl的功能。我现在已经在Tcl wiki上发布了代码。

你可以这样使用:

package require tee
tee stdout build.log
try {
puts "Starting"
include ../OsvvmLibraries/UART/RunDemoTests.tcl
} finally {
# Make sure the tee filter is always popped from the filter stack
chan pop stdout
}

假设include RunDemoTests.tcl命令输出到stdout

可以选两次。

close stdout
set fd [open |[list tee my.log | tee /dev/tty ] w]
puts "test"; # both happend in my.log and stdout

最新更新