Add tracing infrastructure.
[SXSI/xpathcomp.git] / src / options.ml
index d72763e..70d702d 100644 (file)
@@ -1,3 +1,6 @@
+open Utils
+open Format
+
 let index_empty_texts = ref false
 let sample_factor = ref 64
 let disable_text_collection = ref false
@@ -27,7 +30,6 @@ let set_index_type = function
 
 let usage_msg = Printf.sprintf "%s [options] <input.{xml|srx}> 'query' [output]" Sys.argv.(0)
 
-
 let pos = ref 0
 let anon_fun =
   fun s -> match !pos with
@@ -36,6 +38,27 @@ let anon_fun =
     | 2 -> output_file := Some s; incr pos
     | _ -> raise (Arg.Bad(s))
 
+let set_tracer s =
+  List.iter (fun t ->
+    if t = "" then ()
+    else
+      match String.explode t ':' with
+       [ tr; lvl ] ->
+         let l = try int_of_string lvl with _ -> raise (Arg.Bad (lvl)) in
+         if Tracer.is_tracer tr then Tracer.activate tr l
+         else raise (Arg.Bad (t))
+      | _ -> raise (Arg.Bad (t))
+  ) (String.explode s ',')
+
+let pretty_tracers () =
+  ignore(flush_str_formatter());
+  Pretty.print_list
+    ~sep:", "
+    (fun f s -> fprintf f "%s" s)
+    str_formatter
+    (Tracer.available ());
+  flush_str_formatter ()
+
 let spec = Arg.align
   [ "-c", Arg.Set(count_only),
     " counting only (don't materialize the result set)";
@@ -63,10 +86,13 @@ let spec = Arg.align
     " choose text index type";
 
     "-v", Arg.Set(verbose), " verbose mode"; ] @
-IFDEF TRACE
+IFNDEF NTRACE
 THEN [
     "-trace-file", Arg.Set_string(trace_file),
-    "<trace_file> save the full trace in dot format in <trace_file>"
+    "<trace_file> save the full trace in dot format in <trace_file>";
+    "-trace", Arg.String (set_tracer),
+    "<tracer1:l1,...,tracern:ln> enable tracing with the specified level. Valid tracers are: "
+      ^ (pretty_tracers ())
      ]
 ELSE []
 END