Skip to content

Commit 19202cc

Browse files
committed
adb: Use current date/time instead of last logcat line for timestamp
We have some phones, including the Vivo X90 Pro which return an empty string for `logcat -t 1`, resulting in an `unwrap()` crash before starting logging. There doesn't appear to be any reason to check the time of the latest **random** (not even `--uid`-scoped) message in `logcat` as we can just read the current date and time as well. We haven't started the app yet either way, and this timestamp is inevitably already going to be later than the most recent message. Also remove some unnecessary shell escaping (which should have been done by `shlex` otherwise) by calling `adb logcat` rather than `adb shell logcat`.
1 parent 7ef8e49 commit 19202cc

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

xbuild/src/devices/adb.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,21 @@ impl Adb {
197197
Ok(())
198198
}
199199

200-
fn logcat_last_timestamp(&self, device: &str) -> Result<String> {
200+
/// Returns the current device date and time in logcat timestamp format
201+
fn current_date_time(&self, device: &str) -> Result<String> {
201202
let output = self
202203
.shell(device, None)
203-
.arg("logcat")
204-
.arg("-v")
205-
.arg("time")
206-
.arg("-t")
207-
.arg("1")
204+
.arg("date")
205+
// Escape string so that `adb` passes it as-is to `date`
206+
.arg("'+%m-%d %T.000'")
208207
.output()?;
209208
anyhow::ensure!(
210209
output.status.success(),
211-
"adb logcat exited with code {:?}: {}",
210+
"`adb shell date` exited with code {:?}: {}",
212211
output.status.code(),
213212
std::str::from_utf8(&output.stderr)?.trim()
214213
);
215-
let line = std::str::from_utf8(&output.stdout)?.lines().nth(1).unwrap();
216-
Ok(line[..18].to_string())
214+
Ok(std::str::from_utf8(&output.stdout)?.trim().to_owned())
217215
}
218216

219217
fn uidof(&self, device: &str, id: &str) -> Result<u32> {
@@ -244,13 +242,13 @@ impl Adb {
244242
Ok(uid.parse()?)
245243
}
246244

247-
fn logcat(&self, device: &str, uid: u32, last_timestamp: &str) -> Result<Logcat> {
245+
fn logcat(&self, device: &str, uid: u32, since: &str) -> Result<Logcat> {
248246
let child = self
249-
.shell(device, None)
247+
.adb(device)
250248
.arg("logcat")
251249
.arg("-T")
252-
.arg(format!("'{last_timestamp}'"))
253250
.arg(format!("--uid={uid}"))
251+
.arg(since)
254252
.stdin(Stdio::null())
255253
.stdout(Stdio::piped())
256254
.spawn()?;
@@ -366,10 +364,10 @@ impl Adb {
366364
}
367365
self.install(device, path)?;
368366
self.forward_reverse(device, debug_config)?;
369-
let last_timestamp = self.logcat_last_timestamp(device)?;
367+
let since = self.current_date_time(device)?;
370368
self.start(device, package, activity, launch_args)?;
371369
let uid = self.uidof(device, package)?;
372-
let logcat = self.logcat(device, uid, &last_timestamp)?;
370+
let logcat = self.logcat(device, uid, &since)?;
373371
for line in logcat {
374372
println!("{line}");
375373
}

0 commit comments

Comments
 (0)