Skip to content

Commit 9699396

Browse files
committed
Call ShouldColor(Stderr|Stdout) in some places.
Bug: b/533162913
1 parent 131983e commit 9699396

8 files changed

Lines changed: 25 additions & 16 deletions

File tree

base/cvd/cuttlefish/host/commands/cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cf_cc_binary(
3030
srcs = ["main.cc"],
3131
deps = [
3232
":libcvd",
33+
"//cuttlefish/ansi_codes:should_color",
3334
"//cuttlefish/ansi_codes:terminal_colors",
3435
"//cuttlefish/common/libs/utils:environment",
3536
"//cuttlefish/common/libs/utils:files",

base/cvd/cuttlefish/host/commands/cvd/cli/commands/monitor/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cf_cc_library(
3333
":log_tee",
3434
":logcat",
3535
":truncate",
36+
"//cuttlefish/ansi_codes:should_color",
3637
"//cuttlefish/common/libs/utils:environment",
3738
"//cuttlefish/common/libs/utils:tee_logging",
3839
"//cuttlefish/host/libs/log_names",

base/cvd/cuttlefish/host/commands/cvd/cli/commands/monitor/display.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <stdint.h>
2020
#include <sys/types.h>
21-
#include <unistd.h>
2221

2322
#include <algorithm>
2423
#include <cstddef>
@@ -35,7 +34,7 @@
3534
#include "absl/strings/str_replace.h"
3635
#include "absl/strings/str_split.h"
3736

38-
#include "cuttlefish/common/libs/utils/environment.h"
37+
#include "cuttlefish/ansi_codes/should_color.h"
3938
#include "cuttlefish/common/libs/utils/tee_logging.h"
4039
#include "cuttlefish/host/commands/cvd/cli/commands/monitor/kernel.h"
4140
#include "cuttlefish/host/commands/cvd/cli/commands/monitor/launcher.h"
@@ -86,17 +85,10 @@ Result<std::vector<std::string>> GetLastNLines(ReaderSeeker& rs, size_t n) {
8685
return all_lines;
8786
}
8887

89-
bool ShouldColorizeOutput(int fd) {
90-
return !StringFromEnv("NO_COLOR").has_value() &&
91-
(StringFromEnv("FORCE_COLOR").has_value() || isatty(fd));
92-
}
93-
9488
} // namespace
9589

9690
LogMonitorDisplay::LogMonitorDisplay(size_t width)
97-
: width_(width),
98-
total_lines_drawn_(0),
99-
colorize_(ShouldColorizeOutput(1)) {}
91+
: width_(width), total_lines_drawn_(0), colorize_(ShouldColorStdout()) {}
10092

10193
void LogMonitorDisplay::DrawFile(ReaderSeeker& rs, const std::string& title,
10294
size_t max_lines) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "android-base/file.h"
3333
#include "fmt/format.h"
3434

35+
#include "cuttlefish/ansi_codes/should_color.h"
3536
#include "cuttlefish/ansi_codes/terminal_colors.h"
3637
#include "cuttlefish/common/libs/utils/environment.h"
3738
#include "cuttlefish/common/libs/utils/files.h"
@@ -168,7 +169,7 @@ Result<void> CvdMain(cvd_common::Args all_args) {
168169
* is red.
169170
*/
170171
std::string ColoredUrl(const std::string& url) {
171-
if (!isatty(STDERR_FILENO)) {
172+
if (!ShouldColorStderr()) {
172173
return url;
173174
}
174175
std::string coloring_prefix = "\033[01;31m";
@@ -236,7 +237,7 @@ int main(int argc, char** argv) {
236237
return 0;
237238
} else if (log_file.has_value() && isatty(2)) {
238239
VLOG(0) << result.error();
239-
cuttlefish::TerminalColors colors(isatty(2));
240+
cuttlefish::TerminalColors colors(cuttlefish::ShouldColorStderr());
240241
std::cerr << colors.Red() << "'cvd' encountered an error." << colors.Reset()
241242
<< " Please see '" << colors.Cyan() << *log_file << colors.Reset()
242243
<< "' for the complete failure report.\n";

base/cvd/cuttlefish/host/commands/run_cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ cf_cc_library(
4646
hdrs = ["reporting.h"],
4747
deps = [
4848
"//cuttlefish/ansi_codes",
49+
"//cuttlefish/ansi_codes:should_color",
4950
"//libbase",
5051
"@abseil-cpp//absl/log",
5152
"@fruit",

base/cvd/cuttlefish/host/commands/run_cvd/reporting.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,28 @@
2121
#include "absl/log/log.h"
2222

2323
#include "cuttlefish/ansi_codes/ansi_codes.h"
24+
#include "cuttlefish/ansi_codes/should_color.h"
2425

2526
namespace cuttlefish {
2627

2728
DiagnosticInformation::~DiagnosticInformation() = default;
2829

2930
void DiagnosticInformation::PrintAll(
3031
const std::vector<DiagnosticInformation*>& infos) {
31-
LOG(INFO) << kAnsiGreen << " Run `cvd logs` to report paths to device logs."
32-
<< kAnsiReset;
32+
static constexpr char kCvdLogsMsg[] =
33+
" Run `cvd logs` to report paths to device logs.";
34+
if (ShouldColorStderr()) {
35+
LOG(INFO) << kAnsiGreen << kCvdLogsMsg << kAnsiReset;
36+
} else {
37+
LOG(INFO) << kCvdLogsMsg;
38+
}
3339
for (const auto& info : infos) {
3440
for (const auto& line : info->Diagnostics()) {
35-
LOG(INFO) << kAnsiGreen << " " << line << kAnsiReset;
41+
if (ShouldColorStderr()) {
42+
LOG(INFO) << kAnsiGreen << " " << line << kAnsiReset;
43+
} else {
44+
LOG(INFO) << " " << line;
45+
}
3646
}
3747
}
3848
}

base/cvd/cuttlefish/result/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cf_cc_library(
1010
hdrs = ["error_type.h"],
1111
deps = [
1212
"//cuttlefish/ansi_codes",
13+
"//cuttlefish/ansi_codes:should_color",
1314
"//libbase",
1415
"@abseil-cpp//absl/log",
1516
"@abseil-cpp//absl/strings",

base/cvd/cuttlefish/result/error_type.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "android-base/expected.h" // IWYU pragma: export
2828
#include "fmt/core.h" // IWYU pragma: export
2929

30+
#include "cuttlefish/ansi_codes/should_color.h"
31+
3032
namespace cuttlefish {
3133

3234
class StackTraceError;
@@ -188,7 +190,7 @@ class StackTraceError {
188190

189191
std::string Trace() const { return fmt::format(fmt::runtime("{:v}"), *this); }
190192

191-
std::string FormatForEnv(bool color = (isatty(STDERR_FILENO) == 1)) const {
193+
std::string FormatForEnv(bool color = ShouldColorStdout()) const {
192194
return fmt::format(fmt::runtime(ResultErrorFormat(color)), *this);
193195
}
194196

0 commit comments

Comments
 (0)