From 6a39c33d4929a31b799887508b48238ff08359c3 Mon Sep 17 00:00:00 2001 From: Andrew <635596+therealbnut@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:58:14 +1100 Subject: [PATCH] Use oslog for ios (#13364) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective On mobile devices, it's best to use the OS's native logging due to the difficulty of accessing the console. This is already done for Android. This is an updated version of https://github.com/bevyengine/bevy/pull/4462. ## Solution This PR uses Absolucy's [tracing-oslog](https://github.com/Absolucy/tracing-oslog) ([ZLib license](https://github.com/Absolucy/tracing-oslog/blob/main/LICENSE.md)) for iOS in order to use Apple's `os_log`. ## Testing I ran `examples/mobile` with the logging from `examples/app/logs.rs` on an iOS device, I then checked the logs could be filtered in the MacOS Console.app. ## Changelog - Change bevy_log to use Apple's os_log on iOS. ## Questions for Reviewers It's worth noting that the dependency this adds hasn't had bug fixes released in a few years, so we may want to consider one or more of: 1. a feature flag to opt-in, and it would also allow `os_log` on MacOS 2. merge as-is and have some (minor?) upstream bugs 3. hold off on this PR until a suitable alternative dependency arises 4. maintain our own implementation ## Future work In a follow-up PR it might be good to make the `subsystem` field have a better default value, like [this one](https://github.com/bevyengine/bevy/blob/main/examples/mobile/bevy_mobile_example.xcodeproj/project.pbxproj#L363). That value can be retrieved programmatically if we bind another system API (For posterity in Swift this is `Bundle.main.bundleIdentifier`, but the C/ObjC equivalent is likely easier to bind). This would almost always be the correct value, while the current default is unlikely to ever be correct. --------- Co-authored-by: Dusty DeWeese Co-authored-by: Alice Cecile Co-authored-by: Carter Anderson Co-authored-by: François Mockers --- Cargo.toml | 2 +- crates/bevy_ecs/Cargo.toml | 2 +- crates/bevy_log/Cargo.toml | 3 +++ crates/bevy_log/src/lib.rs | 11 ++++++++++- examples/mobile/src/lib.rs | 32 +++++++++++++++++++++----------- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1f548f505c..bffdaf531a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -458,7 +458,7 @@ bytemuck = "1.7" bevy_render = { path = "crates/bevy_render", version = "0.15.0-dev", default-features = false } # Needed to poll Task examples futures-lite = "2.0.1" -async-std = "1.12" +async-std = "1.13" crossbeam-channel = "0.5.0" argh = "0.1.12" thiserror = "1.0" diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index b3939d9789..bcc075354f 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -28,7 +28,7 @@ bevy_ecs_macros = { path = "macros", version = "0.15.0-dev" } petgraph = "0.6" bitflags = "2.3" -concurrent-queue = "2.4.0" +concurrent-queue = "2.5.0" disqualified = "1.0" fixedbitset = "0.5" serde = { version = "1", optional = true, default-features = false } diff --git a/crates/bevy_log/Cargo.toml b/crates/bevy_log/Cargo.toml index d996a78221..173d13e6ba 100644 --- a/crates/bevy_log/Cargo.toml +++ b/crates/bevy_log/Cargo.toml @@ -36,6 +36,9 @@ android_log-sys = "0.3.0" [target.'cfg(target_arch = "wasm32")'.dependencies] tracing-wasm = "0.2.1" +[target.'cfg(target_os = "ios")'.dependencies] +tracing-oslog = "0.2" + [lints] workspace = true diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index 32bdb9a122..118040f10f 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -227,7 +227,11 @@ impl Plugin for LogPlugin { #[cfg(feature = "trace")] let subscriber = subscriber.with(tracing_error::ErrorLayer::default()); - #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] + #[cfg(all( + not(target_arch = "wasm32"), + not(target_os = "android"), + not(target_os = "ios") + ))] { #[cfg(feature = "tracing-chrome")] let chrome_layer = { @@ -290,6 +294,11 @@ impl Plugin for LogPlugin { finished_subscriber = subscriber.with(android_tracing::AndroidLayer::default()); } + #[cfg(target_os = "ios")] + { + finished_subscriber = subscriber.with(tracing_oslog::OsLogger::default()); + } + let logger_already_set = LogTracer::init().is_err(); let subscriber_already_set = bevy_utils::tracing::subscriber::set_global_default(finished_subscriber).is_err(); diff --git a/examples/mobile/src/lib.rs b/examples/mobile/src/lib.rs index 505981167b..511e50ad59 100644 --- a/examples/mobile/src/lib.rs +++ b/examples/mobile/src/lib.rs @@ -3,6 +3,7 @@ use bevy::{ color::palettes::basic::*, input::{gestures::RotationGesture, touch::TouchPhase}, + log::{Level, LogPlugin}, prelude::*, window::{AppLifecycle, WindowMode}, }; @@ -11,17 +12,26 @@ use bevy::{ #[bevy_main] fn main() { let mut app = App::new(); - app.add_plugins(DefaultPlugins.set(WindowPlugin { - primary_window: Some(Window { - resizable: false, - mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary), - // on iOS, gestures must be enabled. - // This doesn't work on Android - recognize_rotation_gesture: true, - ..default() - }), - ..default() - })) + app.add_plugins( + DefaultPlugins + .set(LogPlugin { + // This will show some log events from Bevy to the native logger. + level: Level::DEBUG, + filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(), + ..Default::default() + }) + .set(WindowPlugin { + primary_window: Some(Window { + resizable: false, + mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary), + // on iOS, gestures must be enabled. + // This doesn't work on Android + recognize_rotation_gesture: true, + ..default() + }), + ..default() + }), + ) .add_systems(Startup, (setup_scene, setup_music)) .add_systems(Update, (touch_camera, button_handler, handle_lifetime)) .run();