Posted to tcl by kap at Tue Oct 28 00:30:45 GMT 2014view raw

  1. oo::class create redirect {
  2. variable File
  3. constructor {filename} {
  4. set File [open $filename w]
  5. }
  6. method initialize {handle mode} {
  7. if {$mode ne "write"} {error "can't handle reading"}
  8. return {finalize initialize write}
  9. }
  10. method finalize {handle} {
  11. }
  12. method write {handle data} {
  13. puts $File $data
  14. }
  15. destructor {
  16. catch {close $File}
  17. }
  18. }
  19.  
  20. # Redirect stdout to file
  21. chan push stdout [redirect new foo.txt]
  22. chan configure stdout -buffering none
  23.  
  24. # Test
  25. puts 1
  26. puts 2
  27. puts 3
  28. puts -nonewline a
  29. puts -nonewline b
  30. puts -nonewline c
  31.