Stderror

General FreeBASIC programming questions.
Post Reply
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Stderror

Post by Bunuel66 »

I would like to use open pipe with a program sending it's output to sterr rather to stdout.
Is it some way to do that? (Open pipe seems not to be able to access stderr.)

Regards
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

There's Open Error, which makes me unsure as to whether stderr can be accessed as a pipe. Looks like stdout might handle that.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

@Bunuel

Windows or *nix?

If Windows, I find that STDOUT and STDERR both appear as STDIN to open pipe with fb v.20. Using this test of STDERR:

Code: Select all

'compile as stderr-1.exe
#include once "crt.bi"
'
dim as integer res
res=fprintf(STDERR,!"Now is the time\n")
'
end
[You can test this to verify that it is printing only to STDERR]

And then this as test of open:

Code: Select all

dim as string s
open pipe "stderr-1.exe" for input as #1
while not eof(1)
	line input #1,s
	print s
wend
'
end
I get "Now is the time".
'
I expected to have to redirect STDERR in the pipe:
open pipe "stderr-1.exe 2>&1" for input as #1
but that isn't necessary for me on XP.
'
So my experience with Windows XP is that "open pipe" is capturing STDIN and STDERR as one (folding both into STDIN).
'
You are using what version of fb on what OS?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

I made a test as well.

StdErrTest: Opens up testprog, passes the name of stderrtest to the testprog, and then passes a second argument which will be printed through stderr by testprog.

Testprog: Gets the name of the exe that called it, opens up its output stream and prints data back to it as well as the stderr stream.

stderrtest.bas

Code: Select all

dim as integer ff = freefile()
dim as string tempStr

open pipe "testProg.exe stderrtest.exe foo" for input as #ff
	while not EOF(ff)
		line input #ff, tempStr
		print tempStr
	wend
close #ff
sleep
testProg.bas

Code: Select all

dim as integer fferr = freefile()
dim as integer ffout = freefile()

dim as string cmnd = command(1)
open err for output as #fferr
open pipe cmnd for output as #ffout
	print #ffout, "Hello, " & cmnd
	print #fferr, "RANDOM ERROR BECAUSE OF: " & command(2)
close #ffout
close #fferr
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

After some thought.. STDERR is not wrapped in with STDIN in "open pipe", Bunuel66 is correct that "open pipe" doesn't capture STDERR. So it is necessary to redirect STDERR.

This is a better test, and reflects redirecting STDERR:

Code: Select all

dim as string s
open pipe "stderr-1.exe 2>&1" for input as #1
while not eof(1)
    line input #1,s
    if not eof(1) then
        color 12,0    'if isn't red STDERR wasn't redirected
        print "s= ";s 'if no "s= "..
    end if
wend
'
sleep
end
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Still in trouble....

Post by Bunuel66 »

Thanks guys for the suggestions. As said by zippy stderr is not handled by the pipe.
I have tried a lot of combinations without success.
To be more detailed, I I'm trying to catch the output of the java compiler "javac" for displaying it in an IUP window.
I'm running FBC under Ubuntu 8.04.1
I have found a work around using javac -XStdout for forcing the output to an intermediate file.
But no way to do it directly......If somebody has an idea....

Regards

Luis
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

(an idea) If you are interested in Linux only, then you can use the C runtime functions.

Garvan
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Post by Bunuel66 »

After a lot of test and errors, a very simple way to capture the stderr file through a pipe:
Just call your command with 2>&1 \ before
Beware that the "\" is important for extending the redirection to all the following commands.

Regards
Post Reply