Re: How to redirect stdout

Alexandre Saidi <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Organization ECL
Message-ID <[email protected]>
Hi there,
Following my question about "stdout redirection in Gprolog", Gurvan Le 
Guernic gave a general
  answer ("script" command under Unix/Linux) that did the job.
I post it hereby.

This works not only for Gprolog but also for any command.
In order to gather (to mirror) outputs,  launch " script -c gprolog 
myfile" and "myfile" is a mirrir of the stdout :
(see "man script" also).

Here's an example (sorry for Windoz users !):
---------------------------------------------------------

alex@safran:> script -c gprolog toto
Le script a débuté, le fichier est toto
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- write(1).
1

yes
| ?- listing.

yes
.......................

| ?- halt.

Script complélé, le fichier est toto
alex@safran:>

alex@safran:> cat toto
Le script a débuté sur mar 01 mar 2005 19:58:00 CET
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- write(1).
1

yes
| ?- listing.

yes
.....................
| ?- halt.
Script complété sur mar 01 mar 2005 19:58:10 CET
---------------------------------

AS

Alexandre Saidi a écrit :
> Dear Lindsey,
> In my question, 'lising' was juste an (unfortunate) example !
> 
> I learned however how I'll do  for thisparticular  predicate.
> 
> I dont want to boder you so much (specially  Gurvan Le Guernic gave an 
> answer that's quite ok, for this task and so forth. Merci beaucoup à 
> Gurvan Le Guernic).
> 
> However, I tried  the following with no sucess.
> Don't worry. things seem much tighter !
> 
> I want to thak you for all your answers and post this one :
> Is there any way to have documents (other than "taking a deep look" at 
> the source of gprolog) to know these low-level predicats ?
> Answers would come from there, if any.
> 
> cheers.
> Merci
> --------------------------------
> alex@safran:~> gprolog
> GNU Prolog 1.2.16
> By Daniel Diaz
> Copyright (C) 1999-2002 Daniel Diaz
> | ?- [user].
> compiling user for byte code...
> entier(0).
> entier(succ(N)) :- entier(N).
> 
> user compiled, 3 lines read - 346 bytes written, 5319 ms
> 
> yes
> | ?- open(fichier,write,F), current_input(I), current_output(O), 
> '$set_top_level_streams'(I, F),entier(succ(succ(0))), 
> '$set_top_level_streams'(I, O), close(F).
> 
> F = '$stream'(2)
> I = '$stream'(0)
> O = '$stream'(1)
> 
> yes
> | ?- halt.
> 
> ZZZ : I tried with other examples that normaly give 'yes' or 'no' withe 
> variables or not.
> 
> ---------------------------------
> 
> Lindsey Spratt a écrit :
> 
>> I did some more investigating...
>>
>> The particular predicate with which you are testing the redirection of 
>> output, listing/0, sends its output to the "top level output", not 
>> stdout. So, redirecting stdout doesn't affect the output of listing/0.
>>
>> Here is an approach that does redirect the output of listing/0, using 
>> the internal '$set_top_level_streams'/2 predicate:
>>
>> ======
>> [Lindsey-Spratts-Computer:~/Documents] lindsey% gprolog
>> GNU Prolog 1.2.16
>> By Daniel Diaz
>> Copyright (C) 1999-2002 Daniel Diaz
>> | ?- [user].
>> compiling user for byte code...
>> toto:-true.
>> user compiled, 2 lines read - 205 bytes written, 8773 ms
>>
>> yes
>> | ?- open(fichier,write,F), current_input(I), current_output(O), 
>> '$set_top_level_streams'(I, F), listing, '$set_top_level_streams'(I, 
>> O), close(F).
>>
>>
>> F = '$stream'(2)
>> I = '$stream'(0)
>> O = '$stream'(1)
>>
>> yes
>> | ?- halt.
>> [Lindsey-Spratts-Computer:~/Documents] lindsey% cat fichier
>>
>> toto.
>> [Lindsey-Spratts-Computer:~/Documents] lindsey%
>> =====
>>
>> The redirection using set_output/1 should work when redirecting the 
>> output from the 'write/1' predicate.
>>
>> Cheers,
>> Lindsey Spratt
>>
>> On Mar 1, 2005, at 11:30 AM, Alexandre Saidi wrote:
>>
>>> Thanks Lindsey,
>>> I've been trying any thing I could to make it work.
>>> Ther's no way !
>>> As I said, I saw a mail in the FAQ saying that stdout is not affected 
>>> by set_output.
>>> I'm using the version 1.2.16.
>>> Perhaps ther'is is a trick withe the interactive mode ?
>>>
>>> Thanks any way.
>>> ---------------------------------------------
>>> Here's what I do under prolog (for a test) :
>>> alex@safran:~> gprolog
>>> GNU Prolog 1.2.16
>>> By Daniel Diaz
>>> Copyright (C) 1999-2002 Daniel Diaz
>>> | ?- [user].
>>> compiling user for byte code...
>>> toto :- true.
>>>
>>> user compiled, 2 lines read - 207 bytes written, 4104 ms
>>>
>>> yes
>>> | ?- open(fichier,write,F), current_output(Old), set_output(F), 
>>> listing, flush_output(F), flush_output, close(F).
>>>
>>> toto.
>>>
>>> F = '$stream'(2)
>>> Old = '$stream'(1)
>>>
>>> (1 ms) yes
>>> | ?- halt.
>>> alex@safran:~>
>>>
>>> The file 'fichier' is empty.
>>> I've done many other tests with no success.
>>> ---------------------------------------------------------
>>>
>>> Lindsey Spratt a écrit :
>>>
>>>> Perhaps flush_output(F) before the close(F), or simply 
>>>> 'flush_output', would help.
>>>> Lindsey
>>>> On Mar 1, 2005, at 3:04 AM, Alexandre Saidi wrote:
>>>>
>>>>> Thanks for your help.
>>>>>
>>>>> I'd already tried :
>>>>>
>>>>> | ?-open('/home/alex/fichier',write,F), current_output(Old), 
>>>>> set_output(F), a_call, close(F), set_output(Old).
>>>>>
>>>>> But the file 'fichier' is of size 0 !
>>>>>
>>>>> So what's wrong with my code?
>>>>> should I flush something ?
>>>>>
>>>>> regards.
>>>>>
>>>>> AS
>>>
>>>
>>>
>>> -- 
>>> Aleksander S. Saidi
>>> Ecole Centrale de Lyon
>>> Département Mathématiques-Informatique
>>> Mél : [email protected]
>>> Tél : 04.72.18.65.30, Fax : 04.78.33.16.15
>>>
>>
>>
> 

-- 
Aleksander S. Saidi
Ecole Centrale de Lyon
Département Mathématiques-Informatique
Mél : [email protected]
Tél : 04.72.18.65.30, Fax : 04.78.33.16.15
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.