Skip to content

Commit a190a1b

Browse files
committed
Drop logcat special handling for lines without tags
Looking at some logcat output in practice, I don't see any lines without tags. The special handling for this case is needless complexity.
1 parent e33440e commit a190a1b

2 files changed

Lines changed: 16 additions & 45 deletions

File tree

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

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
#include "cuttlefish/host/commands/cvd/cli/commands/monitor/logcat.h"
1818

19-
#include <cstddef>
19+
#include <stddef.h>
20+
2021
#include <string>
2122
#include <string_view>
23+
#include <utility>
2224
#include <vector>
2325

26+
#include "absl/strings/ascii.h"
2427
#include "absl/strings/str_cat.h"
2528
#include "absl/strings/str_split.h"
2629

@@ -41,57 +44,38 @@ Result<LogcatLine> ParseLogcatLine(std::string_view line) {
4144
// Note: We avoid absl::MaxSplits here because it counts delimiter matches
4245
// *before* absl::SkipWhitespace filters them, which breaks index alignment on
4346
// double-spaces.
44-
std::vector<std::string_view> fields =
47+
const std::vector<std::string_view> fields =
4548
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipWhitespace());
4649

4750
CF_EXPECT(fields.size() > 5, "Failed to parse Logcat line");
4851

4952
CF_EXPECT(fields[4].size() == 1, "Invalid verbosity indicator");
50-
char verbosity = fields[4][0];
51-
52-
std::string_view tag;
53-
std::string_view message;
53+
const char verbosity = fields[4][0];
5454

5555
// Remainder starts at Field 5
5656
const char* remainder_start = fields[5].data();
5757
const size_t remainder_len = line.data() + line.size() - remainder_start;
58-
std::string_view remainder(remainder_start, remainder_len);
58+
const std::string_view remainder(remainder_start, remainder_len);
5959

60-
size_t colon = remainder.find(':');
61-
if (colon == std::string_view::npos) {
62-
tag = "";
63-
message = remainder;
64-
} else {
65-
tag = remainder.substr(0, colon + 1); // "TagName:"
66-
message = remainder.substr(colon + 1);
67-
if (!message.empty() && (message[0] == ' ' || message[0] == '\t')) {
68-
message.remove_prefix(1);
69-
}
70-
}
60+
std::pair<std::string_view, std::string_view> tag_message =
61+
absl::StrSplit(remainder, absl::MaxSplits(':', 1));
7162

7263
return LogcatLine{
7364
.date = fields[0],
7465
.time = fields[1],
7566
.uid = fields[2],
7667
.pid = fields[3],
7768
.verbosity = verbosity,
78-
.tag = tag,
79-
.message = message,
69+
.tag = absl::StripAsciiWhitespace(tag_message.first),
70+
.message = absl::StripAsciiWhitespace(tag_message.second),
8071
};
8172
}
8273

8374
std::string FormatLogcatLine(const LogcatLine& line) {
84-
const std::string_view verb_color = GetColorForVerbosity(line.verbosity);
85-
std::string result = absl::StrCat(kAnsiGreen, line.date, " ", line.time, " ",
86-
verb_color, line.uid, " ", line.pid, " ",
87-
std::string_view(&line.verbosity, 1), " ");
88-
89-
if (!line.tag.empty()) {
90-
absl::StrAppend(&result, kAnsiYellow, line.tag, " ");
91-
}
92-
93-
absl::StrAppend(&result, kAnsiReset, line.message);
94-
return result;
75+
return absl::StrCat(kAnsiGreen, line.date, " ", line.time, " ",
76+
GetColorForVerbosity(line.verbosity), line.uid, " ",
77+
line.pid, " ", std::string_view(&line.verbosity, 1), " ",
78+
kAnsiYellow, line.tag, ": ", kAnsiReset, line.message);
9579
}
9680

9781
} // namespace cuttlefish

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,10 @@ TEST(LogcatTest, ParseLogcatLineValid) {
3434
EXPECT_EQ(parsed->uid, "1000");
3535
EXPECT_EQ(parsed->pid, "1000");
3636
EXPECT_EQ(parsed->verbosity, 'I');
37-
EXPECT_EQ(parsed->tag, "TagName:");
37+
EXPECT_EQ(parsed->tag, "TagName");
3838
EXPECT_EQ(parsed->message, "message");
3939
}
4040

41-
TEST(LogcatTest, ParseLogcatLineNoColon) {
42-
std::string line = "05-15 15:28:15.123 1000 1000 I message without colon";
43-
auto parsed = ParseLogcatLine(line);
44-
ASSERT_THAT(parsed, IsOk());
45-
EXPECT_EQ(parsed->date, "05-15");
46-
EXPECT_EQ(parsed->time, "15:28:15.123");
47-
EXPECT_EQ(parsed->uid, "1000");
48-
EXPECT_EQ(parsed->pid, "1000");
49-
EXPECT_EQ(parsed->verbosity, 'I');
50-
EXPECT_EQ(parsed->tag, "");
51-
EXPECT_EQ(parsed->message, "message without colon");
52-
}
53-
5441
TEST(LogcatTest, ParseLogcatLineInvalid) {
5542
EXPECT_THAT(ParseLogcatLine("Failed to read logcat:"), IsError());
5643
EXPECT_THAT(ParseLogcatLine("short line"), IsError());

0 commit comments

Comments
 (0)