xref: /aosp_15_r20/tools/asuite/adevice/src/main.rs (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1 //! Update an Android device with locally built changes.
2 mod adevice;
3 mod cli;
4 mod commands;
5 mod device;
6 mod fingerprint;
7 mod metrics;
8 mod progress;
9 mod restart_chooser;
10 mod tracking;
11 use tracing::{error, info};
12 
13 use crate::adevice::Profiler;
14 use crate::adevice::RealHost;
15 use crate::device::RealDevice;
16 use crate::metrics::MetricSender;
17 use crate::metrics::Metrics;
18 
19 use clap::Parser;
20 use std::fs::File;
21 use std::path::PathBuf;
22 
23 use anyhow::Result;
24 
main() -> Result<()>25 fn main() -> Result<()> {
26     let total_time = std::time::Instant::now();
27     let host = RealHost::new();
28     let cli = cli::Cli::parse();
29     let mut profiler = Profiler::default();
30     let device = RealDevice::new(cli.global_options.serial.clone());
31     let mut metrics = Metrics::default();
32     let result = crate::adevice::adevice(
33         &host,
34         &device,
35         &cli,
36         &mut std::io::stdout(),
37         &mut metrics,
38         log_file(),
39         &mut profiler,
40     );
41 
42     // cleanup tasks (metrics, profiling)
43     match result {
44         Ok(()) => metrics.add_exit_event("", 0),
45         Err(ref err) => {
46             progress::stop();
47             metrics.add_exit_event(&err.to_string(), 1);
48             error!("\n{}", err.to_string());
49         }
50     }
51     progress::stop();
52     profiler.total = total_time.elapsed();
53     metrics.add_profiler_events(&profiler);
54     println!(
55         "\nFinished in {} secs, [Logfile at $ANDROID_BUILD_TOP/out/adevice.log]",
56         profiler.total.as_secs()
57     );
58     info!("TIMING: {}", profiler.to_string());
59 
60     result
61 }
62 
63 /// Return a file open at $ANDROID_BUILD_TOP/out/adevice.log or None
64 /// Ideally, use the file_rotate crate: https://docs.rs/file-rotate/latest/file_rotate/ as well.
log_file() -> Option<File>65 fn log_file() -> Option<File> {
66     match std::env::var("ANDROID_BUILD_TOP") {
67         Ok(top) if !top.is_empty() => {
68             let path = PathBuf::from(top).join("out").join("adevice.log");
69             File::create(path).ok()
70         }
71         _ => None,
72     }
73 }
74