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 AAA18686; Fri, 16 Jul 2004 00:20:47 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA17677; Fri, 16 Jul 2004 00:20:46 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i6FMKcSH027889; Fri, 16 Jul 2004 00:20:38 +0200 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA18634; Fri, 16 Jul 2004 00:20:38 +0200 (MET DST) From: Pierre Weis Message-Id: <200407152220.AAA18634@pauillac.inria.fr> Subject: Re: [Caml-list] kprintf with user formatters In-Reply-To: <20040715001758.GF26614@fichte.ai.univie.ac.at> from Markus Mottl at "Jul 15, 104 02:17:58 am" To: markus@oefai.at (Markus Mottl) Date: Fri, 16 Jul 2004 00:20:38 +0200 (MET DST) Cc: pierre.weis@inria.fr, caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 40F70336.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; pierre:01 weis:01 pierre:01 weis:01 caml-list:01 kprintf:01 formatters:01 printf:01 fmt:01 fmt:01 printf:01 ips:99 tolerate:01 invocation:01 evidently:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > On Wed, 14 Jul 2004, Pierre Weis wrote: > > Your problem is thus that the function Debug.printf is not the right > > one (since it has not the right type :) > > Btw., since we are at it: I'd like to use my own printers depending on > a conditional, e.g.: > > let log level fmt = > if may_log level then > kfprintf ... > else > ??? > > If the given log level "level" does not allow logging the message > specified by "fmt", I just want to ignore the parameters provided together > with "fmt" - but how? Is there some (safe) trick, or do I have to write > my own Printf/Format-modules? I don't want to waste computation time > by unnecessarily converting format arguments to strings, which may be > very costly, e.g. when this would mean converting IPs to hostnames, etc. I don't know of any way to safely prevent the arguments of printf from being converted to string when interpreting the format. If you tolerate this effect, the problem is still a mere question of user's defined pretty-printer. Since you have a if then else, the ``???'' expression should have exactly the same type as the ``then kfprintf ...'' expression. Hence the simplest way to do so is to have another kfprintf invocation with the same format. Evidently you want to redirect the output of this invocation to /dev/null (I mean you do not want to see its output). The best way to do so is not to redefine a specialized version of printf, it is simply to define a specialized pretty printer that would behave as you want and then to specialize fprintf with it: let null_formatter = let null_out s idx len = () in let null_flush = ignore in Format.make_formatter null_out null_flush;; let may_log level = level > 1;; let log level fmt = if may_log level then fprintf err_formatter fmt else fprintf null_formatter fmt;; Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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