From 42037a7a99e1796d10fe4e84f4f9b69c49334ee2 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 16 Jan 2023 17:47:43 -0300 Subject: [PATCH] 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(()) })