[GRASS-dev] Merging stdout and stderr

Glynn Clements glynn at gclements.plus.com
Tue Nov 14 06:54:06 EST 2006


Paul Kelly wrote:

> The GRASS console thing (I guess that's where the name gronsole comes 
> from?) which runs commands via the Tcl/Tk "open" command and captures 
> their stdout and stderr, displaying some output as-is to the gronsole 
> window and catching certain things like percentage complete and warnings 
> and displaying them differently is actually quite cool.
> 
> I remember discussion on the list about how stdout and stderr had to be 
> merged because Tcl/Tk has a bug/feature in that it treats anything written 
> to stderr as an error indicator for the called program. On Unix this 
> merging is done as follows:
> set cmd [concat | $cmd 2>@ stdout]
> but the "2>@ stdout" syntax will not work on Windows. A cross-platform 
> solution would be to use the "|&" redirector instead of "2>@". "|&" means 
> "pipe both stdout and stderr to stdin of the following command" (see 
> http://www.tcl.tk/man/tcl8.5/tutorial/Tcl26.html ). This command could 
> then copy everything to its stdout and that's what gronsole gets back.
> 
> The Unix cat command is an obvious choice but it isn't available on 
> Windows. "more" kind of works but it's a shell (cmd.exe) built-in command 
> on Windows so not suitable either. I was wondering if anybody has any 
> thoughts on
> 1. Are there any other Windows commands that might be suitable for this?
> or
> 2. Might it be a worthwhile idea to include a simple program with GRASS 
> that did this (copies everything from stdin to stdout) so we can be sure 
> what's going on?

I suggest the latter, i.e. write our own mini-"cat" program. It will
need to explicitly put stdin/stdout into line-buffered mode (pipes are
block-buffered by default).

BTW, 2>@stdout doesn't merge stdout/stderr, in the way that 2>&1 does
in bash. It redirects the process' stderr to wish's stdout; on Unix,
this is typically the terminal from which gis.m was started, but wish
may not have stdin/stdout/stderr channels on Windows.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list