caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alan Schmitt <alan.schmitt@polytechnique.org>
To: Jeremie Dimino <jdimino@janestreet.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel?
Date: Sat, 29 Mar 2014 12:02:57 +0100	[thread overview]
Message-ID: <m2fvm1w9lq.fsf@polytechnique.org> (raw)
In-Reply-To: <CANhEzE6AyCjw4dEF2K_R8=Q9tFZtPo196+G+5Sj958aUHeVuMw@mail.gmail.com> (Jeremie Dimino's message of "Mon, 24 Mar 2014 14:47:30 +0000")

Hello Jeremie,

Jeremie Dimino <jdimino@janestreet.com> writes:

> The best way is to create a custom toplevel and map the replies.
>
> "val x : int = 3" is printed using [Toploop.print_out_phrase] which is
> a reference, so you can replace by a function adding markers around
> the text.
>
> This won't catch error/warning messages. If you also want to
> distinguish these, you can redirect the standard formatters. The
> toplevel prints replies and error messages on the formatter passed to
> [Toploop.loop], which is [Format.std_formatter] by default. So you can
> either:
> (1) override [Format.std_formatter] callbacks
> (2) call [Toploop.loop] with a custom formatter
>
> You can do (1) with [Format.pp_set_all_formatter_output_functions] and
> (2) by setting the startup hook:
>
> Toploop.toplevel_startup_hook := (fun () -> Toploop.loop my_formatter;
> exit 0)
>
> Warnings are always printed on [Format.err_formatter] so you'll need
> to override its callbacks.
>
> If you can't create a custom toplevel, I guess you can also send the
> code to the toplevel at the beginning of the session.

Thanks a lot, this was most helpful. This is what I ended up doing. The
following, when evaluated, will send all the information from the
toplevel to some file, and only output in the toplevel things that are
explicitly printed.

#+begin_src ocaml
let _,_,newline,space = 
  Format.pp_get_all_formatter_output_functions Format.std_formatter ();;

let outfile = open_out "/Users/schmitta/tmp/testoutml.txt";;

let myout s p n = Printf.fprintf outfile "%s" (String.sub s p n);;

let myflush () = flush outfile;;

Format.pp_set_all_formatter_output_functions 
  Format.std_formatter 
  myout 
  myflush 
  newline 
  space;;
#+end_src

Alan


      reply	other threads:[~2014-03-29 11:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-24 13:17 Alan Schmitt
2014-03-24 14:47 ` Jeremie Dimino
2014-03-29 11:02   ` Alan Schmitt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2fvm1w9lq.fsf@polytechnique.org \
    --to=alan.schmitt@polytechnique.org \
    --cc=caml-list@inria.fr \
    --cc=jdimino@janestreet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).