From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA29659; Wed, 30 Jun 2004 18:34:50 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA29649 for ; Wed, 30 Jun 2004 18:34:49 +0200 (MET DST) Received: from fafnir.ens-lsh.fr (fafnir.ens-lsh.fr [193.51.131.254]) by nez-perce.inria.fr (8.12.10/8.12.10) with SMTP id i5UGYmEV022974 for ; Wed, 30 Jun 2004 18:34:48 +0200 Received: from ([172.16.1.1]) by fafnir.ens-lsh.fr; Wed, 30 Jun 2004 18:35:02 +0200 (CEST) Received: from avalon.ens-lsh.fr (localhost [127.0.0.1]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id i5UHXQOb010178 for ; Wed, 30 Jun 2004 18:33:27 +0100 (WEST) Received: from ens-notes1.ens-lsh.fr (ens-notes1.ens-lsh.fr [10.2.1.44]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id i5UHXQ7u010175 for ; Wed, 30 Jun 2004 18:33:26 +0100 (WEST) Received: from mostha ([10.3.25.139]) by ens-notes1.ens-lsh.fr (Lotus Domino Release 5.0.10) with SMTP id 2004063018344409:58734 ; Wed, 30 Jun 2004 18:34:44 +0200 Date: Wed, 30 Jun 2004 18:32:37 +0200 From: Damien To: Ocaml Subject: [Caml-list] kprintf with user formatters Message-Id: <20040630183237.457317c7@mostha> Organization: mosthagloups X-Mailer: Sylpheed version 0.9.11claws (GTK+ 1.2.10; i386-pc-linux-gnu) Mime-Version: 1.0 X-MIMETrack: Itemize by SMTP Server on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 30/06/2004 18:34:44, Serialize by Router on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 30/06/2004 18:34:46, Serialize complete at 30/06/2004 18:34:46 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Miltered: at nez-perce with ID 40E2EBA8.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; damien:01 damien:01 ens-lyon:01 kprintf:01 formatters:01 threads:01 threads:01 struct:01 printf:01 kprintf:01 endline:01 val:01 val:01 printf:01 formatter:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, I am writing a multi-threaded program, so that I need some synchronization when pretty-printing things : << #directory "+threads";; #load "unix.cma";; #load "threads.cma";; module Debug = struct let m = Mutex.create() let printf x = Format.kprintf (fun s -> Mutex.lock m; print_endline s; Mutex.unlock m) x end;; (* module Debug : sig val m: Mutex.t val printf : ('a, unit, string, unit) format4 -> 'a end *) >> but now, I can't _nicely_ use my previously defined printers : suppose I have got a type with its toplevel pretty-printer : << type t val format_t: Format.formatter -> t -> unit val x: t >> I would like to write : << Debug.printf "val t = %a" format_t x >> but I cannot since the user printer is expected to be "unit->t->string" (while it is "formatter->t->unit" for the standard Format.printf "%a") currently I use : << let (!!) f = (fun () x -> f Format.str_formatter x; Format.flush_str_formatter());; (* val (!!): (Format.formatter -> 'a -> 'b) -> unit -> 'a -> string *) Debug.printf "val t = %a" !!format_t x >> this seems to be working, but I wonder if : - there is a better solution - this use of the global value "Format.str_formatter" is really thread-safe (I would say no...) - I missed something - printf could not understand a conversion character 'A' that would always need a _general_ user-formatter (of type formatter->t->unit) thanks, damien ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners