fix(log): use Swift OSLog instead of oslog Rust binding (#262)

This commit is contained in:
Lucas Fernandes Nogueira
2023-02-23 09:17:15 -08:00
committed by GitHub
parent 22f987bf24
commit 961602bd1b
8 changed files with 117 additions and 89 deletions
+4 -1
View File
@@ -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"]
+12 -9
View File
@@ -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);
}
}
+11
View File
@@ -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
+31
View File
@@ -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")
]
)
+3
View File
@@ -0,0 +1,3 @@
# Log
Exposes a function log a message using the OSLog API.
+13
View File
@@ -0,0 +1,13 @@
import UIKit
import Tauri
import SwiftRs
@_cdecl("tauri_log")
func log(level: Int, message: UnsafePointer<SRString>) {
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
}
}
+15 -21
View File
@@ -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<HashMap<String, oslog::OsLog>> = 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)]