2017-03-10 12:32:27 +00:00
|
|
|
open Import
|
|
|
|
|
|
|
|
type real =
|
2018-02-07 11:38:21 +00:00
|
|
|
{ oc : out_channel
|
|
|
|
; buf : Buffer.t
|
|
|
|
; ppf : Format.formatter
|
|
|
|
; display : Config.Display.t
|
2017-03-10 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type t = real option
|
|
|
|
|
|
|
|
let no_log = None
|
|
|
|
|
2018-02-07 11:38:21 +00:00
|
|
|
let create ?(display=Config.default.display) () =
|
2017-03-10 12:32:27 +00:00
|
|
|
if not (Sys.file_exists "_build") then
|
|
|
|
Unix.mkdir "_build" 0o777;
|
2018-04-24 20:25:27 +00:00
|
|
|
let oc = Io.open_out (Path.of_string "_build/log") in
|
2017-05-26 13:07:21 +00:00
|
|
|
Printf.fprintf oc "# %s\n# OCAMLPARAM: %s\n%!"
|
|
|
|
(String.concat (List.map (Array.to_list Sys.argv) ~f:quote_for_shell) ~sep:" ")
|
2018-03-29 15:58:41 +00:00
|
|
|
(match Env.get Env.initial "OCAMLPARAM" with
|
|
|
|
| Some s -> Printf.sprintf "%S" s
|
|
|
|
| None -> "unset");
|
2017-03-10 12:32:27 +00:00
|
|
|
let buf = Buffer.create 1024 in
|
|
|
|
let ppf = Format.formatter_of_buffer buf in
|
2018-02-07 11:38:21 +00:00
|
|
|
Some { oc; buf; ppf; display }
|
2017-03-10 12:32:27 +00:00
|
|
|
|
2018-02-07 11:38:21 +00:00
|
|
|
let info_internal { ppf; display; _ } str =
|
|
|
|
let write ppf =
|
2017-03-30 16:36:58 +00:00
|
|
|
List.iter (String.split_lines str) ~f:(function
|
2018-02-07 11:38:21 +00:00
|
|
|
| "" -> Format.pp_print_string ppf "#\n"
|
|
|
|
| s -> Format.fprintf ppf "# %s\n" s);
|
|
|
|
Format.pp_print_flush ppf ()
|
|
|
|
in
|
|
|
|
write ppf;
|
2018-02-13 19:04:21 +00:00
|
|
|
if display = Verbose then print_to_console (Format.asprintf "%t" write)
|
2017-03-10 12:32:27 +00:00
|
|
|
|
|
|
|
let info t str =
|
|
|
|
match t with
|
|
|
|
| None -> ()
|
|
|
|
| Some t -> info_internal t str
|
|
|
|
|
|
|
|
let infof t fmt =
|
|
|
|
match t with
|
|
|
|
| None -> Format.ikfprintf ignore Format.str_formatter fmt
|
|
|
|
| Some t ->
|
|
|
|
Format.kfprintf
|
|
|
|
(fun ppf ->
|
|
|
|
Format.pp_print_flush ppf ();
|
|
|
|
let s = Buffer.contents t.buf in
|
|
|
|
Buffer.clear t.buf;
|
|
|
|
info_internal t s)
|
|
|
|
t.ppf
|
|
|
|
fmt
|
|
|
|
|
|
|
|
let command t ~command_line ~output ~exit_status =
|
|
|
|
match t with
|
|
|
|
| None -> ()
|
|
|
|
| Some { oc; _ } ->
|
|
|
|
Printf.fprintf oc "$ %s\n" (Ansi_color.strip command_line);
|
|
|
|
List.iter (String.split_lines output) ~f:(fun s ->
|
|
|
|
match Ansi_color.strip s with
|
|
|
|
| "" -> output_string oc ">\n"
|
|
|
|
| s -> Printf.fprintf oc "> %s\n" s);
|
|
|
|
(match (exit_status : Unix.process_status) with
|
|
|
|
| WEXITED 0 -> ()
|
|
|
|
| WEXITED n -> Printf.fprintf oc "[%d]\n" n
|
|
|
|
| WSIGNALED n -> Printf.fprintf oc "[got signal %s]\n" (Utils.signal_name n)
|
|
|
|
| WSTOPPED _ -> assert false);
|
|
|
|
flush oc
|