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>
2223#include < vector>
@@ -41,57 +42,33 @@ Result<LogcatLine> ParseLogcatLine(std::string_view line) {
4142 // Note: We avoid absl::MaxSplits here because it counts delimiter matches
4243 // *before* absl::SkipWhitespace filters them, which breaks index alignment on
4344 // double-spaces.
44- std::vector<std::string_view> fields =
45+ const std::vector<std::string_view> fields =
4546 absl::StrSplit (line, absl::ByAnyChar (" \t " ), absl::SkipWhitespace ());
4647
47- CF_EXPECT (fields.size () > 5 , " Failed to parse Logcat line" );
48-
49- CF_EXPECT (fields[4 ].size () == 1 , " Invalid verbosity indicator" );
50- char verbosity = fields[4 ][0 ];
48+ CF_EXPECT_GT (fields.size (), 6 , " Failed to parse Logcat line" );
5149
52- std::string_view tag;
53- std::string_view message;
50+ CF_EXPECT_EQ (fields[4 ].size (), 1 , " Invalid verbosity indicator" );
5451
55- // Remainder starts at Field 5
56- const char * remainder_start = fields[5 ].data ();
52+ // Remainder starts at Field 6
53+ const char * remainder_start = fields[6 ].data ();
5754 const size_t remainder_len = line.data () + line.size () - remainder_start;
58- std::string_view remainder (remainder_start, remainder_len);
59-
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- }
7155
7256 return LogcatLine{
7357 .date = fields[0 ],
7458 .time = fields[1 ],
7559 .uid = fields[2 ],
7660 .pid = fields[3 ],
77- .verbosity = verbosity ,
78- .tag = tag ,
79- .message = message ,
61+ .verbosity = fields[ 4 ][ 0 ] ,
62+ .tag = fields[ 5 ] ,
63+ .message = std::string_view (remainder_start, remainder_len) ,
8064 };
8165}
8266
8367std::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;
68+ return absl::StrCat (kAnsiGreen , line.date , " " , line.time , " " ,
69+ GetColorForVerbosity (line.verbosity ), line.uid , " " ,
70+ line.pid , " " , std::string_view (&line.verbosity , 1 ), " " ,
71+ kAnsiYellow , line.tag , kAnsiReset , " " , line.message );
9572}
9673
9774} // namespace cuttlefish
0 commit comments