Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] kprintf with user formatters
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] kprintf with user formatters
> 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