system/corennnnn
Révision | e2e1a1002692f58630c4313e0bbcc72c29ad12f2 (tree) |
---|---|
l'heure | 2016-08-05 05:15:18 |
Auteur | Felipe Leme <felipeal@goog...> |
Commiter | gitbuildkicker |
Minor improvements on bugreport generation.
- Skipped artificial 100/100 message, since pulling will take care of
- Consolidated unsupported lines in just one message.
- Let user know the bugreport can still be recovered when it could not
BUG: 30451114
Change-Id: Icfce9c1e8e7ed407719728b9874679ac40b21eab
@@ -15,6 +15,7 @@ | ||
15 | 15 | */ |
16 | 16 | |
17 | 17 | #include <string> |
18 | +#include <vector> | |
18 | 19 | |
19 | 20 | #include <android-base/strings.h> |
20 | 21 |
@@ -31,10 +32,12 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface | ||
31 | 32 | public: |
32 | 33 | BugreportStandardStreamsCallback(const std::string& dest_file, bool show_progress, Bugreport* br) |
33 | 34 | : br_(br), |
35 | + src_file_(), | |
34 | 36 | dest_file_(dest_file), |
35 | 37 | line_message_(android::base::StringPrintf("generating %s", dest_file_.c_str())), |
38 | + invalid_lines_(), | |
36 | 39 | show_progress_(show_progress), |
37 | - status_(-1), | |
40 | + status_(0), | |
38 | 41 | line_() { |
39 | 42 | } |
40 | 43 |
@@ -54,9 +57,39 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface | ||
54 | 57 | OnStream(nullptr, stderr, buffer, length); |
55 | 58 | } |
56 | 59 | int Done(int unused_) { |
57 | - // Process remaining line, if any... | |
60 | + // Process remaining line, if any. | |
58 | 61 | ProcessLine(line_); |
59 | - // ..then return. | |
62 | + | |
63 | + // Warn about invalid lines, if any. | |
64 | + if (!invalid_lines_.empty()) { | |
65 | + fprintf(stderr, | |
66 | + "WARNING: bugreportz generated %zu line(s) with unknown commands, " | |
67 | + "device might not support zipped bugreports:\n", | |
68 | + invalid_lines_.size()); | |
69 | + for (const auto& line : invalid_lines_) { | |
70 | + fprintf(stderr, "\t%s\n", line.c_str()); | |
71 | + } | |
72 | + fprintf(stderr, | |
73 | + "If the zipped bugreport was not generated, try 'adb bugreport' instead.\n"); | |
74 | + } | |
75 | + | |
76 | + // Pull the generated bug report. | |
77 | + if (status_ == 0) { | |
78 | + if (src_file_.empty()) { | |
79 | + fprintf(stderr, "bugreportz did not return a '%s' or '%s' line\n", BUGZ_OK_PREFIX, | |
80 | + BUGZ_FAIL_PREFIX); | |
81 | + return -1; | |
82 | + } | |
83 | + std::vector<const char*> srcs{src_file_.c_str()}; | |
84 | + status_ = br_->DoSyncPull(srcs, dest_file_.c_str(), true, line_message_.c_str()) ? 0 : 1; | |
85 | + if (status_ != 0) { | |
86 | + fprintf(stderr, | |
87 | + "Bug report finished but could not be copied to '%s'.\n" | |
88 | + "Try to run 'adb pull %s <directory>'\n" | |
89 | + "to copy it to a directory that can be written.\n", | |
90 | + dest_file_.c_str(), src_file_.c_str()); | |
91 | + } | |
92 | + } | |
60 | 93 | return status_; |
61 | 94 | } |
62 | 95 |
@@ -65,17 +98,7 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface | ||
65 | 98 | if (line.empty()) return; |
66 | 99 | |
67 | 100 | if (android::base::StartsWith(line, BUGZ_OK_PREFIX)) { |
68 | - if (show_progress_) { | |
69 | - // Make sure pull message doesn't conflict with generation message. | |
70 | - br_->UpdateProgress(line_message_, 100, 100); | |
71 | - } | |
72 | - | |
73 | - const char* zip_file = &line[strlen(BUGZ_OK_PREFIX)]; | |
74 | - std::vector<const char*> srcs{zip_file}; | |
75 | - status_ = br_->DoSyncPull(srcs, dest_file_.c_str(), true, line_message_.c_str()) ? 0 : 1; | |
76 | - if (status_ != 0) { | |
77 | - fprintf(stderr, "Could not copy file '%s' to '%s'\n", zip_file, dest_file_.c_str()); | |
78 | - } | |
101 | + src_file_ = &line[strlen(BUGZ_OK_PREFIX)]; | |
79 | 102 | } else if (android::base::StartsWith(line, BUGZ_FAIL_PREFIX)) { |
80 | 103 | const char* error_message = &line[strlen(BUGZ_FAIL_PREFIX)]; |
81 | 104 | fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message); |
@@ -91,17 +114,15 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface | ||
91 | 114 | int total = std::stoi(line.substr(idx2 + 1)); |
92 | 115 | br_->UpdateProgress(dest_file_, progress, total); |
93 | 116 | } else { |
94 | - fprintf(stderr, | |
95 | - "WARNING: unexpected line (%s) returned by bugreportz, " | |
96 | - "device probably does not support zipped bugreports.\n" | |
97 | - "Try 'adb bugreport' instead.", | |
98 | - line.c_str()); | |
117 | + invalid_lines_.push_back(line); | |
99 | 118 | } |
100 | 119 | } |
101 | 120 | |
102 | 121 | Bugreport* br_; |
122 | + std::string src_file_; | |
103 | 123 | const std::string dest_file_; |
104 | 124 | const std::string line_message_; |
125 | + std::vector<std::string> invalid_lines_; | |
105 | 126 | bool show_progress_; |
106 | 127 | int status_; |
107 | 128 |
@@ -132,18 +153,15 @@ int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, | ||
132 | 153 | std::string bugz_stderr; |
133 | 154 | DefaultStandardStreamsCallback version_callback(nullptr, &bugz_stderr); |
134 | 155 | int status = SendShellCommand(transport_type, serial, "bugreportz -v", false, &version_callback); |
156 | + std::string bugz_version = android::base::Trim(bugz_stderr); | |
135 | 157 | |
136 | - if (status != 0) { | |
158 | + if (status != 0 || bugz_version.empty()) { | |
137 | 159 | fprintf(stderr, |
138 | - "Failed to get bugreportz version: 'bugreport -v' returned '%s' " | |
139 | - "(code %d)." | |
140 | - "\nIf the device does not support it, try running 'adb bugreport' " | |
141 | - "to get a " | |
142 | - "flat-file bugreport.", | |
160 | + "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n" | |
161 | + "If the device runs Android M or below, try 'adb bugreport' instead.\n", | |
143 | 162 | bugz_stderr.c_str(), status); |
144 | - return status; | |
163 | + return status != 0 ? status : -1; | |
145 | 164 | } |
146 | - std::string bugz_version = android::base::Trim(bugz_stderr); | |
147 | 165 | |
148 | 166 | bool show_progress = true; |
149 | 167 | std::string bugz_command = "bugreportz -p"; |
@@ -153,8 +171,7 @@ int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, | ||
153 | 171 | fprintf(stderr, |
154 | 172 | "Bugreport is in progress and it could take minutes to complete.\n" |
155 | 173 | "Please be patient and do not cancel or disconnect your device " |
156 | - "until it completes." | |
157 | - "\n"); | |
174 | + "until it completes.\n"); | |
158 | 175 | show_progress = false; |
159 | 176 | bugz_command = "bugreportz"; |
160 | 177 | } |
@@ -189,14 +189,14 @@ TEST_F(BugreportTest, Ok) { | ||
189 | 189 | ExpectProgress(10, 100); |
190 | 190 | ExpectProgress(50, 100); |
191 | 191 | ExpectProgress(99, 100); |
192 | - ExpectProgress(100, 100); | |
193 | 192 | // clang-format off |
194 | 193 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
194 | + // NOTE: DoAll accepts at most 10 arguments, and we have reached that limit... | |
195 | 195 | .WillOnce(DoAll( |
196 | 196 | // Progress line in one write |
197 | 197 | WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")), |
198 | 198 | // Add some bogus lines |
199 | - WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\n")), | |
199 | + WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\nBLA\n\nBLA\nBLA\n\n")), | |
200 | 200 | // Multiple progress lines in one write |
201 | 201 | WithArg<4>(WriteOnStdout("PROGRESS:10/100\nPROGRESS:50/100\n")), |
202 | 202 | // Progress line in multiple writes |
@@ -207,6 +207,7 @@ TEST_F(BugreportTest, Ok) { | ||
207 | 207 | WithArg<4>(WriteOnStdout("OK:/device/bugreport")), |
208 | 208 | WithArg<4>(WriteOnStdout(".zip")), |
209 | 209 | WithArg<4>(ReturnCallbackDone()))); |
210 | + // clang-format on | |
210 | 211 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
211 | 212 | true, HasSubstr("file.zip"))) |
212 | 213 | .WillOnce(Return(true)); |
@@ -218,7 +219,6 @@ TEST_F(BugreportTest, Ok) { | ||
218 | 219 | // Tests 'adb bugreport file' when it succeeds |
219 | 220 | TEST_F(BugreportTest, OkNoExtension) { |
220 | 221 | SetBugreportzVersion("1.1"); |
221 | - ExpectProgress(100, 100); | |
222 | 222 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
223 | 223 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip\n")), |
224 | 224 | WithArg<4>(ReturnCallbackDone()))); |
@@ -281,6 +281,14 @@ TEST_F(BugreportTest, BugreportzVersionFailed) { | ||
281 | 281 | ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
282 | 282 | } |
283 | 283 | |
284 | +// Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output. | |
285 | +TEST_F(BugreportTest, BugreportzVersionEmpty) { | |
286 | + SetBugreportzVersion(""); | |
287 | + | |
288 | + const char* args[1024] = {"bugreport", "file.zip"}; | |
289 | + ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); | |
290 | +} | |
291 | + | |
284 | 292 | // Tests 'adb bugreport file.zip' when the main bugreportz command failed |
285 | 293 | TEST_F(BugreportTest, BugreportzFailed) { |
286 | 294 | SetBugreportzVersion("1.1"); |
@@ -294,7 +302,6 @@ TEST_F(BugreportTest, BugreportzFailed) { | ||
294 | 302 | // Tests 'adb bugreport file.zip' when the bugreport could not be pulled |
295 | 303 | TEST_F(BugreportTest, PullFails) { |
296 | 304 | SetBugreportzVersion("1.1"); |
297 | - ExpectProgress(100, 100); | |
298 | 305 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
299 | 306 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")), |
300 | 307 | WithArg<4>(ReturnCallbackDone()))); |