1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{error::*, types::AndroidSdk};
use std::process::Command;

/// Returns `adb logcat` command
fn logcat_cmd(sdk: &AndroidSdk) -> Result<Command> {
    let mut adb = sdk.platform_tool(bin!("adb"))?;
    adb.arg("logcat");
    Ok(adb)
}

/// Attach logger to device with filter that passes only Rust Stdout or Stderr.
/// Runs`adb logcat RustStdoutStderr:D '*:S'` command
pub fn attach_logger_only_rust(sdk: &AndroidSdk) -> Result<()> {
    let mut adb = logcat_cmd(sdk)?;
    adb.arg("RustStdoutStderr:D")
        .arg("SAPP:D")
        .arg("Crossbow:D")
        .arg("CrossbowPlugin:D")
        .arg("*:S");
    adb.spawn()?.wait()?;
    Ok(())
}

/// Attach logger to device with filter that filters only App Stdout or Stderr.
/// Runs`adb logcat --pid=`adb shell pidof -s com.crossbow.game`` command
pub fn attach_logger_only_app(sdk: &AndroidSdk) -> Result<()> {
    let mut adb_shell = sdk.platform_tool(bin!("adb"))?;
    adb_shell.args(["shell", "pidof", "-s", "com.crossbow.game"]);
    let res = adb_shell.output()?.stdout;
    let pid = String::from_utf8_lossy(&res).to_string();

    let mut adb = logcat_cmd(sdk)?;
    adb.arg("--pid").arg(pid.trim());
    adb.spawn()?.wait()?;
    Ok(())
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn test_attach_logger_only_app() -> Result<()> {
//         let sdk = AndroidSdk::from_env()?;
//         attach_logger_only_app(&sdk)?;
//         Ok(())
//     }
// }