tcl

Redirecting output of tcl proc to file and output (like tee)


I need redirect that I found in one of my searches:

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

Where include is a TCL proc and ../RunDemoTests.tcl is a parameter to the proc. Is there a library I need to be able to use redirect or is this not general tcl?

I am working in an EDA tool environment that runs under both Windows and Linux, so I need a solution that is just TCL and does not rely on something from the OS.

I have tried numerous variations of:

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

However, this only seems to work when the command is something from the OS environment, such as:

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

Printing from this can be a significant number of lines, buffering is ok, but not collecting the whole file and then printing as the files can be long (180K lines).


Solution

  • In response to a question on comp.lang.tcl a while ago, I created a small Tcl module to provide tee-like functionality in Tcl. I have now published the code on the Tcl wiki.

    You would use it like this:

    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
    }
    

    This assumes the include RunDemoTests.tcl command produces output to stdout.