@@ -20,12 +20,26 @@ let with_lock m f x =
2020 Mutex. unlock m;
2121 raise e
2222
23+ let buffer = Buffer. create 128
24+ let m = Mutex. create ()
25+ let c = Condition. create ()
26+ let shutdown_requested = ref false
27+ let shutdown_done = ref false
28+
29+ let shutdown () =
30+ with_lock m
31+ (fun () ->
32+ shutdown_requested := true ;
33+ Buffer. add_string buffer " logging system has shutdown" ;
34+ Condition. broadcast c;
35+ while not ! shutdown_done do
36+ Condition. wait c m;
37+ done
38+ ) ()
39+
2340let reporter =
2441 let max_buffer_size = 65536 in
25- let buffer = Buffer. create 128 in
2642 let dropped_bytes = ref 0 in
27- let m = Mutex. create () in
28- let c = Condition. create () in
2943 let (_: Thread.t ) = Thread. create (fun () ->
3044 let rec next () = match Buffer. contents buffer with
3145 | "" ->
@@ -36,6 +50,14 @@ let reporter =
3650 dropped_bytes := 0 ;
3751 Buffer. reset buffer;
3852 data, dropped in
53+ let should_continue () = match Buffer. contents buffer with
54+ | "" ->
55+ if ! shutdown_requested then begin
56+ shutdown_done := true ;
57+ Condition. broadcast c;
58+ end ;
59+ not ! shutdown_done
60+ | _ -> true (* more logs to print *) in
3961 let rec loop () =
4062 let data, dropped = with_lock m next () in
4163 (* Block writing to stderr without the buffer mutex held. Logging may continue into the buffer. *)
@@ -44,15 +66,15 @@ let reporter =
4466 output_string stderr (Printf. sprintf " %d bytes of logs dropped\n " dropped)
4567 end ;
4668 flush stderr;
47- loop () in
69+ if with_lock m should_continue () then loop () in
4870 loop ()
4971 ) () in
5072 let buffer_fmt = Format. formatter_of_buffer buffer in
5173
5274
5375 let report src level ~over k msgf =
5476 let k _ =
55- Condition. signal c;
77+ Condition. broadcast c;
5678 over () ;
5779 k ()
5880 in
0 commit comments