-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ml
35 lines (30 loc) · 1.08 KB
/
debug.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(* not thread-safe. *)
module type DEBUG = sig
val indent : int -> unit
val dedent : int -> unit
val with_indent : int -> (unit -> 'a) -> 'a
val current_indent : unit -> int
val indent_string : unit -> string
val print_endline : string -> unit
val println : ('a, out_channel, unit) format -> 'a
end
module Debug : DEBUG = struct
let the_indent = ref 0
let indent i = (assert (i >= 0);
the_indent := !the_indent + i)
let dedent i = (assert (i >= 0);
assert (i <= !the_indent);
the_indent := !the_indent - i)
let with_indent i f =
let old = !the_indent in
indent i;
let result = try f () with exn -> (the_indent := old; raise exn) in
dedent i;
assert (!the_indent = old);
result
let current_indent () = !the_indent
let indent_string () = String.make !the_indent ' '
let print_endline s = Pervasives.print_endline (indent_string () ^ s)
let println fmt = Printf.printf
(Scanf.format_from_string (indent_string ()) "" ^^ fmt)
end