From 42037a7a99e1796d10fe4e84f4f9b69c49334ee2 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 16 Jan 2023 17:47:43 -0300 Subject: [PATCH 01/12] feat(log): implement mobile logging --- Cargo.lock | 290 +++++++++++++++++++++++++++++++---------- plugins/log/Cargo.toml | 13 +- plugins/log/build.rs | 12 ++ plugins/log/src/lib.rs | 228 +++++++++++++++++++++++--------- 4 files changed, 416 insertions(+), 127 deletions(-) create mode 100644 plugins/log/build.rs diff --git a/Cargo.lock b/Cargo.lock index bb4c2849..847a616e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,7 +91,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", ] @@ -956,6 +956,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 1.0.0", + "hashbrown", + "lock_api", + "once_cell", + "parking_lot_core 0.9.6", +] + [[package]] name = "der" version = "0.5.1" @@ -1161,19 +1174,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 = "event-listener" version = "2.5.3" @@ -1882,12 +1882,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" @@ -1977,10 +1971,11 @@ dependencies = [ [[package]] name = "ignore" -version = "0.4.19" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a05705bc64e0b66a806c3740bd6578ea66051b157ec42dc219c785cbf185aef3" +checksum = "713f1b139373f96a2e0ce3ac931cd01ee973c3c5dd7c40c0c2efe96ad2b6751d" dependencies = [ + "crossbeam-utils", "globset", "lazy_static", "log", @@ -2848,6 +2843,17 @@ dependencies = [ "pin-project-lite", ] +[[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" @@ -4229,9 +4235,9 @@ dependencies = [ [[package]] name = "tao" -version = "0.15.8" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac8e6399427c8494f9849b58694754d7cc741293348a6836b6c8d2c5aa82d8e6" +checksum = "704522803dda895767f69198af8351b0a3f4fe2e293c3ca54cce0ecba05a97f2" dependencies = [ "bitflags", "cairo-rs", @@ -4263,11 +4269,11 @@ dependencies = [ "objc", "once_cell", "parking_lot 0.12.1", - "paste", "png", "raw-window-handle", "scopeguard", "serde", + "tao-macros", "unicode-segmentation", "uuid 1.2.2", "windows 0.39.0", @@ -4275,6 +4281,17 @@ dependencies = [ "x11-dl", ] +[[package]] +name = "tao-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b6fcd8245d45a39ffc8715183d92ae242750eb57b285eb3bcd63dfd512afd09" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tar" version = "0.4.38" @@ -4289,8 +4306,54 @@ dependencies = [ [[package]] name = "tauri" version = "2.0.0-alpha.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8dca2d7a5853ab98d1453bf74078308f175634934e2fd1f427433c56c179da5" +dependencies = [ + "anyhow", + "attohttpc", + "cocoa", + "dirs-next", + "embed_plist", + "encoding_rs", + "flate2", + "futures-util", + "glib", + "glob", + "gtk", + "heck 0.4.0", + "http", + "ignore", + "libc", + "log", + "objc", + "once_cell", + "paste", + "percent-encoding", + "rand 0.8.5", + "raw-window-handle", + "semver 1.0.16", + "serde", + "serde_json", + "serde_repr", + "serialize-to-javascript", + "state", + "tar", + "tauri-macros 2.0.0-alpha.0", + "tauri-runtime 0.13.0-alpha.0", + "tauri-runtime-wry 0.13.0-alpha.0", + "tauri-utils 2.0.0-alpha.0", + "tempfile", + "thiserror", + "tokio", + "url", + "uuid 1.2.2", + "webkit2gtk", + "webview2-com", + "windows 0.39.0", +] + +[[package]] +name = "tauri" +version = "2.0.0-alpha.2" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" dependencies = [ "android_logger", "anyhow", @@ -4299,7 +4362,6 @@ dependencies = [ "dirs-next", "embed_plist", "encoding_rs", - "env_logger 0.9.3", "flate2", "futures-util", "glib", @@ -4308,9 +4370,11 @@ dependencies = [ "heck 0.4.0", "http", "ignore", + "libc", "log", "objc", "once_cell", + "oslog", "paste", "percent-encoding", "rand 0.8.5", @@ -4322,10 +4386,10 @@ dependencies = [ "serialize-to-javascript", "state", "tar", - "tauri-macros", - "tauri-runtime", - "tauri-runtime-wry", - "tauri-utils", + "tauri-macros 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-runtime 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-runtime-wry 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", "tempfile", "thiserror", "tokio", @@ -4339,8 +4403,6 @@ dependencies = [ [[package]] name = "tauri-codegen" version = "2.0.0-alpha.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e78b01de486c5aefb99d2623d856b3d3afb88499bc9b8514e246762505006c1" dependencies = [ "base64 0.13.1", "brotli", @@ -4354,7 +4416,32 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.6", - "tauri-utils", + "tauri-utils 2.0.0-alpha.0", + "thiserror", + "time 0.3.17", + "url", + "uuid 1.2.2", + "walkdir", +] + +[[package]] +name = "tauri-codegen" +version = "2.0.0-alpha.0" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +dependencies = [ + "base64 0.13.1", + "brotli", + "ico", + "json-patch", + "plist", + "png", + "proc-macro2", + "quote", + "semver 1.0.16", + "serde", + "serde_json", + "sha2 0.10.6", + "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "time 0.3.17", "url", @@ -4365,15 +4452,26 @@ dependencies = [ [[package]] name = "tauri-macros" version = "2.0.0-alpha.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df039672c40713ded0f7991efd1d7d6219e008c73137483d6dd2d9988f39472e" dependencies = [ "heck 0.4.0", "proc-macro2", "quote", "syn", - "tauri-codegen", - "tauri-utils", + "tauri-codegen 2.0.0-alpha.0", + "tauri-utils 2.0.0-alpha.0", +] + +[[package]] +name = "tauri-macros" +version = "2.0.0-alpha.0" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +dependencies = [ + "heck 0.4.0", + "proc-macro2", + "quote", + "syn", + "tauri-codegen 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", ] [[package]] @@ -4390,7 +4488,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.6", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "u2f", ] @@ -4403,7 +4501,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4414,7 +4512,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4426,7 +4524,7 @@ dependencies = [ "notify", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4438,7 +4536,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "tiny_http", ] @@ -4447,13 +4545,15 @@ dependencies = [ name = "tauri-plugin-log" version = "0.1.0" dependencies = [ + "android_logger", "byte-unit", "fern", "log", + "oslog", "serde", "serde_json", "serde_repr", - "tauri", + "tauri 2.0.0-alpha.2", "time 0.3.17", ] @@ -4465,7 +4565,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4477,7 +4577,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4488,7 +4588,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "windows-sys", "zbus", @@ -4503,7 +4603,7 @@ dependencies = [ "serde", "serde_json", "sqlx", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "tokio", ] @@ -4515,7 +4615,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] @@ -4531,7 +4631,7 @@ dependencies = [ "rusty-fork", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "zeroize", ] @@ -4546,7 +4646,7 @@ dependencies = [ "reqwest", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "tokio", "tokio-util", @@ -4561,7 +4661,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "tokio", "tokio-tungstenite", @@ -4575,15 +4675,13 @@ dependencies = [ "log", "serde", "serde_json", - "tauri", + "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", ] [[package]] name = "tauri-runtime" version = "0.13.0-alpha.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "010bdd6bb6585b647afe262d3e43ec612969f328d32c9ce20f9cfaefccb5e7a9" dependencies = [ "gtk", "http", @@ -4592,7 +4690,26 @@ dependencies = [ "raw-window-handle", "serde", "serde_json", - "tauri-utils", + "tauri-utils 2.0.0-alpha.0", + "thiserror", + "uuid 1.2.2", + "webview2-com", + "windows 0.39.0", +] + +[[package]] +name = "tauri-runtime" +version = "0.13.0-alpha.0" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +dependencies = [ + "gtk", + "http", + "http-range", + "rand 0.8.5", + "raw-window-handle", + "serde", + "serde_json", + "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", "thiserror", "uuid 1.2.2", "webview2-com", @@ -4602,16 +4719,33 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dadf5f22694dbb5a1fd9496fe28f48ba4288ed4cb8c2584911a81cd57e24e90" dependencies = [ "cocoa", "gtk", "percent-encoding", "rand 0.8.5", "raw-window-handle", - "tauri-runtime", - "tauri-utils", + "tauri-runtime 0.13.0-alpha.0", + "tauri-utils 2.0.0-alpha.0", + "uuid 1.2.2", + "webkit2gtk", + "webview2-com", + "windows 0.39.0", + "wry", +] + +[[package]] +name = "tauri-runtime-wry" +version = "0.13.0-alpha.0" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +dependencies = [ + "cocoa", + "gtk", + "percent-encoding", + "rand 0.8.5", + "raw-window-handle", + "tauri-runtime 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", "uuid 1.2.2", "webkit2gtk", "webview2-com", @@ -4622,8 +4756,33 @@ dependencies = [ [[package]] name = "tauri-utils" version = "2.0.0-alpha.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c7ee9f908d975a116f2fe3db47d65b18e568aa7913aa333b7a5cd990f179e5e" +dependencies = [ + "brotli", + "ctor", + "glob", + "heck 0.4.0", + "html5ever", + "infer", + "json-patch", + "kuchiki", + "memchr", + "phf 0.10.1", + "proc-macro2", + "quote", + "semver 1.0.16", + "serde", + "serde_json", + "serde_with", + "thiserror", + "url", + "walkdir", + "windows 0.39.0", +] + +[[package]] +name = "tauri-utils" +version = "2.0.0-alpha.0" +source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" dependencies = [ "brotli", "ctor", @@ -5632,9 +5791,8 @@ dependencies = [ [[package]] name = "wry" -version = "0.23.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c1ad8e2424f554cc5bdebe8aa374ef5b433feff817aebabca0389961fc7ef98" +version = "0.24.1" +source = "git+https://github.com/tauri-apps/wry?branch=dev#87216c7f01d5f65641422343dd0aa7f08ea61d0d" dependencies = [ "base64 0.13.1", "block", diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 41a1457a..77bec244 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -12,12 +12,21 @@ rust-version.workspace = true [dependencies] serde.workspace = true serde_json.workspace = true -tauri.workspace = true +# tauri.workspace = true +tauri = { path = "../../../tauri/core/tauri" } serde_repr = "0.1" byte-unit = "4.0" -fern = "0.6" log = { workspace = true, features = ["kv_unstable"] } time = { version = "0.3", features = ["formatting"] } +[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] +fern = "0.6" + +[target."cfg(target_os = \"android\")".dependencies] +android_logger = "0.9" + +[target."cfg(target_os = \"ios\")".dependencies] +oslog = "0.2" + [features] colored = ["fern/colored"] \ No newline at end of file diff --git a/plugins/log/build.rs b/plugins/log/build.rs new file mode 100644 index 00000000..e5214de8 --- /dev/null +++ b/plugins/log/build.rs @@ -0,0 +1,12 @@ +fn alias(alias: &str, has_feature: bool) { + if has_feature { + println!("cargo:rustc-cfg={alias}"); + } +} + +fn main() { + let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); + let mobile = target_os == "ios" || target_os == "android"; + alias("desktop", !mobile); + alias("mobile", mobile); +} diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index cf25db2d..3df238ec 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +#[cfg(desktop)] use fern::FormatCallback; use log::{logger, RecordBuilder}; use log::{LevelFilter, Record}; @@ -20,6 +21,7 @@ use tauri::{ Manager, Runtime, }; +#[cfg(desktop)] pub use fern; const DEFAULT_MAX_FILE_SIZE: u128 = 40000; @@ -143,31 +145,41 @@ fn log( } pub struct Builder { + #[cfg(desktop)] dispatch: fern::Dispatch, rotation_strategy: RotationStrategy, max_file_size: u128, targets: Vec, + level_filter: Option, + levels: Vec<(Cow<'static, str>, log::LevelFilter)>, } impl Default for Builder { fn default() -> Self { - let format = - time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") - .unwrap(); - let dispatch = fern::Dispatch::new().format(move |out, message, record| { - out.finish(format_args!( - "{}[{}][{}] {}", - time::OffsetDateTime::now_utc().format(&format).unwrap(), - record.target(), - record.level(), - message - )) - }); + #[cfg(desktop)] + let dispatch = { + let format = time::format_description::parse( + "[[[year]-[month]-[day]][[[hour]:[minute]:[second]]", + ) + .unwrap(); + fern::Dispatch::new().format(move |out, message, record| { + out.finish(format_args!( + "{}[{}][{}] {}", + time::OffsetDateTime::now_utc().format(&format).unwrap(), + record.target(), + record.level(), + message + )) + }) + }; Self { + #[cfg(desktop)] dispatch, rotation_strategy: DEFAULT_ROTATION_STRATEGY, max_file_size: DEFAULT_MAX_FILE_SIZE, targets: DEFAULT_LOG_TARGETS.into(), + level_filter: None, + levels: Vec::new(), } } } @@ -187,6 +199,7 @@ impl Builder { self } + #[cfg(desktop)] pub fn format(mut self, formatter: F) -> Self where F: Fn(FormatCallback, &Arguments, &Record) + Sync + Send + 'static, @@ -196,15 +209,27 @@ impl Builder { } pub fn level(mut self, level_filter: impl Into) -> Self { - self.dispatch = self.dispatch.level(level_filter.into()); + self.level_filter.replace(level_filter.into()); self } pub fn level_for(mut self, module: impl Into>, level: LevelFilter) -> Self { - self.dispatch = self.dispatch.level_for(module, level); + let module = module.into(); + + if let Some((index, _)) = self + .levels + .iter() + .enumerate() + .find(|&(_, &(ref name, _))| name == &module) + { + self.levels.remove(index); + } + + self.levels.push((module, level)); self } + #[cfg(desktop)] pub fn filter(mut self, filter: F) -> Self where F: Fn(&log::Metadata) -> bool + Send + Sync + 'static, @@ -223,7 +248,7 @@ impl Builder { self } - #[cfg(feature = "colored")] + #[cfg(all(desktop, feature = "colored"))] pub fn with_colors(self, colors: fern::colors::ColoredLevelConfig) -> Self { let format = time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") @@ -240,61 +265,146 @@ impl Builder { } pub fn build(mut self) -> TauriPlugin { + #[cfg(desktop)] + { + if let Some(level) = self.level_filter { + self.dispatch = self.dispatch.level(level); + } + for (module, level) in self.levels { + self.dispatch = self.dispatch.level_for(module, level); + } + } + plugin::Builder::new("log") .invoke_handler(tauri::generate_handler![log]) .setup(move |app_handle| { let app_name = &app_handle.package_info().name; - // setup targets - for target in &self.targets { - self.dispatch = self.dispatch.chain(match target { - LogTarget::Stdout => fern::Output::from(std::io::stdout()), - LogTarget::Stderr => fern::Output::from(std::io::stderr()), - LogTarget::Folder(path) => { - if !path.exists() { - fs::create_dir_all(path).unwrap(); - } - - fern::log_file(get_log_file_path( - &path, - app_name, - &self.rotation_strategy, - self.max_file_size, - )?)? - .into() + #[cfg(target_os = "ios")] + { + let mut subsystem = String::new(); + for (i, w) in app_handle + .config() + .tauri + .bundle + .identifier + .split('.') + .enumerate() + { + if i != last { + subsystem.push_str(w); + subsystem.push('.'); } - LogTarget::LogDir => { - let path = app_handle.path_resolver().app_log_dir().unwrap(); - if !path.exists() { - fs::create_dir_all(&path).unwrap(); - } + } + subsystem.pop(); + subsystem.push_str(&app_handle.package_info().crate_name); + let mut logger = oslog::OsLogger::new(&subsystem); + if let Some(level_filter) = self.level_filter { + logger = logger.level_filter(level_filter); + } + for (module, level) in self.levels { + logger = logger.category_level_filter(&module, level); + } + logger.init()?; + } - fern::log_file(get_log_file_path( - &path, - app_name, - &self.rotation_strategy, - self.max_file_size, - )?)? - .into() + #[cfg(target_os = "android")] + { + let mut logger = android_logger::Config::default(); + if let Some(level_filter) = self.level_filter { + if let Some(level) = level_filter.to_level() { + logger = logger.with_min_level(level); } - LogTarget::Webview => { - let app_handle = app_handle.clone(); - - fern::Output::call(move |record| { - let payload = RecordPayload { - message: record.args().to_string(), - level: record.level().into(), - }; - let app_handle = app_handle.clone(); - tauri::async_runtime::spawn(async move { - app_handle.emit_all("log://log", payload).unwrap(); - }); - }) + } else { + logger = logger.with_min_level(log::Level::Trace); + } + if !self.levels.is_empty() { + let mut filter = android_logger::FilterBuilder::new(); + for (module, level) in self.levels { + filter.filter_module(&module, level); } - }); + logger = logger.with_filter(filter.build()); + } + println!( + "with tag {}", + app_handle + .config() + .tauri + .bundle + .identifier + .split('.') + .rev() + .next() + .unwrap(), + ); + android_logger::init_once( + logger.with_tag( + app_handle + .config() + .tauri + .bundle + .identifier + .split('.') + .rev() + .next() + .unwrap(), + ), + ); } - self.dispatch.apply()?; + #[cfg(desktop)] + { + // setup targets + for target in &self.targets { + self.dispatch = self.dispatch.chain(match target { + LogTarget::Stdout => fern::Output::from(std::io::stdout()), + LogTarget::Stderr => fern::Output::from(std::io::stderr()), + LogTarget::Folder(path) => { + if !path.exists() { + fs::create_dir_all(path).unwrap(); + } + + fern::log_file(get_log_file_path( + &path, + app_name, + &self.rotation_strategy, + self.max_file_size, + )?)? + .into() + } + LogTarget::LogDir => { + let path = app_handle.path_resolver().app_log_dir().unwrap(); + if !path.exists() { + fs::create_dir_all(&path).unwrap(); + } + + fern::log_file(get_log_file_path( + &path, + app_name, + &self.rotation_strategy, + self.max_file_size, + )?)? + .into() + } + LogTarget::Webview => { + let app_handle = app_handle.clone(); + + fern::Output::call(move |record| { + let payload = RecordPayload { + message: record.args().to_string(), + level: record.level().into(), + }; + let app_handle = app_handle.clone(); + tauri::async_runtime::spawn(async move { + app_handle.emit_all("log://log", payload).unwrap(); + }); + }) + } + }); + } + + self.dispatch.apply()?; + } Ok(()) }) From 13fcc06087653fb4e8ca4453900fbeceb0946be7 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 16 Jan 2023 18:08:36 -0300 Subject: [PATCH 02/12] fix iOS build --- plugins/log/src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 3df238ec..20f6e94c 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -283,14 +283,10 @@ impl Builder { #[cfg(target_os = "ios")] { let mut subsystem = String::new(); - for (i, w) in app_handle - .config() - .tauri - .bundle - .identifier - .split('.') - .enumerate() - { + let identifier = &app_handle.config().tauri.bundle.identifier; + let mut s = identifier.split('.'); + let last = s.clone().count() - 1; + for (i, w) in s.enumerate() { if i != last { subsystem.push_str(w); subsystem.push('.'); From ff2ce60a9c96c5b2ed698d3e89314502b68f7979 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 12:44:53 -0300 Subject: [PATCH 03/12] fix ios subsystem --- plugins/log/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 20f6e94c..796c1391 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -292,7 +292,6 @@ impl Builder { subsystem.push('.'); } } - subsystem.pop(); subsystem.push_str(&app_handle.package_info().crate_name); let mut logger = oslog::OsLogger::new(&subsystem); if let Some(level_filter) = self.level_filter { From 976cd70eef2960b27def7fadc42fbcb860d97116 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 12:58:10 -0300 Subject: [PATCH 04/12] fix default log level --- plugins/log/src/lib.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 796c1391..d24bc7a7 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -278,13 +278,11 @@ impl Builder { plugin::Builder::new("log") .invoke_handler(tauri::generate_handler![log]) .setup(move |app_handle| { - let app_name = &app_handle.package_info().name; - #[cfg(target_os = "ios")] { let mut subsystem = String::new(); let identifier = &app_handle.config().tauri.bundle.identifier; - let mut s = identifier.split('.'); + let s = identifier.split('.'); let last = s.clone().count() - 1; for (i, w) in s.enumerate() { if i != last { @@ -293,10 +291,10 @@ impl Builder { } } subsystem.push_str(&app_handle.package_info().crate_name); + let mut logger = oslog::OsLogger::new(&subsystem); - if let Some(level_filter) = self.level_filter { - logger = logger.level_filter(level_filter); - } + logger = + logger.level_filter(self.level_filter.unwrap_or(log::LevelFilter::Trace)); for (module, level) in self.levels { logger = logger.category_level_filter(&module, level); } @@ -349,6 +347,7 @@ impl Builder { #[cfg(desktop)] { + let app_name = &app_handle.package_info().name; // setup targets for target in &self.targets { self.dispatch = self.dispatch.chain(match target { From cc03bde0eaf273e5c00c5c5e07c1c6ac48ae828d Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 13:15:02 -0300 Subject: [PATCH 05/12] revert tauri dep --- Cargo.lock | 195 ++++++----------------------------------- plugins/log/Cargo.toml | 2 +- 2 files changed, 26 insertions(+), 171 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 847a616e..329eeb04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4303,53 +4303,6 @@ dependencies = [ "xattr", ] -[[package]] -name = "tauri" -version = "2.0.0-alpha.2" -dependencies = [ - "anyhow", - "attohttpc", - "cocoa", - "dirs-next", - "embed_plist", - "encoding_rs", - "flate2", - "futures-util", - "glib", - "glob", - "gtk", - "heck 0.4.0", - "http", - "ignore", - "libc", - "log", - "objc", - "once_cell", - "paste", - "percent-encoding", - "rand 0.8.5", - "raw-window-handle", - "semver 1.0.16", - "serde", - "serde_json", - "serde_repr", - "serialize-to-javascript", - "state", - "tar", - "tauri-macros 2.0.0-alpha.0", - "tauri-runtime 0.13.0-alpha.0", - "tauri-runtime-wry 0.13.0-alpha.0", - "tauri-utils 2.0.0-alpha.0", - "tempfile", - "thiserror", - "tokio", - "url", - "uuid 1.2.2", - "webkit2gtk", - "webview2-com", - "windows 0.39.0", -] - [[package]] name = "tauri" version = "2.0.0-alpha.2" @@ -4386,10 +4339,10 @@ dependencies = [ "serialize-to-javascript", "state", "tar", - "tauri-macros 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", - "tauri-runtime 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", - "tauri-runtime-wry 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", - "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-macros", + "tauri-runtime", + "tauri-runtime-wry", + "tauri-utils", "tempfile", "thiserror", "tokio", @@ -4400,30 +4353,6 @@ dependencies = [ "windows 0.39.0", ] -[[package]] -name = "tauri-codegen" -version = "2.0.0-alpha.0" -dependencies = [ - "base64 0.13.1", - "brotli", - "ico", - "json-patch", - "plist", - "png", - "proc-macro2", - "quote", - "semver 1.0.16", - "serde", - "serde_json", - "sha2 0.10.6", - "tauri-utils 2.0.0-alpha.0", - "thiserror", - "time 0.3.17", - "url", - "uuid 1.2.2", - "walkdir", -] - [[package]] name = "tauri-codegen" version = "2.0.0-alpha.0" @@ -4441,7 +4370,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.6", - "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-utils", "thiserror", "time 0.3.17", "url", @@ -4449,18 +4378,6 @@ dependencies = [ "walkdir", ] -[[package]] -name = "tauri-macros" -version = "2.0.0-alpha.0" -dependencies = [ - "heck 0.4.0", - "proc-macro2", - "quote", - "syn", - "tauri-codegen 2.0.0-alpha.0", - "tauri-utils 2.0.0-alpha.0", -] - [[package]] name = "tauri-macros" version = "2.0.0-alpha.0" @@ -4470,8 +4387,8 @@ dependencies = [ "proc-macro2", "quote", "syn", - "tauri-codegen 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", - "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-codegen", + "tauri-utils", ] [[package]] @@ -4488,7 +4405,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.6", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "u2f", ] @@ -4501,7 +4418,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4512,7 +4429,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4524,7 +4441,7 @@ dependencies = [ "notify", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4536,7 +4453,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "tiny_http", ] @@ -4553,7 +4470,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", - "tauri 2.0.0-alpha.2", + "tauri", "time 0.3.17", ] @@ -4565,7 +4482,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4577,7 +4494,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4588,7 +4505,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "windows-sys", "zbus", @@ -4603,7 +4520,7 @@ dependencies = [ "serde", "serde_json", "sqlx", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "tokio", ] @@ -4615,7 +4532,7 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", ] @@ -4631,7 +4548,7 @@ dependencies = [ "rusty-fork", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "zeroize", ] @@ -4646,7 +4563,7 @@ dependencies = [ "reqwest", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "tokio", "tokio-util", @@ -4661,7 +4578,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri", "thiserror", "tokio", "tokio-tungstenite", @@ -4675,26 +4592,8 @@ dependencies = [ "log", "serde", "serde_json", - "tauri 2.0.0-alpha.2 (git+https://github.com/tauri-apps/tauri?branch=next)", - "thiserror", -] - -[[package]] -name = "tauri-runtime" -version = "0.13.0-alpha.0" -dependencies = [ - "gtk", - "http", - "http-range", - "rand 0.8.5", - "raw-window-handle", - "serde", - "serde_json", - "tauri-utils 2.0.0-alpha.0", + "tauri", "thiserror", - "uuid 1.2.2", - "webview2-com", - "windows 0.39.0", ] [[package]] @@ -4709,31 +4608,13 @@ dependencies = [ "raw-window-handle", "serde", "serde_json", - "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-utils", "thiserror", "uuid 1.2.2", "webview2-com", "windows 0.39.0", ] -[[package]] -name = "tauri-runtime-wry" -version = "0.13.0-alpha.0" -dependencies = [ - "cocoa", - "gtk", - "percent-encoding", - "rand 0.8.5", - "raw-window-handle", - "tauri-runtime 0.13.0-alpha.0", - "tauri-utils 2.0.0-alpha.0", - "uuid 1.2.2", - "webkit2gtk", - "webview2-com", - "windows 0.39.0", - "wry", -] - [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.0" @@ -4744,8 +4625,8 @@ dependencies = [ "percent-encoding", "rand 0.8.5", "raw-window-handle", - "tauri-runtime 0.13.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", - "tauri-utils 2.0.0-alpha.0 (git+https://github.com/tauri-apps/tauri?branch=next)", + "tauri-runtime", + "tauri-utils", "uuid 1.2.2", "webkit2gtk", "webview2-com", @@ -4753,32 +4634,6 @@ dependencies = [ "wry", ] -[[package]] -name = "tauri-utils" -version = "2.0.0-alpha.0" -dependencies = [ - "brotli", - "ctor", - "glob", - "heck 0.4.0", - "html5ever", - "infer", - "json-patch", - "kuchiki", - "memchr", - "phf 0.10.1", - "proc-macro2", - "quote", - "semver 1.0.16", - "serde", - "serde_json", - "serde_with", - "thiserror", - "url", - "walkdir", - "windows 0.39.0", -] - [[package]] name = "tauri-utils" version = "2.0.0-alpha.0" diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 77bec244..60f21510 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -13,7 +13,7 @@ rust-version.workspace = true serde.workspace = true serde_json.workspace = true # tauri.workspace = true -tauri = { path = "../../../tauri/core/tauri" } +tauri.workspace = true serde_repr = "0.1" byte-unit = "4.0" log = { workspace = true, features = ["kv_unstable"] } From fa44de7440f30dfd5f32371c95e9daa55fa85233 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 13:17:28 -0300 Subject: [PATCH 06/12] remove warnings --- plugins/log/src/lib.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index d24bc7a7..925eb262 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -4,26 +4,32 @@ #[cfg(desktop)] use fern::FormatCallback; +use log::LevelFilter; use log::{logger, RecordBuilder}; -use log::{LevelFilter, Record}; use serde::Serialize; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::borrow::Cow; use std::collections::HashMap; -use std::{ - fmt::Arguments, - fs::{self, File}, - iter::FromIterator, - path::{Path, PathBuf}, -}; +use std::{iter::FromIterator, path::PathBuf}; use tauri::{ plugin::{self, TauriPlugin}, - Manager, Runtime, + Runtime, }; +#[cfg(desktop)] +use desktop::*; #[cfg(desktop)] pub use fern; +#[cfg(desktop)] +mod desktop { + pub use std::{ + fs::{self, File}, + path::Path, + }; + pub use tauri::Manager; +} + const DEFAULT_MAX_FILE_SIZE: u128 = 40000; const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne; const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir]; @@ -202,7 +208,7 @@ impl Builder { #[cfg(desktop)] pub fn format(mut self, formatter: F) -> Self where - F: Fn(FormatCallback, &Arguments, &Record) + Sync + Send + 'static, + F: Fn(FormatCallback, &std::fmt::Arguments, &log::Record) + Sync + Send + 'static, { self.dispatch = self.dispatch.format(formatter); self @@ -264,7 +270,7 @@ impl Builder { }) } - pub fn build(mut self) -> TauriPlugin { + pub fn build(#[allow(unused_mut)] mut self) -> TauriPlugin { #[cfg(desktop)] { if let Some(level) = self.level_filter { @@ -406,6 +412,7 @@ impl Builder { } } +#[cfg(desktop)] fn get_log_file_path( dir: &impl AsRef, app_name: &str, From bbdabbd64d69fe0c3f11fd0ff0ba514ab0360126 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 15:51:16 -0300 Subject: [PATCH 07/12] refactor: keep fern [skip ci] --- plugins/log/Cargo.toml | 2 - plugins/log/src/lib.rs | 284 +++++++++++++++-------------------------- 2 files changed, 105 insertions(+), 181 deletions(-) diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 60f21510..883ffd95 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -18,8 +18,6 @@ serde_repr = "0.1" byte-unit = "4.0" log = { workspace = true, features = ["kv_unstable"] } time = { version = "0.3", features = ["formatting"] } - -[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] fern = "0.6" [target."cfg(target_os = \"android\")".dependencies] diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 925eb262..4323e5a1 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -2,34 +2,26 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -#[cfg(desktop)] use fern::FormatCallback; -use log::LevelFilter; use log::{logger, RecordBuilder}; +use log::{LevelFilter, Record}; use serde::Serialize; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::borrow::Cow; use std::collections::HashMap; -use std::{iter::FromIterator, path::PathBuf}; +use std::{ + fmt::Arguments, + fs::{self, File}, + iter::FromIterator, + path::{Path, PathBuf}, +}; use tauri::{ plugin::{self, TauriPlugin}, - Runtime, + Manager, Runtime, }; -#[cfg(desktop)] -use desktop::*; -#[cfg(desktop)] pub use fern; -#[cfg(desktop)] -mod desktop { - pub use std::{ - fs::{self, File}, - path::Path, - }; - pub use tauri::Manager; -} - const DEFAULT_MAX_FILE_SIZE: u128 = 40000; const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne; const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir]; @@ -151,41 +143,31 @@ fn log( } pub struct Builder { - #[cfg(desktop)] dispatch: fern::Dispatch, rotation_strategy: RotationStrategy, max_file_size: u128, targets: Vec, - level_filter: Option, - levels: Vec<(Cow<'static, str>, log::LevelFilter)>, } impl Default for Builder { fn default() -> Self { - #[cfg(desktop)] - let dispatch = { - let format = time::format_description::parse( - "[[[year]-[month]-[day]][[[hour]:[minute]:[second]]", - ) - .unwrap(); - fern::Dispatch::new().format(move |out, message, record| { - out.finish(format_args!( - "{}[{}][{}] {}", - time::OffsetDateTime::now_utc().format(&format).unwrap(), - record.target(), - record.level(), - message - )) - }) - }; + let format = + time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") + .unwrap(); + let dispatch = fern::Dispatch::new().format(move |out, message, record| { + out.finish(format_args!( + "{}[{}][{}] {}", + time::OffsetDateTime::now_utc().format(&format).unwrap(), + record.target(), + record.level(), + message + )) + }); Self { - #[cfg(desktop)] dispatch, rotation_strategy: DEFAULT_ROTATION_STRATEGY, max_file_size: DEFAULT_MAX_FILE_SIZE, targets: DEFAULT_LOG_TARGETS.into(), - level_filter: None, - levels: Vec::new(), } } } @@ -205,37 +187,24 @@ impl Builder { self } - #[cfg(desktop)] pub fn format(mut self, formatter: F) -> Self where - F: Fn(FormatCallback, &std::fmt::Arguments, &log::Record) + Sync + Send + 'static, + F: Fn(FormatCallback, &Arguments, &Record) + Sync + Send + 'static, { self.dispatch = self.dispatch.format(formatter); self } pub fn level(mut self, level_filter: impl Into) -> Self { - self.level_filter.replace(level_filter.into()); + self.dispatch = self.dispatch.level(level_filter.into()); self } pub fn level_for(mut self, module: impl Into>, level: LevelFilter) -> Self { - let module = module.into(); - - if let Some((index, _)) = self - .levels - .iter() - .enumerate() - .find(|&(_, &(ref name, _))| name == &module) - { - self.levels.remove(index); - } - - self.levels.push((module, level)); + self.dispatch = self.dispatch.level_for(module, level); self } - #[cfg(desktop)] pub fn filter(mut self, filter: F) -> Self where F: Fn(&log::Metadata) -> bool + Send + Sync + 'static, @@ -254,7 +223,7 @@ impl Builder { self } - #[cfg(all(desktop, feature = "colored"))] + #[cfg(feature = "colored")] pub fn with_colors(self, colors: fern::colors::ColoredLevelConfig) -> Self { let format = time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") @@ -270,149 +239,106 @@ impl Builder { }) } - pub fn build(#[allow(unused_mut)] mut self) -> TauriPlugin { - #[cfg(desktop)] - { - if let Some(level) = self.level_filter { - self.dispatch = self.dispatch.level(level); - } - for (module, level) in self.levels { - self.dispatch = self.dispatch.level_for(module, level); - } - } - + pub fn build(mut self) -> TauriPlugin { plugin::Builder::new("log") .invoke_handler(tauri::generate_handler![log]) .setup(move |app_handle| { - #[cfg(target_os = "ios")] - { - let mut subsystem = String::new(); - let identifier = &app_handle.config().tauri.bundle.identifier; - let s = identifier.split('.'); - let last = s.clone().count() - 1; - for (i, w) in s.enumerate() { - if i != last { - subsystem.push_str(w); - subsystem.push('.'); - } - } - subsystem.push_str(&app_handle.package_info().crate_name); - - let mut logger = oslog::OsLogger::new(&subsystem); - logger = - logger.level_filter(self.level_filter.unwrap_or(log::LevelFilter::Trace)); - for (module, level) in self.levels { - logger = logger.category_level_filter(&module, level); - } - logger.init()?; - } - - #[cfg(target_os = "android")] - { - let mut logger = android_logger::Config::default(); - if let Some(level_filter) = self.level_filter { - if let Some(level) = level_filter.to_level() { - logger = logger.with_min_level(level); + let app_name = &app_handle.package_info().name; + + // setup targets + for target in &self.targets { + let logger = match target { + #[cfg(target_os = "android")] + LogTarget::Stdout | LogTarget::Stderr => { + fern::Output::call(android_logger::log) } - } else { - logger = logger.with_min_level(log::Level::Trace); - } - if !self.levels.is_empty() { - let mut filter = android_logger::FilterBuilder::new(); - for (module, level) in self.levels { - filter.filter_module(&module, level); - } - logger = logger.with_filter(filter.build()); - } - println!( - "with tag {}", - app_handle - .config() - .tauri - .bundle - .identifier - .split('.') - .rev() - .next() - .unwrap(), - ); - android_logger::init_once( - logger.with_tag( - app_handle - .config() - .tauri - .bundle - .identifier - .split('.') - .rev() - .next() - .unwrap(), - ), - ); - } - - #[cfg(desktop)] - { - let app_name = &app_handle.package_info().name; - // setup targets - for target in &self.targets { - self.dispatch = self.dispatch.chain(match target { - LogTarget::Stdout => fern::Output::from(std::io::stdout()), - LogTarget::Stderr => fern::Output::from(std::io::stderr()), - LogTarget::Folder(path) => { - if !path.exists() { - fs::create_dir_all(path).unwrap(); + #[cfg(target_os = "ios")] + LogTarget::Stdout | LogTarget::Stderr => { + use std::{collections::HashMap, sync::Mutex}; + let loggers: Mutex> = Default::default(); + let mut subsystem = String::new(); + let identifier = &app_handle.config().tauri.bundle.identifier; + let s = identifier.split('.'); + let last = s.clone().count() - 1; + for (i, w) in s.enumerate() { + if i != last { + subsystem.push_str(w); + subsystem.push('.'); } - - fern::log_file(get_log_file_path( - &path, - app_name, - &self.rotation_strategy, - self.max_file_size, - )?)? - .into() } - LogTarget::LogDir => { - let path = app_handle.path_resolver().app_log_dir().unwrap(); - if !path.exists() { - fs::create_dir_all(&path).unwrap(); - } + subsystem.push_str(&app_handle.package_info().crate_name); - fern::log_file(get_log_file_path( - &path, - app_name, - &self.rotation_strategy, - self.max_file_size, - )?)? - .into() + fern::Output::call(move |record| { + let mut loggers = loggers.lock().unwrap(); + let pair = + loggers.entry(record.target().into()).or_insert_with(|| { + oslog::OsLog::new(&subsystem, record.target()) + }); + + let message = format!("{}", record.args()); + (*pair).with_level(record.level().into(), &message); + }); + } + #[cfg(desktop)] + LogTarget::Stdout => std::io::stdout().into(), + #[cfg(desktop)] + LogTarget::Stderr => std::io::stderr().into(), + LogTarget::Folder(path) => { + if !path.exists() { + fs::create_dir_all(path).unwrap(); } - LogTarget::Webview => { - let app_handle = app_handle.clone(); - fern::Output::call(move |record| { - let payload = RecordPayload { - message: record.args().to_string(), - level: record.level().into(), - }; - let app_handle = app_handle.clone(); - tauri::async_runtime::spawn(async move { - app_handle.emit_all("log://log", payload).unwrap(); - }); - }) + fern::log_file(get_log_file_path( + &path, + app_name, + &self.rotation_strategy, + self.max_file_size, + )?)? + .into() + } + #[cfg(mobile)] + LogTarget::LogDir => continue, + #[cfg(desktop)] + LogTarget::LogDir => { + let path = app_handle.path_resolver().app_log_dir().unwrap(); + if !path.exists() { + fs::create_dir_all(&path).unwrap(); } - }); - } - self.dispatch.apply()?; + fern::log_file(get_log_file_path( + &path, + app_name, + &self.rotation_strategy, + self.max_file_size, + )?)? + .into() + } + LogTarget::Webview => { + let app_handle = app_handle.clone(); + + fern::Output::call(move |record| { + let payload = RecordPayload { + message: record.args().to_string(), + level: record.level().into(), + }; + let app_handle = app_handle.clone(); + tauri::async_runtime::spawn(async move { + app_handle.emit_all("log://log", payload).unwrap(); + }); + }) + } + }; + self.dispatch = self.dispatch.chain(logger); } + self.dispatch.apply()?; + Ok(()) }) .build() } } -#[cfg(desktop)] fn get_log_file_path( dir: &impl AsRef, app_name: &str, From 9424204a19f79811227cf1448cb88c1f2bbe49fb Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 16:00:09 -0300 Subject: [PATCH 08/12] fix ios [skip ci] --- plugins/log/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 4323e5a1..62194d57 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -254,7 +254,7 @@ impl Builder { } #[cfg(target_os = "ios")] LogTarget::Stdout | LogTarget::Stderr => { - use std::{collections::HashMap, sync::Mutex}; + use std::sync::Mutex; let loggers: Mutex> = Default::default(); let mut subsystem = String::new(); let identifier = &app_handle.config().tauri.bundle.identifier; @@ -277,7 +277,7 @@ impl Builder { let message = format!("{}", record.args()); (*pair).with_level(record.level().into(), &message); - }); + }) } #[cfg(desktop)] LogTarget::Stdout => std::io::stdout().into(), From 510845c1a058eed6fae0bc52787edec2e3d18dc4 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 16:06:30 -0300 Subject: [PATCH 09/12] update android_logger --- plugins/log/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 883ffd95..3441022c 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -21,7 +21,7 @@ time = { version = "0.3", features = ["formatting"] } fern = "0.6" [target."cfg(target_os = \"android\")".dependencies] -android_logger = "0.9" +android_logger = "0.11" [target."cfg(target_os = \"ios\")".dependencies] oslog = "0.2" From df21598a487e2a1162755ae0473b63b2def6e816 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 18:26:19 -0300 Subject: [PATCH 10/12] update deps --- Cargo.lock | 26 ++++++++++++-------------- plugins/log/Cargo.toml | 1 - 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 329eeb04..5d6c4f8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,14 +86,14 @@ checksum = "85965b6739a430150bdd138e2374a98af0c3ee0d030b3bb7fc3bddff58d0102e" [[package]] name = "android_logger" -version = "0.9.2" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ec2333c185d826313162cee39d3fcc6a84ba08114a839bebf53b961e7e75773" +checksum = "8619b80c242aa7bd638b5c7ddd952addeecb71f69c75e33f1d47b2804f8f883a" dependencies = [ "android_log-sys", "env_logger", - "lazy_static", "log", + "once_cell", ] [[package]] @@ -1166,9 +1166,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.7.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" dependencies = [ "log", "regex", @@ -4306,9 +4306,8 @@ dependencies = [ [[package]] name = "tauri" version = "2.0.0-alpha.2" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ - "android_logger", "anyhow", "attohttpc", "cocoa", @@ -4327,7 +4326,6 @@ dependencies = [ "log", "objc", "once_cell", - "oslog", "paste", "percent-encoding", "rand 0.8.5", @@ -4356,7 +4354,7 @@ dependencies = [ [[package]] name = "tauri-codegen" version = "2.0.0-alpha.0" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ "base64 0.13.1", "brotli", @@ -4381,7 +4379,7 @@ dependencies = [ [[package]] name = "tauri-macros" version = "2.0.0-alpha.0" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ "heck 0.4.0", "proc-macro2", @@ -4599,7 +4597,7 @@ dependencies = [ [[package]] name = "tauri-runtime" version = "0.13.0-alpha.0" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ "gtk", "http", @@ -4618,7 +4616,7 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.0" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ "cocoa", "gtk", @@ -4637,7 +4635,7 @@ dependencies = [ [[package]] name = "tauri-utils" version = "2.0.0-alpha.0" -source = "git+https://github.com/tauri-apps/tauri?branch=next#c36d451bcd9576dd4e0c673078c421dcb83932be" +source = "git+https://github.com/tauri-apps/tauri?branch=next#630a7f4b18cef169bfd48673609306fec434e397" dependencies = [ "brotli", "ctor", @@ -5647,7 +5645,7 @@ dependencies = [ [[package]] name = "wry" version = "0.24.1" -source = "git+https://github.com/tauri-apps/wry?branch=dev#87216c7f01d5f65641422343dd0aa7f08ea61d0d" +source = "git+https://github.com/tauri-apps/wry?branch=dev#bce39e2be195194e547b0021e770e45a3df15fa1" dependencies = [ "base64 0.13.1", "block", diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 3441022c..ec783b4f 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -12,7 +12,6 @@ rust-version.workspace = true [dependencies] serde.workspace = true serde_json.workspace = true -# tauri.workspace = true tauri.workspace = true serde_repr = "0.1" byte-unit = "4.0" From 5f13148aad6a0752dc41f3b7ab75e8ba888c38a2 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 18:30:06 -0300 Subject: [PATCH 11/12] simplify default formatter for mobile --- plugins/log/src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 62194d57..ae170080 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -155,13 +155,18 @@ impl Default for Builder { time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") .unwrap(); let dispatch = fern::Dispatch::new().format(move |out, message, record| { - out.finish(format_args!( - "{}[{}][{}] {}", - time::OffsetDateTime::now_utc().format(&format).unwrap(), - record.target(), - record.level(), - message - )) + out.finish( + #[cfg(mobile)] + format_args!("[{}] {}", record.target(), message), + #[cfg(desktop)] + format_args!( + "{}[{}][{}] {}", + time::OffsetDateTime::now_utc().format(&format).unwrap(), + record.target(), + record.level(), + message + ), + ) }); Self { dispatch, From 1675a4de80111195eca519d8136b1bf488cfc9b7 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 17 Jan 2023 19:22:18 -0300 Subject: [PATCH 12/12] fix unused var [skip ci] --- plugins/log/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index ae170080..70a140fd 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -151,6 +151,7 @@ pub struct Builder { impl Default for Builder { fn default() -> Self { + #[cfg(desktop)] let format = time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]") .unwrap();