Add tracing infrastructure.
[SXSI/xpathcomp.git] / src / tracer.ml
diff --git a/src/tracer.ml b/src/tracer.ml
new file mode 100644 (file)
index 0000000..eae76a5
--- /dev/null
@@ -0,0 +1,25 @@
+open Format
+
+type tracer = string
+type level = int
+
+let tracers = [ "top-down-run"; "top-down-approx"; "result-set" ]
+let active_tracers : (tracer, int) Hashtbl.t = Hashtbl.create 17
+
+let available () = tracers
+
+let is_tracer s = List.mem s tracers
+let level s = try Hashtbl.find active_tracers s with Not_found -> 0
+let is_active s = Hashtbl.mem active_tracers s
+let activate s lvl = if not (is_active s) then Hashtbl.add active_tracers s lvl
+let deactivate s = Hashtbl.remove active_tracers s
+
+let tracer_output = ref err_formatter
+let set_output f = tracer_output := f
+
+let trace t l s =
+  if l <= level t
+  then begin
+    fprintf !tracer_output "%s: " t;
+    fprintf !tracer_output "%s" s
+  end