From 961602bd1b15e9c0d83d4c32c221a075fbabd83b Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 23 Feb 2023 09:17:15 -0800 Subject: [PATCH] fix(log): use Swift OSLog instead of oslog Rust binding (#262) --- Cargo.lock | 86 ++++++++----------------- plugins/log/Cargo.toml | 5 +- plugins/log/build.rs | 21 +++--- plugins/log/ios/.gitignore | 11 ++++ plugins/log/ios/Package.swift | 31 +++++++++ plugins/log/ios/README.md | 3 + plugins/log/ios/Sources/LogPlugin.swift | 13 ++++ plugins/log/src/lib.rs | 36 +++++------ 8 files changed, 117 insertions(+), 89 deletions(-) create mode 100644 plugins/log/ios/.gitignore create mode 100644 plugins/log/ios/Package.swift create mode 100644 plugins/log/ios/README.md create mode 100644 plugins/log/ios/Sources/LogPlugin.swift diff --git a/Cargo.lock b/Cargo.lock index b0b561c4..ba17acfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -959,19 +959,6 @@ 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 0.9.7", -] - [[package]] name = "der" version = "0.5.1" @@ -1831,9 +1818,9 @@ dependencies = [ [[package]] name = "http" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" dependencies = [ "bytes 1.4.0", "fnv", @@ -2707,6 +2694,15 @@ dependencies = [ "syn", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -2802,17 +2798,6 @@ 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" @@ -3108,7 +3093,7 @@ dependencies = [ "line-wrap", "quick-xml", "serde", - "time 0.3.18", + "time 0.3.15", ] [[package]] @@ -4273,7 +4258,7 @@ dependencies = [ [[package]] name = "tauri" version = "2.0.0-alpha.3" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "anyhow", "attohttpc", @@ -4323,7 +4308,7 @@ dependencies = [ [[package]] name = "tauri-build" version = "2.0.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "anyhow", "cargo_toml", @@ -4342,7 +4327,7 @@ dependencies = [ [[package]] name = "tauri-codegen" version = "2.0.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "base64 0.21.0", "brotli", @@ -4358,7 +4343,7 @@ dependencies = [ "sha2 0.10.6", "tauri-utils", "thiserror", - "time 0.3.18", + "time 0.3.15", "url", "uuid", "walkdir", @@ -4367,7 +4352,7 @@ dependencies = [ [[package]] name = "tauri-macros" version = "2.0.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "heck", "proc-macro2", @@ -4453,12 +4438,13 @@ dependencies = [ "byte-unit", "fern", "log", - "oslog", "serde", "serde_json", "serde_repr", + "swift-rs", "tauri", - "time 0.3.18", + "tauri-build", + "time 0.3.15", ] [[package]] @@ -4587,7 +4573,7 @@ dependencies = [ [[package]] name = "tauri-runtime" version = "0.13.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "gtk", "http", @@ -4608,7 +4594,7 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "cocoa", "gtk", @@ -4628,7 +4614,7 @@ dependencies = [ [[package]] name = "tauri-utils" version = "2.0.0-alpha.1" -source = "git+https://github.com/tauri-apps/tauri?branch=next#6aaba83476339fa413fe34d28877a932cb485117" +source = "git+https://github.com/tauri-apps/tauri?branch=next#105fe3fa242df87ad7655b519b0c01c3da9d8ee4" dependencies = [ "brotli", "ctor", @@ -4745,29 +4731,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.18" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af0097eaf301d576d0b2aead7a59facab6d53cc636340f0291fab8446a2e8613" +checksum = "d634a985c4d4238ec39cacaed2e7ae552fbd3c476b552c1deac3021b7d7eaf0c" dependencies = [ "itoa 1.0.5", - "serde", - "time-core", - "time-macros", -] - -[[package]] -name = "time-core" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" - -[[package]] -name = "time-macros" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2" -dependencies = [ - "time-core", + "libc", + "num_threads", ] [[package]] @@ -5698,7 +5668,7 @@ dependencies = [ [[package]] name = "wry" version = "0.27.0" -source = "git+https://github.com/tauri-apps/wry?branch=dev#9975dda12a122051b375ec6485650546d2d91c26" +source = "git+https://github.com/tauri-apps/wry?branch=dev#7795356a45b1bd015fad0e9973fc5af58c8c339b" dependencies = [ "base64 0.13.1", "block", diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index ec783b4f..2bd073e3 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -9,6 +9,9 @@ rust-version.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[build-dependencies] +tauri-build.workspace = true + [dependencies] serde.workspace = true serde_json.workspace = true @@ -23,7 +26,7 @@ fern = "0.6" android_logger = "0.11" [target."cfg(target_os = \"ios\")".dependencies] -oslog = "0.2" +swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "eb6de914ad57501da5019154d476d45660559999" } [features] colored = ["fern/colored"] \ No newline at end of file diff --git a/plugins/log/build.rs b/plugins/log/build.rs index e5214de8..e499a5e5 100644 --- a/plugins/log/build.rs +++ b/plugins/log/build.rs @@ -1,12 +1,15 @@ -fn alias(alias: &str, has_feature: bool) { - if has_feature { - println!("cargo:rustc-cfg={alias}"); - } -} +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use std::process::exit; 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); + if let Err(error) = tauri_build::mobile::PluginBuilder::new() + .ios_path("ios") + .run() + { + println!("{error:#}"); + exit(1); + } } diff --git a/plugins/log/ios/.gitignore b/plugins/log/ios/.gitignore new file mode 100644 index 00000000..7d98e3ad --- /dev/null +++ b/plugins/log/ios/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc +Package.resolved +/tauri-api diff --git a/plugins/log/ios/Package.swift b/plugins/log/ios/Package.swift new file mode 100644 index 00000000..5b818823 --- /dev/null +++ b/plugins/log/ios/Package.swift @@ -0,0 +1,31 @@ +// swift-tools-version:5.7 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "tauri-plugin-log", + platforms: [ + .iOS(.v11), + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "tauri-plugin-log", + type: .static, + targets: ["tauri-plugin-log"]), + ], + dependencies: [ + .package(name: "Tauri", path: "tauri-api") + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "tauri-plugin-log", + dependencies: [ + .byName(name: "Tauri") + ], + path: "Sources") + ] +) diff --git a/plugins/log/ios/README.md b/plugins/log/ios/README.md new file mode 100644 index 00000000..578d9ffa --- /dev/null +++ b/plugins/log/ios/README.md @@ -0,0 +1,3 @@ +# Log + +Exposes a function log a message using the OSLog API. diff --git a/plugins/log/ios/Sources/LogPlugin.swift b/plugins/log/ios/Sources/LogPlugin.swift new file mode 100644 index 00000000..04c98aa1 --- /dev/null +++ b/plugins/log/ios/Sources/LogPlugin.swift @@ -0,0 +1,13 @@ +import UIKit +import Tauri +import SwiftRs + +@_cdecl("tauri_log") +func log(level: Int, message: UnsafePointer) { + switch level { + case 1: Logger.debug(message.pointee.to_string()) + case 2: Logger.info(message.pointee.to_string()) + case 3: Logger.error(message.pointee.to_string()) + default: break + } +} diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index f01bd1ac..86c2b618 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -22,6 +22,11 @@ use tauri::{ pub use fern; +#[cfg(target_os = "ios")] +extern "C" { + fn tauri_log(level: u8, message: &swift_rs::SRString); +} + const DEFAULT_MAX_FILE_SIZE: u128 = 40000; const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne; const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir]; @@ -260,29 +265,18 @@ impl Builder { } #[cfg(target_os = "ios")] LogTarget::Stdout | LogTarget::Stderr => { - use std::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('.'); - } - } - subsystem.push_str(&app_handle.package_info().crate_name); - 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); + unsafe { + tauri_log( + match record.level() { + log::Level::Trace | log::Level::Debug => 1, + log::Level::Info => 2, + log::Level::Warn | log::Level::Error => 3, + }, + &message.as_str().into(), + ); + } }) } #[cfg(desktop)]