add CCFormat.tee

This commit is contained in:
Simon Cruanes 2017-01-11 18:38:37 +01:00
parent ed38c25711
commit 3f80e794ba
2 changed files with 24 additions and 0 deletions

View file

@ -130,6 +130,26 @@ let fprintf = Format.fprintf
let stdout = Format.std_formatter
let stderr = Format.err_formatter
let tee a b =
let fa = Format.pp_get_formatter_out_functions a () in
let fb = Format.pp_get_formatter_out_functions b () in
Format.make_formatter
(fun str i len ->
fa.Format.out_string str i len;
fb.Format.out_string str i len)
(fun () -> fa.Format.out_flush (); fb.Format.out_flush ())
(*$R
let buf1 = Buffer.create 42 in
let buf2 = Buffer.create 42 in
let f1 = Format.formatter_of_buffer buf1 in
let f2 = Format.formatter_of_buffer buf2 in
let fmt = tee f1 f2 in
Format.fprintf fmt "coucou@.";
assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf1);
assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf2);
*)
let to_file filename format =
let oc = open_out filename in
let fmt = Format.formatter_of_out_channel oc in

View file

@ -155,6 +155,10 @@ val to_string : 'a printer -> 'a -> string
val stdout : t
val stderr : t
val tee : t -> t -> t
(** [tee a b] makes a new formatter that writes in both [a] and [b].
@since NEXT_RELEASE *)
val sprintf : ('a, t, unit, string) format4 -> 'a
(** Print into a string any format string that would usually be compatible
with {!fprintf}. Similar to {!Format.asprintf}. *)