Permalink
Browse files

Display stderr/stdout messages when using --verbose

  • Loading branch information...
1 parent 970ea15 commit 3409973bf3d74ea95d71ab7dd2cde1ab6fbb1105 @samoht samoht committed Jun 10, 2012
Showing with 22 additions and 8 deletions.
  1. +2 −0 src/globals.ml
  2. +1 −0 src/opam.ml
  3. +19 −8 src/process.ml
View
@@ -18,6 +18,8 @@ let debug = ref (
with _ -> false
)
+let verbose = ref false
+
let yes = ref false
let version = "0.1+dev"
View
@@ -46,6 +46,7 @@ let () = Globals.root_path := Globals.default_opam_path
let global_args = [
"--debug" , Arg.Set Globals.debug, " Print more debug messages";
+ "--verbose", Arg.Set Globals.verbose, " Display stdout/stderr of subprocesses";
"--version", Arg.Unit version, " Display version information";
"--yes" , Arg.Set Globals.yes, " Answer yes to all questions";
"--root" , Arg.Set_string Globals.root_path,
View
@@ -28,12 +28,23 @@ type t = {
let open_flags = [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC]
let create ?info ?stdout ?stderr ?env cmd args =
- let stdout_fd = match stdout with
- | None -> Unix.stdout
- | Some f -> Unix.openfile f open_flags 0o644 in
- let stderr_fd = match stderr with
- | None -> Unix.stderr
- | Some f -> Unix.openfile f open_flags 0o644 in
+ let nothing () = () in
+ let tee f =
+ let fd = Unix.openfile f open_flags 0o644 in
+ if !Globals.debug || !Globals.verbose then (
+ let chan = Unix.open_process_out ("tee " ^ f) in
+ let close () =
+ match Unix.close_process_out chan with
+ | _ -> () in
+ Unix.descr_of_out_channel chan, close
+ ) else
+ fd, nothing in
+ let stdout_fd, close_stdout = match stdout with
+ | None -> Unix.stdout, nothing
+ | Some f -> tee f in
+ let stderr_fd, close_stderr = match stderr with
+ | None -> Unix.stderr, nothing
+ | Some f -> tee f in
let env = match env with
| None -> Unix.environment ()
| Some e -> e in
@@ -44,8 +55,8 @@ let create ?info ?stdout ?stderr ?env cmd args =
(Array.of_list (cmd :: args))
env
Unix.stdin stdout_fd stderr_fd in
- (match stdout with None -> () | Some _ -> Unix.close stdout_fd);
- (match stderr with None -> () | Some _ -> Unix.close stderr_fd);
+ close_stdout ();
+ close_stderr ();
{
p_name = cmd;
p_args = args;

0 comments on commit 3409973

Please sign in to comment.