将 tcl proc 的输出重定向到文件并输出(如 tee)

问题描述 投票:0回答:2

我需要在我的一次搜索中找到的

redirect

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

其中

include
是 TCL
proc
../RunDemoTests.tcl
是 proc 的参数。是否有一个我需要能够使用重定向的库,或者这不是一般的 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 行)。

tcl
2个回答
3
投票

不久前在回答 comp.lang.tcl 上的问题时,我创建了一个小型 Tcl 模块来在 Tcl 中提供类似 tee 的功能。我现在已经在 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


0
投票

我也需要解决方案。我发现你可以为此开两次球。

close stdout
set fd [open |[list tee ${rundir}/REPORTS/$rptn.log | tee /dev/tty ] w]
© www.soinside.com 2019 - 2024. All rights reserved.