Skip to content

Commit 415f008

Browse files
committed
Pass verbosity flag to subprocesses as environment variable
1 parent 3f2255a commit 415f008

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

base/cvd/cuttlefish/common/libs/utils/tee_logging.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ Result<LogSeverity> ToSeverity(const std::string& value) {
290290
}
291291
int value_int;
292292
CF_EXPECT(android::base::ParseInt(value, &value_int),
293-
"Unable to determine severity from \"" << value << "\"");
294-
for (const auto& [name, value]: string_to_severity) {
293+
"Unable to determine severity from \"" << value << "\"");
294+
for (const auto& [name, value] : string_to_severity) {
295295
if (static_cast<int>(value) == value_int) {
296296
return value;
297297
}
@@ -306,11 +306,16 @@ static LogSeverity GuessSeverity(const std::string& env_var,
306306
}
307307

308308
LogSeverity ConsoleSeverity() {
309-
return GuessSeverity("CF_CONSOLE_SEVERITY", LogSeverity::Info);
309+
return GuessSeverity(kConsoleSeverityEnvVar, LogSeverity::Info);
310310
}
311311

312312
LogSeverity LogFileSeverity() {
313-
return GuessSeverity("CF_FILE_SEVERITY", LogSeverity::Debug);
313+
LogSeverity severity = GuessSeverity(kFileSeverityEnvVar, LogSeverity::Debug);
314+
if (severity > LogSeverity::Debug) {
315+
// Debug or higher severity logs must always be written to files.
316+
return LogSeverity::Debug;
317+
}
318+
return severity;
314319
}
315320

316321
void SetLoggers(std::vector<SeverityTarget> destinations,

base/cvd/cuttlefish/common/libs/utils/tee_logging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
namespace cuttlefish {
2727

28+
constexpr const char* kConsoleSeverityEnvVar = "CF_CONSOLE_SEVERITY";
29+
constexpr const char* kFileSeverityEnvVar = "CF_FILE_SEVERITY";
30+
2831
enum class LogSeverity : int {
2932
Verbose = 0,
3033
Debug,

base/cvd/cuttlefish/host/commands/cvd/main.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
// TODO(315772518) Re-enable once metrics send is reenabled
4545
// #include "cuttlefish/host/commands/cvd/metrics/cvd_metrics_api.h"
4646

47+
extern char** environ;
48+
4749
namespace cuttlefish {
4850
namespace {
4951

@@ -70,6 +72,16 @@ LogSeverity CvdVerbosityOption(cvd_common::Args& all_args) {
7072
return LogSeverity::Info;
7173
}
7274
LogSeverity verbosity = *to_severity_res;
75+
// Set environment variables to the requested verbosity for subcommands and
76+
// subprocesses to use.
77+
if (int res = setenv(kConsoleSeverityEnvVar, verbosity_flag_value.c_str(), 1);
78+
res) {
79+
PLOG(ERROR) << "Failed to set console log severity environment variable";
80+
}
81+
if (int res = setenv(kFileSeverityEnvVar, verbosity_flag_value.c_str(), 1);
82+
res) {
83+
PLOG(ERROR) << "Failed to set file log severity environment variable";
84+
}
7385
return verbosity;
7486
}
7587

@@ -110,15 +122,15 @@ void IncreaseFileLimit() {
110122
}
111123
}
112124

113-
Result<void> CvdMain(cvd_common::Args all_args, char** envp) {
125+
Result<void> CvdMain(cvd_common::Args all_args) {
114126
if (!isatty(0)) {
115127
LOG(INFO) << GetVersionIds().ToString();
116128
}
117129
CF_EXPECT(EnsureCvdDirectoriesExist());
118130

119131
CF_EXPECT(!all_args.empty());
120132

121-
auto env = EnvpToMap(envp);
133+
auto env = EnvpToMap(environ);
122134
// TODO(315772518) Re-enable once metrics send is skipped in a env
123135
// without network support
124136
// CvdMetrics::SendCvdMetrics(all_args);
@@ -205,7 +217,7 @@ bool ValidateHostConfiguration() {
205217
} // namespace
206218
} // namespace cuttlefish
207219

208-
int main(int argc, char** argv, char** envp) {
220+
int main(int argc, char** argv) {
209221
srand(time(NULL));
210222

211223
cuttlefish::cvd_common::Args all_args = cuttlefish::ArgsToVec(argc, argv);
@@ -220,7 +232,7 @@ int main(int argc, char** argv, char** envp) {
220232
if (!cuttlefish::ValidateHostConfiguration()) {
221233
return -1;
222234
}
223-
auto result = cuttlefish::CvdMain(std::move(all_args), envp);
235+
auto result = cuttlefish::CvdMain(std::move(all_args));
224236
if (result.ok()) {
225237
return 0;
226238
} else {

0 commit comments

Comments
 (0)