Search code examples
http-redirectpackagetcltk-toolkitstderr

TCL/TK - how does one capture stderr output from a package?


I have an application making use of some packages. Looking at their source code I see they're doing a simple puts stderr ... to dump out debug information. The problem is if you wrap the program with something like FreeWrap or the TDK, you lose access to the console; so we'd like to forward that stderr output to a file instead so we can see what's being printed.

I saw somewhere on StackOverflow that you can simply close the stderr channel, open a new one, and it should automatically replace the most recently closed channel like so:

close stderr
set out [open "outfile.txt" w]

puts stderr "hello world" # should output to the file 

Unfortunatly this doesn't work. When I try it I get the error message: can not find channel named "stderr"


Solution

  • You can override puts so that printing to stderr can be intercepted:

    set error_file [open "outfile.txt" w]
    
    rename puts __tcl__puts
    
    proc puts {args} {
        if {[llength $args] == 2 && [lindex $args 0] eq "stderr"} {
            set args [list $::error_file [lindex $args end]]
        }
        __tcl__puts {*}$args
    }