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>
2324
25+ #include " absl/strings/ascii.h"
2426#include " absl/strings/str_cat.h"
2527#include " absl/strings/str_split.h"
2628
@@ -41,57 +43,34 @@ Result<LogcatLine> ParseLogcatLine(std::string_view line) {
4143 // Note: We avoid absl::MaxSplits here because it counts delimiter matches
4244 // *before* absl::SkipWhitespace filters them, which breaks index alignment on
4345 // double-spaces.
44- std::vector<std::string_view> fields =
46+ const std::vector<std::string_view> fields =
4547 absl::StrSplit (line, absl::ByAnyChar (" \t " ), absl::SkipWhitespace ());
4648
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 ];
49+ CF_EXPECT_GT (fields.size (), 6 , " Failed to parse Logcat line" );
5150
52- std::string_view tag;
53- std::string_view message;
51+ CF_EXPECT_EQ (fields[4 ].size (), 1 , " Invalid verbosity indicator" );
5452
55- // Remainder starts at Field 5
56- const char * remainder_start = fields[5 ].data ();
53+ // Remainder starts at Field 6
54+ const char * remainder_start = fields[6 ].data ();
5755 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- }
56+ const std::string_view remainder (remainder_start, remainder_len);
7157
7258 return LogcatLine{
7359 .date = fields[0 ],
7460 .time = fields[1 ],
7561 .uid = fields[2 ],
7662 .pid = fields[3 ],
77- .verbosity = verbosity ,
78- .tag = tag ,
79- .message = message ,
63+ .verbosity = fields[ 4 ][ 0 ] ,
64+ .tag = fields[ 5 ] ,
65+ .message = absl::StripTrailingAsciiWhitespace (remainder) ,
8066 };
8167}
8268
8369std::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;
70+ return absl::StrCat (kAnsiGreen , line.date , " " , line.time , " " ,
71+ GetColorForVerbosity (line.verbosity ), line.uid , " " ,
72+ line.pid , " " , std::string_view (&line.verbosity , 1 ), " " ,
73+ kAnsiYellow , line.tag , kAnsiReset , " " , line.message );
9574}
9675
9776} // namespace cuttlefish
0 commit comments