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: David MENTRE <dmentre@l...>
Subject: Re: [Caml-list] kprintf with user formatters
Hello,

Markus Mottl <markus@oefai.at> writes:

> 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?

# let cur_level = ref 0;;
val cur_level : int ref = {contents = 0}
# let log level fmt =
  let print_at_level str = if !cur_level >= level then print_string str in
  Format.kprintf print_at_level fmt;;
val log : int -> ('a, unit, string, unit) format4 -> 'a = <fun>

# log 3 "%d %s" 3 "toto";;
- : unit = ()

# cur_level := 4;;
- : unit = ()
# log 3 "%d %s" 3 "toto";;
3 toto- : unit = ()

The trick is to call kprintf as last expression. Thank you to Damien
Doligez: http://caml.inria.fr/archives/200405/msg00355.html

I hope it helps,
Yours,
d.
-- 
 David Mentré <dmentre@linux-france.org>

-------------------
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