@@ -7,17 +7,18 @@ type ops =
77 ; gc_stat : unit -> Gc .stat
88 }
99
10- type state =
10+ type mode =
1111 | Disabled
12- | Path of string
1312 | Using of ops
1413
1514type t =
16- { mutable state : state
15+ { mutable mode : mode
16+ ; mutable after_first_event : bool
1717 }
1818
1919let make () =
20- { state = Disabled
20+ { mode = Disabled
21+ ; after_first_event = false
2122 }
2223
2324let fake_gc_stat =
@@ -44,43 +45,47 @@ let fake time_ref buf =
4445 let close () = () in
4546 let get_time () = ! time_ref in
4647 let gc_stat () = fake_gc_stat in
47- { state =
48+ { mode =
4849 Using
4950 { print
5051 ; close
5152 ; get_time
5253 ; gc_stat
5354 }
55+ ; after_first_event = false
5456 }
5557
56- let close t = match t.state with
58+ let close t = match t.mode with
5759 | Disabled -> ()
58- | Path _ -> ()
5960 | Using {print; close; _} ->
6061 print " ]\n " ;
6162 close ()
6263
63- let enable t path =
64- t.state < - Path path
65-
66- let make_reporter path =
64+ let path_ops path =
6765 let channel = Pervasives. open_out path in
6866 let print s = Pervasives. output_string channel s in
6967 let close () = Pervasives. close_out channel in
7068 let get_time () = Unix. gettimeofday () in
7169 let gc_stat () = Gc. stat () in
7270 {print; close; get_time; gc_stat}
7371
72+ let enable t path =
73+ t.mode < - Using (path_ops path)
74+
75+ let next_leading_char t =
76+ match t.after_first_event with
77+ | true -> ','
78+ | false ->
79+ t.after_first_event < - true ;
80+ '['
81+
7482let printf t format_string =
75- match t.state with
83+ match t.mode with
7684 | Disabled ->
7785 Printf. ifprintf () format_string
7886 | Using {print; _} ->
79- Printf. ksprintf print (" ," ^^ format_string ^^ " \n " )
80- | Path path ->
81- let reporter = make_reporter path in
82- t.state < - Using reporter;
83- Printf. ksprintf reporter.print (" [" ^^ format_string ^^ " \n " )
87+ let c = next_leading_char t in
88+ Printf. ksprintf print (" %c" ^^ format_string ^^ " \n " ) c
8489
8590let color_of_name = function
8691 | "ocamlc" | "ocamlc.opt" -> " thread_state_uninterruptible"
@@ -144,20 +149,13 @@ let emit_counters t ~time (stat: Gc.stat) =
144149 emit_counter t ~time " free_words" stat.free_words;
145150 emit_counter t ~time " stack_size" stat.stack_size
146151
147- let get_time t = match t.state with
148- | Disabled
149- | Path _
150- -> 0.
151- | Using {get_time; _}
152- ->
153- get_time ()
152+ let get_time t = match t.mode with
153+ | Disabled -> 0.
154+ | Using {get_time; _} -> get_time ()
154155
155- let gc_stat t = match t.state with
156- | Disabled
157- | Path _
158- -> fake_gc_stat
159- | Using {gc_stat; _} ->
160- gc_stat ()
156+ let gc_stat t = match t.mode with
157+ | Disabled -> fake_gc_stat
158+ | Using {gc_stat; _} -> gc_stat ()
161159
162160let on_process_start t ~program ~args =
163161 { start_time = get_time t
0 commit comments