summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Schuffelen <schuffelen@google.com>2019-09-27 12:37:00 -0700
committerA. Cody Schuffelen <schuffelen@google.com>2019-10-15 17:34:41 -0700
commit973f3f0424c0dc2fd2c6ee564cc4afb7db415836 (patch)
tree0a51c6abecaa6577d69ae4aaba25f4425e5ce025
parenta7df8c93db39a12249cf182622babb365856b35e (diff)
downloadcuttlefish_common-973f3f0424c0dc2fd2c6ee564cc4afb7db415836.tar.gz
Support silently running subprocesses.
This will cut down on the log spam from things like running --helpxml to deduce the flags of subprocesses. Test: Used in later CL Change-Id: I1642dcbcc3e1535da5c2dca39b9b2e29a3e67481
-rw-r--r--common/libs/utils/subprocess.cpp23
-rw-r--r--common/libs/utils/subprocess.h5
2 files changed, 19 insertions, 9 deletions
diff --git a/common/libs/utils/subprocess.cpp b/common/libs/utils/subprocess.cpp
index cda01676..4685eebf 100644
--- a/common/libs/utils/subprocess.cpp
+++ b/common/libs/utils/subprocess.cpp
@@ -68,8 +68,7 @@ cvd::Subprocess subprocess_impl(
const char* const* command, const char* const* envp,
const std::map<cvd::Subprocess::StdIOChannel, int>& redirects,
const std::map<cvd::SharedFD, int>& inherited_fds, bool with_control_socket,
- cvd::SubprocessStopper stopper,
- bool in_group = false) {
+ cvd::SubprocessStopper stopper, bool in_group = false, bool verbose = false) {
// The parent socket will get closed on the child on the call to exec, the
// child socket will be closed on the parent when this function returns and no
// references to the fd are left
@@ -117,10 +116,12 @@ cvd::Subprocess subprocess_impl(
if (pid == -1) {
LOG(ERROR) << "fork failed (" << strerror(errno) << ")";
}
- LOG(INFO) << "Started (pid: " << pid << "): " << command[0];
- int i = 1;
- while (command[i]) {
- LOG(INFO) << command[i++];
+ if (verbose) {
+ LOG(INFO) << "Started (pid: " << pid << "): " << command[0];
+ int i = 1;
+ while (command[i]) {
+ LOG(INFO) << command[i++];
+ }
}
return cvd::Subprocess(pid, parent_socket, stopper);
}
@@ -279,15 +280,21 @@ bool Command::RedirectStdIO(Subprocess::StdIOChannel subprocess_channel,
cvd::SharedFD::Dup(static_cast<int>(parent_channel)));
}
+void Command::SetVerbose(bool verbose) {
+ verbose_ = verbose;
+}
+
Subprocess Command::StartHelper(bool with_control_socket, bool in_group) const {
auto cmd = ToCharPointers(command_);
if (use_parent_env_) {
return subprocess_impl(cmd.data(), nullptr, redirects_, inherited_fds_,
- with_control_socket, subprocess_stopper_, in_group);
+ with_control_socket, subprocess_stopper_, in_group,
+ verbose_);
} else {
auto envp = ToCharPointers(env_);
return subprocess_impl(cmd.data(), envp.data(), redirects_, inherited_fds_,
- with_control_socket, subprocess_stopper_, in_group);
+ with_control_socket, subprocess_stopper_, in_group,
+ verbose_);
}
}
diff --git a/common/libs/utils/subprocess.h b/common/libs/utils/subprocess.h
index bcfa173d..06539fd6 100644
--- a/common/libs/utils/subprocess.h
+++ b/common/libs/utils/subprocess.h
@@ -119,7 +119,7 @@ class Command {
// SIGKILL to the subprocess.
Command(const std::string& executable,
SubprocessStopper stopper = KillSubprocess)
- : subprocess_stopper_(stopper) {
+ : subprocess_stopper_(stopper), verbose_(true) {
command_.push_back(executable);
}
Command(Command&&) = default;
@@ -158,6 +158,8 @@ class Command {
bool RedirectStdIO(Subprocess::StdIOChannel subprocess_channel,
Subprocess::StdIOChannel parent_channel);
+ void SetVerbose(bool verbose);
+
// Starts execution of the command. This method can be called multiple times,
// effectively staring multiple (possibly concurrent) instances. If
// with_control_socket is true the returned Subprocess instance will have a
@@ -182,6 +184,7 @@ class Command {
bool use_parent_env_ = true;
std::vector<std::string> env_{};
SubprocessStopper subprocess_stopper_;
+ bool verbose_;
};
// Convenience wrapper around Command and Subprocess class, allows to easily