In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update. Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update. Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update. >> Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
Yes, I am not completely happy about it.
The problem with your solution is that even when there is no output
the file is created, or the file-stamp changed.
(Both is not what I want.)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
I don't think it makes sense to spawn an external command for each update. Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
In article <87wnkgl5th.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update. >>> Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
Yes, I am not completely happy about it.
The problem with your solution is that even when there is no output
the file is created, or the file-stamp changed.
(Both is not what I want.)
Well, that is true with "tee" as well, but you could do something like
puts $output
set f [open "textFile.txt" a]
# fconfigure if needed
puts $f $output
close $f
That would be more expensive, but still less so than spawning off echo,
and wouldn't create the file unless the write were going to be done.
On 07/12/2021 15:39, Ted Nolan <tednolan> wrote:
I don't think it makes sense to spawn an external command for eachA more sophisticated way would be to use a stacked channel. Then you can
update.
Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
do this:
package require tee
tee stdout textFile.txt
puts $output
And the output will go to both stdout and textFile.txt. If you want to
return to normal operation, just remove the transformation:
chan pop stdout
To accomplish this, put the following code in a file called tee-1.0.tm
that is somewhere in your tcl::tm::path:
namespace eval tee {
variable methods {initialize finalize write}
namespace ensemble create -command transchan -parameters fd \
-subcommands $methods
namespace export tee
}
proc tee::tee {chan file} {
set fd [open $file w]
chan push $chan [list [namespace which transchan] $fd]
}
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
}
namespace import tee::tee
Delaying opening the file until something is actually written to stdout
is left as an exercise for the reader.
Schelte.
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <87wnkgl5th.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update. >>>> Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
Yes, I am not completely happy about it.
The problem with your solution is that even when there is no output
the file is created, or the file-stamp changed.
(Both is not what I want.)
Well, that is true with "tee" as well, but you could do something like
puts $output
set f [open "textFile.txt" a]
# fconfigure if needed
puts $f $output
close $f
That would be more expensive, but still less so than spawning off echo,
and wouldn't create the file unless the write were going to be done.
I was already thinking in those lines.
I created the following script:
proc testOpen {} {
set output "Output I want"
puts ${output}
set fp [open testFile.txt w]
puts $fp ${output}
close $fp
}
proc testTee {} {
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
}
for {set i 0} {$i < 10000} {incr i} {
file delete textFile.txt
testTee
}
This takes about 13.7 user seconds and 17.9 system seconds.
When I change the testTee to testOpen that becomes 0.4 and 1.5.
The difference is a lot bigger as I expected.
An optimisation could be that the first time I need to do output I
open the file and only close it at the end of the program (when it is >opened).
In article <87sfv4kvbr.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <87wnkgl5th.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
ted@loft.tnolan.com (Ted Nolan <tednolan>) writes:
In article <871r2omse0.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
In bash I would do something like:
echo "Output I want" | tee logfile.log
What is the best way to do this in tcl?
At the moment I do:
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
I don't think it makes sense to spawn an external command for each update.
Best to do it all from Tcl.
set f [open "textFile.txt" w]
# fconfigure if needed
...
...
...
puts $output
puts $f $output
...
...
...
close $f
Yes, I am not completely happy about it.
The problem with your solution is that even when there is no output
the file is created, or the file-stamp changed.
(Both is not what I want.)
Well, that is true with "tee" as well, but you could do something like
puts $output
set f [open "textFile.txt" a]
# fconfigure if needed
puts $f $output
close $f
That would be more expensive, but still less so than spawning off echo,
and wouldn't create the file unless the write were going to be done.
I was already thinking in those lines.
I created the following script:
proc testOpen {} {
set output "Output I want"
puts ${output}
set fp [open testFile.txt w]
puts $fp ${output}
close $fp
}
proc testTee {} {
set output "Output I want"
puts ${output}
exec echo ${output} >> textFile.txt
}
for {set i 0} {$i < 10000} {incr i} {
file delete textFile.txt
testTee
}
This takes about 13.7 user seconds and 17.9 system seconds.
When I change the testTee to testOpen that becomes 0.4 and 1.5.
The difference is a lot bigger as I expected.
An optimisation could be that the first time I need to do output I
open the file and only close it at the end of the program (when it is >>opened).
Well, I'm not too surprised as you are creating a lot of processes
with exec.
One thing I would note, which doesn't really matter for this test,
but the two procs don't do exactly the same thing. "testOpen" is going
to zero out the file each time since it is opened with "w", and proc "testTee" is going to append to it.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 28:15:23 |
Calls: | 6,448 |
Files: | 12,050 |
Messages: | 5,254,514 |