From dee9460f9c9bc92e9c638e7691e616849ac2085b Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 27 Dec 2022 13:12:01 -0800 Subject: [PATCH] feat: keep CLI alive when iOS app exits, show logs, closes #5855 (#5902) --- .changes/ios-keep-alive.md | 6 ++++ .changes/ios-logs.md | 7 ++++ .changes/mobile-env-vars-rename.md | 6 ++++ core/tauri-build/src/lib.rs | 10 ++++-- core/tauri-macros/src/mobile.rs | 7 ++-- core/tauri/Cargo.toml | 4 ++- core/tauri/src/lib.rs | 40 ++++++++++++++++++++-- examples/api/src-tauri/Cargo.lock | 54 +++++++++++++++++------------- tooling/cli/Cargo.lock | 4 +-- tooling/cli/Cargo.toml | 2 +- tooling/cli/src/mobile/ios/dev.rs | 4 +-- 11 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 .changes/ios-keep-alive.md create mode 100644 .changes/ios-logs.md create mode 100644 .changes/mobile-env-vars-rename.md diff --git a/.changes/ios-keep-alive.md b/.changes/ios-keep-alive.md new file mode 100644 index 000000000..2313abdd8 --- /dev/null +++ b/.changes/ios-keep-alive.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +Keep the process alive even when the iOS application is closed. diff --git a/.changes/ios-logs.md b/.changes/ios-logs.md new file mode 100644 index 000000000..52493a06f --- /dev/null +++ b/.changes/ios-logs.md @@ -0,0 +1,7 @@ +--- +"cli.rs": patch +"cli.js": patch +"tauri": patch +--- + +Show all application logs on iOS. diff --git a/.changes/mobile-env-vars-rename.md b/.changes/mobile-env-vars-rename.md new file mode 100644 index 000000000..d977243e1 --- /dev/null +++ b/.changes/mobile-env-vars-rename.md @@ -0,0 +1,6 @@ +--- +"tauri-build": patch +"tauri-macros": patch +--- + +Refactor mobile environment variables. diff --git a/core/tauri-build/src/lib.rs b/core/tauri-build/src/lib.rs index 09469f9d9..5e62e9495 100644 --- a/core/tauri-build/src/lib.rs +++ b/core/tauri-build/src/lib.rs @@ -222,14 +222,20 @@ pub fn try_build(attributes: Attributes) -> Result<()> { let s = config.tauri.bundle.identifier.split('.'); let last = s.clone().count() - 1; let mut domain = String::new(); + let mut android_package_prefix = String::new(); for (i, w) in s.enumerate() { if i != last { domain.push_str(w); - domain.push('_'); + domain.push('.'); + + android_package_prefix.push_str(w); + android_package_prefix.push('_'); } } domain.pop(); - println!("cargo:rustc-env=TAURI_ANDROID_DOMAIN={domain}"); + android_package_prefix.pop(); + println!("cargo:rustc-env=TAURI_MOBILE_DOMAIN={domain}"); + println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_PREFIX={android_package_prefix}"); cfg_alias("dev", !has_feature("custom-protocol")); diff --git a/core/tauri-macros/src/mobile.rs b/core/tauri-macros/src/mobile.rs index 44bd177fb..4502b0d6f 100644 --- a/core/tauri-macros/src/mobile.rs +++ b/core/tauri-macros/src/mobile.rs @@ -33,13 +33,14 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream { let function_name = function.sig.ident.clone(); let mut error = None; - let domain = get_env_var("TAURI_ANDROID_DOMAIN", |r| r, &mut error, &function); + let domain = get_env_var("TAURI_ANDROID_PACKAGE_PREFIX", |r| r, &mut error, &function); let app_name = get_env_var( "CARGO_PKG_NAME", |r| r.replace('_', "_1"), &mut error, &function, ); + let domain_str = var("TAURI_MOBILE_DOMAIN").unwrap(); let app_name_str = var("CARGO_PKG_NAME").unwrap(); if let Some(e) = error { @@ -59,9 +60,11 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream { #function fn _start_app() { - ::tauri::init_logging(#app_name_str); + #[cfg(target_os = "ios")] + ::tauri::init_logging(&format!("{}.{}", #domain_str, #app_name_str)); #[cfg(target_os = "android")] { + ::tauri::init_logging(#app_name_str); use ::tauri::paste; ::tauri::wry_android_binding!(#domain, #app_name, _start_app, ::tauri::wry); } diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 7253ecf41..7215cdb17 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -114,7 +114,9 @@ android_logger = "0.9" log = "0.4" [target."cfg(target_os = \"ios\")".dependencies] -env_logger = "0.9.0" +oslog = "0.2" +log = "0.4" +libc = "0.2" [build-dependencies] heck = "0.4" diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index e14b1bd5c..d09e7a61f 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -290,8 +290,44 @@ pub fn init_logging(tag: &str) { #[cfg(target_os = "ios")] #[doc(hidden)] -pub fn init_logging(_tag: &str) { - env_logger::init(); +pub fn init_logging(subsystem: &str) { + use std::{ + ffi::CString, + fs::File, + io::{BufRead, BufReader}, + os::unix::prelude::*, + thread, + }; + + let mut logpipe: [RawFd; 2] = Default::default(); + unsafe { + libc::pipe(logpipe.as_mut_ptr()); + libc::dup2(logpipe[1], libc::STDOUT_FILENO); + libc::dup2(logpipe[1], libc::STDERR_FILENO); + } + thread::spawn(move || unsafe { + let file = File::from_raw_fd(logpipe[0]); + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + loop { + buffer.clear(); + if let Ok(len) = reader.read_line(&mut buffer) { + if len == 0 { + break; + } else if let Ok(msg) = CString::new(buffer.clone()) + .map_err(|_| ()) + .and_then(|c| c.into_string().map_err(|_| ())) + { + log::info!("{}", msg); + } + } + } + }); + + oslog::OsLogger::new(subsystem) + .level_filter(log::LevelFilter::Trace) + .init() + .unwrap(); } /// Updater events. diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 5c66e3081..1a48aaa4f 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -80,7 +80,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ec2333c185d826313162cee39d3fcc6a84ba08114a839bebf53b961e7e75773" dependencies = [ "android_log-sys", - "env_logger 0.7.1", + "env_logger", "lazy_static", "log", ] @@ -610,6 +610,19 @@ dependencies = [ "syn", ] +[[package]] +name = "dashmap" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc" +dependencies = [ + "cfg-if", + "hashbrown", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "dbus" version = "0.9.6" @@ -717,19 +730,6 @@ dependencies = [ "regex", ] -[[package]] -name = "env_logger" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "fastrand" version = "1.8.0" @@ -1279,12 +1279,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.23" @@ -1941,6 +1935,17 @@ version = "6.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3baf96e39c5359d2eb0dd6ccb42c62b91d9678aa68160d261b9e0ccbf9e9dea9" +[[package]] +name = "oslog" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d2043d1f61d77cb2f4b1f7b7b2295f40507f5f8e9d1c8bf10a1ca5f97a3969" +dependencies = [ + "cc", + "dashmap", + "log", +] + [[package]] name = "overload" version = "0.1.1" @@ -2935,7 +2940,7 @@ dependencies = [ [[package]] name = "tauri" -version = "2.0.0-alpha.0" +version = "2.0.0-alpha.2" dependencies = [ "android_logger", "anyhow", @@ -2947,7 +2952,6 @@ dependencies = [ "dirs-next", "embed_plist", "encoding_rs", - "env_logger 0.9.3", "flate2", "futures-util", "glib", @@ -2958,6 +2962,7 @@ dependencies = [ "ico", "ignore", "infer 0.9.0", + "libc", "log", "minisign-verify", "notify-rust", @@ -2966,6 +2971,7 @@ dependencies = [ "open", "os_info", "os_pipe", + "oslog", "paste", "percent-encoding", "png", @@ -4037,9 +4043,9 @@ dependencies = [ [[package]] name = "wry" -version = "0.23.1" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a76c9236a810d4af02213f89f5bc55bf3262d40c4407b13a9fc847156ef8b856" +checksum = "4c1ad8e2424f554cc5bdebe8aa374ef5b433feff817aebabca0389961fc7ef98" dependencies = [ "base64", "block", diff --git a/tooling/cli/Cargo.lock b/tooling/cli/Cargo.lock index 7cb952d53..3052a8a88 100644 --- a/tooling/cli/Cargo.lock +++ b/tooling/cli/Cargo.lock @@ -3908,9 +3908,9 @@ dependencies = [ [[package]] name = "tauri-mobile" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b23191ab3de2efc8d266ec26c16c1c90bd9e171f6f8ccdc56cd31cc9ad97130" +checksum = "723e99ec18695c25936deb0bc4e271ff4d25f48de88d03d59959fe8a31d9bf91" dependencies = [ "cocoa", "colored 1.9.3", diff --git a/tooling/cli/Cargo.toml b/tooling/cli/Cargo.toml index 0616cae2b..498816db6 100644 --- a/tooling/cli/Cargo.toml +++ b/tooling/cli/Cargo.toml @@ -39,7 +39,7 @@ name = "cargo-tauri" path = "src/main.rs" [dependencies] -tauri-mobile = { version = "0.1", default-features = false } +tauri-mobile = { version = "0.1.1", default-features = false } textwrap = { version = "0.11.0", features = [ "term_size" ] } jsonrpsee = { version = "0.16", features = [ "client", "server" ] } thiserror = "1" diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index 1c2ce58bf..ad3bb6bdb 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -189,15 +189,13 @@ fn run( Profile::Release }; - let non_interactive = true; // ios-deploy --noninteractive (quit when app crashes or exits) - device_prompt(env, device) .map_err(|e| RunError::FailedToPromptForDevice(e.to_string()))? .run( config, env, NoiseLevel::FranklyQuitePedantic, - non_interactive, + false, // do not quit on app exit profile, ) .map(DevChild::new)