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
8374std::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
0 commit comments