Discussion:
a trivial and complicated problem in TCL
(too old to reply)
aotto1968
2024-08-12 12:45:39 UTC
Permalink
Hi,

I use
pkg_mkIndex -verbose . {*}$libs
to build a 'pkgIndex.tcl' file … GOOD
NOW I want to redirect the "stderr" output (-verbose) to stdout ... BAD

I can't figure out a "trivial" solution for this simple task IN tcl.

I know there are "exec/open" etc command there I can redirect the output of an EXTERNAL program
but this is not for a tcl INTERNAL "proc" like "pkg_mkIndex".

I know that I can start an second tclsh with the "pkg_mkIndex" and redirect the output BUT
I search for an tcl INTERNAL solution.

my CURRENT solution is way-to-over-engineerd
→ recreate the tcl "put" command and exchange "stderr" with "stdout"

=========================================
# ERASE "stderr" from tcl output
rename puts __tcl__puts
proc puts {args} {

set nxt [lindex $args 0]
if {[string index $nxt 0] eq "-"} {
set nnl $nxt
set args [lassign $args -]
} else {
set nnl ""
}

set nxt [lindex $args 0]
if {[llength [chan names $nxt]]} {
if {$nxt eq "stderr"} {
set chn stdout
} else {
set chn $nxt
}
set args [lassign $args -]
} else {
set chn ""
}

__tcl__puts {*}[concat $nnl $chn $args]
}
=======================================
chan redirect /dev/stderr /dev/stdout
which works without touching any TCL related "proc" stuff or
spawn an extra shell.


→ any idea ?
Ralf Fassel
2024-08-12 13:48:32 UTC
Permalink
* aotto1968 <***@t-online.de>
| I use
| > pkg_mkIndex -verbose . {*}$libs
| to build a 'pkgIndex.tcl' file … GOOD
| NOW I want to redirect the "stderr" output (-verbose) to stdout ... BAD
| I can't figure out a "trivial" solution for this simple task IN tcl.

Looking at the code for pkg_mkIndex, it uses 'tclLog' for the 'verbose'
output. 'tclLog' itself is defined in init.tcl, with the explicit comment

# Define a log command (which can be overwritten to log errors
# differently, specially when stderr is not available)

if {[namespace which -command tclLog] eq ""} {
proc tclLog {string} {
catch {puts s $string}
}
}

So if you redefine

proc tclLog {string} {
catch {puts stdout $string}
}

prior to invoking pkg_mkIndex, it might "just work".

Haven'd tried it, so YMMV.

HTH
R'
undroidwish
2024-08-12 13:54:19 UTC
Permalink
Post by aotto1968
...
my CURRENT solution is way-to-over-engineerd
→ recreate the tcl "put" command and exchange "stderr" with "stdout"
You could have studied the implementation of "pkg_mkIndex" and found
that it uses a command "tclLog" for logging. Then you could have
exchanged the "tclLog" command with the implementation of your choice.
If this is over or under engineering depends solely on your own
version of "tclLog".

HTH,
Christian

Loading...