Lucas,

For the record, you can also achieve this kind of effect with Posix.IO.dup2. 
See below for sample code.

Regards,

Rob.


(*

Redirecting standard output to a file using the Posix facilities
in the SML Basis Library.

Implements a stack of output files.

Rob Arthan. r...@lemma-one.com 12th November 2008.

*)
local
(*
Open Posix.IO then Posix.FileSys structures - order matters!
*)
        open Posix.IO Posix.FileSys TextIO;
(*
Take a duplicate of current stdout.
*)
        val duplicate_stdout : file_desc = dup stdout;
(*
Create an initially empty stack of file descriptors:
*)
        val stack : file_desc list ref = ref [];
(*
File creation mode: read/write for user, group and others,
but bits set with umask(1) will be cleared as usual.
*)
        val rw_rw_rw = S.flags[S.irusr, S.iwusr, S.irgrp,
                        S.iwgrp, S.iroth, S.iwoth];
in
(*
push_out_file: start a new output file, stacking the file descriptor.
*)
fun push_output_file {name: string, append : bool} : unit = (
        let     val flags = if append then O.append else O.trunc;
                val fd = createf(name, O_WRONLY, flags, rw_rw_rw);
        in      output(stdOut, "*** Redirecting output to \"" ^ name ^ "\"\n");
                dup2{old = fd, new = stdout};
                stack := fd :: !stack
        end
);
(*
pop_output_file: close file descriptor at top of stack and
revert to previous; returns true if the output file stack
is not empty on exit, so you can close all open output files
and clear the stack with:

        while pop_output_file() do ();

*)
fun pop_output_file (() : unit) : bool = (
        (case !stack of
                cur_fd :: rest => (
                        close cur_fd;
                        stack := rest
        ) |     [] => ());
        case !stack of
                fd :: _ => (
                        dup2{old = fd, new = stdout};
                        true
        ) |     [] => (
                        dup2{old = duplicate_stdout, new = stdout};
                        false
        )
);
end;

On 13 Nov 2010, at 20:39, Lucas Dixon wrote:

> Thanks! that is exactly what I was looking for.
> 
> lucas
> 
> On 13/11/2010 01:06, Matthew Fluet wrote:
>> I think you want the "setOutstream" function of the IMPERATIVE_IO
>> signature (matched by TextIO), probably in conjunction with the
>> "getOutstream" function.
>> See 
>> http://www.standardml.org/Basis/imperative-io.html#SIG:IMPERATIVE_IO.setOutstream:VAL
>> 
>> -Matthew
>> 
>> On Fri, Nov 12, 2010 at 7:16 PM, Lucas Dixon<ldi...@inf.ed.ac.uk>  wrote:
>>> Hi,
>>> 
>>> I was recently wondering if it is possible to redirect std-out to go to a
>>> different output stream in ML. Has anyone done anything similar? I was
>>> having trouble seeing how to do this with the Standard-ML basis...
>>> 
>>> any suggestions on directions to look at? I can see how to effectively to it
>>> by hacking the compiler... but was wondering if there is a better way...
>>> (surely!?) ?
>>> 
>>> cheers,
>>> lucas
>>> 
>>> --
>>> The University of Edinburgh is a charitable body, registered in
>>> Scotland, with registration number SC005336.
>>> 
>>> _______________________________________________
>>> polyml mailing list
>>> polyml@inf.ed.ac.uk
>>> http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
>>> 
>> 
> 
> 
> -- 
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
> 
> _______________________________________________
> polyml mailing list
> polyml@inf.ed.ac.uk
> http://lists.inf.ed.ac.uk/mailman/listinfo/polyml

_______________________________________________
polyml mailing list
polyml@inf.ed.ac.uk
http://lists.inf.ed.ac.uk/mailman/listinfo/polyml

Reply via email to