English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Camlp4: varargs ie a la Printf
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-06-17 (14:47)
From: Basile STARYNKEVITCH <basile.starynkevitch@c...>
Subject: [Caml-list] Camlp4: varargs ie a la Printf
Dear All Camlp4-ers,

I am using OCaml 3.04 (with the standard Camlp4 3.04) I do know that
the CVS Ocaml does have a Printf.kprintf function which would make my
problem easier to solve.

My problem is how to have a sort of variadic macro.

(**** file pa_trace.ml *****)

    this file provide a tracing preprocessor facility; 
   at compile time, you can preprocess with the -Tfoo flag (there can be several such flags);
   in the preprocessed source, you can code 
      trace FOO "i=%d x=%g" i x end
   which is preprocessed to 
      if (Trace.Tracing.foo) then Trace.trace Trace.Level.Foo (Printf.sprintf "i=%d x=%g" i x) ;
   if preprocessed with -Tfoo or just to the unit value

module SetOfName = Set.Make(
    type t= string;;
    let compare s1 s2 = Pervasives.compare s1 s2 

let trset = ref SetOfName.empty;;

(* function to add tracing *)
let add_tracing trname = 
  trset := SetOfName.add trname !trset

let is_traced trname =
  (SetOfName.is_empty !trset) || (SetOfName.mem trname !trset)

  GLOBAL: Pcaml.expr ;
  Pcaml.expr: LEVEL "top" 
  [ [ "trace" ; trid = UIDENT ; fmt = STRING ; args = LIST0 Pcaml.expr ; "end" ->
    let lctrid = String.lowercase trid in 
    Printf.eprintf "trace %s does not work yet\n" lctrid;
    if is_traced lctrid then
      if Trace.Tracing.$lid:lctrid$ 
      then Trace.trace Trace.Level.$lid:lctrid$ 
	(Printf.sprintf $str:fmt$ 
      else () 
      <:expr< () >>
    ] ]

(****** end of file *****)

My problem is to produce a quotation of a list of expressions (and not
a quotation of a list expression). What should I put in place of the
comment WHAT SHOULD BE ADDED HERE?   trace FOO "i=%d x=%g" i x end

I am not sure to understand what the antiquotation  $list:l$ is for.

With the future Ocaml I would just expand
   trace FOO "i=%d x=%g" i x end
   if (Trace.Tracing.foo) then Printf.kprintf (Trace.ktrace Trace.Level.Foo) "i=%d x=%g" i x

which I believe should be simpler.

Perhaps the question is easy, but it is really too hot now
here..... so I am coding poorly.

If Ocaml3.05 is out soon I might use kprintf anyway....

Otherwise if some people have some tracing or logging macros (which
they accept to share into a GPL-ed code?) I would be grateful.


N.B. Any opinions expressed here are only mine, and not of my organization.
N.B. Les opinions exprimees ici me sont personnelles et n engagent pas le CEA.

Basile STARYNKEVITCH   ----  Commissariat à l Energie Atomique * France
DRT/LIST/DTSI/SLA * CEA/Saclay b.528 (p111f) * 91191 GIF/YVETTE CEDEX 
phone:+33 1,6908.6055; fax: 1,6908.8395 home: 1,4665.4553; mobile: 6,8501.2359
work email: Basile point Starynkevitch at cea point fr 
home email: Basile at Starynkevitch point net

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