sbron: In unix/linux there is a standard command named tee , that duplicates its input to both standard output and a file. Inspired on that concept, I came up with some code to add similar functionality to Tcl.
The module adds a tee command to Tcl. It has three subcommands:
The commands return a handle to the secondary output file to allow the caller to configure things like encoding, buffering, and baud rate.
To stop copying the data to file and return chan to normal, use: chan pop chan. This closes the secondary output file. (Also in the case of tee channel chan fd!)
Usage example:
package require tee tee stdout /tmp/sessionlog.txt try { puts "Hello, World!" } finally { chan pop stdout }
Implementation
Put the following code in a file called tee-1.0.tm and place it somewhere in the tcl::tm path:
namespace eval tee { variable methods {initialize finalize write} namespace ensemble create -subcommands {replace append channel} \ -unknown [namespace current]::default namespace ensemble create -command transchan -parameters fd \ -subcommands $methods } proc tee::default {command subcommand args} { return [list $command replace $subcommand] } proc tee::channel {chan fd} { chan push $chan [list [namespace which transchan] $fd] return $fd } proc tee::replace {chan file} { return [channel $chan [open $file w]] } proc tee::append {chan file} { return [channel $chan [open $file a]] } proc tee::initialize {fd handle mode} { variable methods return $methods } proc tee::finalize {fd handle} { close $fd } proc tee::write {fd handle buffer} { puts -nonewline $fd $buffer return $buffer }