From eef9cc855d64cecd5b183d40e1a325c5d3e63c36 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 21:59:49 -0400 Subject: [PATCH 1/7] feat: add log deps --- Cargo.toml | 2 ++ crates/bevy_editor_pls/Cargo.toml | 1 + 2 files changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 1e983b5..0dd2f3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ bevy_editor_pls_default_windows = { version = "0.6.0", path = "crates/bevy_edito bevy-inspector-egui = "0.21.0" egui = "0.23" egui-gizmo = "0.12" +log = "0.4" +egui_extras = "0.23" [profile.dev.package."*"] opt-level = 2 diff --git a/crates/bevy_editor_pls/Cargo.toml b/crates/bevy_editor_pls/Cargo.toml index 2ec7011..a44ecb6 100644 --- a/crates/bevy_editor_pls/Cargo.toml +++ b/crates/bevy_editor_pls/Cargo.toml @@ -28,6 +28,7 @@ egui-gizmo.workspace = true # bevy_framepace = { version = "0.12", default-features = false } [dev-dependencies] +log.workspace = true bevy = { version = "0.12", default-features = false, features = [ "bevy_winit", "bevy_core_pipeline", From f4c03e9a9dbbf676cb5535d7396e4c725c3ea84a Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 22:01:01 -0400 Subject: [PATCH 2/7] feat: create selectable widget --- crates/bevy_editor_pls_core/src/lib.rs | 2 + crates/bevy_editor_pls_core/src/widgets.rs | 1 + .../src/widgets/selectable.rs | 73 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 crates/bevy_editor_pls_core/src/widgets.rs create mode 100644 crates/bevy_editor_pls_core/src/widgets/selectable.rs diff --git a/crates/bevy_editor_pls_core/src/lib.rs b/crates/bevy_editor_pls_core/src/lib.rs index 4b91e5a..aad7095 100644 --- a/crates/bevy_editor_pls_core/src/lib.rs +++ b/crates/bevy_editor_pls_core/src/lib.rs @@ -3,6 +3,8 @@ pub mod editor; /// Trait definition for new editor windows pub mod editor_window; +pub mod widgets; + use std::marker::PhantomData; use bevy::prelude::*; diff --git a/crates/bevy_editor_pls_core/src/widgets.rs b/crates/bevy_editor_pls_core/src/widgets.rs new file mode 100644 index 0000000..17d0089 --- /dev/null +++ b/crates/bevy_editor_pls_core/src/widgets.rs @@ -0,0 +1 @@ +pub mod selectable; diff --git a/crates/bevy_editor_pls_core/src/widgets/selectable.rs b/crates/bevy_editor_pls_core/src/widgets/selectable.rs new file mode 100644 index 0000000..ccaa29f --- /dev/null +++ b/crates/bevy_editor_pls_core/src/widgets/selectable.rs @@ -0,0 +1,73 @@ +#![allow(dead_code)] +use bevy_inspector_egui::egui::{self, Button, Color32, KeyboardShortcut, Layout, Rounding, Vec2}; + +#[derive(Clone, PartialEq, Eq)] +pub struct Selectable { + tabs: Vec<(String, T, Option)>, + changed: bool, + font_size: u32, + space: u32, + selected_color: Color32, + selected: Option, +} + +impl Selectable +where + T: Clone + PartialEq, +{ + pub fn new( + tabs: &[(N, T, Option)], + default_selected: T, + font_size: u32, + space: u32, + selected_color: Color32, + ) -> Self { + Self { + space, + font_size, + selected_color, + changed: false, + selected: Some(default_selected), + tabs: tabs + .iter() + .map(|t| (t.0.to_string(), t.1.clone(), t.2.clone())) + .collect(), + } + } + + pub fn add_tab(&mut self, pos: usize, name: &str, tab: T, shortcut: Option) { + self.tabs.insert(pos, (name.to_string(), tab, shortcut)); + } + + pub fn remove_tab(&mut self, pos: usize) { + self.tabs.remove(pos); + } + + pub fn show(&mut self, ui: &mut egui::Ui) -> Option { + ui.horizontal(|ui| { + ui.with_layout(Layout::left_to_right(egui::Align::Center), |ui| { + ui.style_mut().visuals.button_frame = false; + for (tab, d, shortcut) in self.tabs.iter() { + let item = if self.selected.as_ref().is_some_and(|s| s == d) { + Button::new(tab) + .fill(self.selected_color) + .min_size(Vec2::splat(self.font_size as f32)) + .rounding(Rounding::ZERO) + } else { + Button::new(tab) + .min_size(Vec2::splat(self.font_size as f32)) + .rounding(Rounding::ZERO) + }; + let item = ui.add(item); + ui.add_space(self.space as f32); + if item.clicked() + || shortcut.is_some_and(|s| ui.input_mut(|i| i.consume_shortcut(&s))) + { + self.selected = Some(d.clone()); + } + } + }); + }); + self.selected.clone() + } +} From 22fd0b7dd241ae887df736e1eea80dd62f0836c5 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 22:01:38 -0400 Subject: [PATCH 3/7] feat: add console log deps --- crates/bevy_editor_pls_default_windows/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_editor_pls_default_windows/Cargo.toml b/crates/bevy_editor_pls_default_windows/Cargo.toml index 09aea1f..ee54e7c 100644 --- a/crates/bevy_editor_pls_default_windows/Cargo.toml +++ b/crates/bevy_editor_pls_default_windows/Cargo.toml @@ -31,4 +31,6 @@ indexmap = "2" pretty-type-name = "1.0" bevy_mod_debugdump = "0.9" opener = "0.6.0" +log.workspace = true egui-gizmo.workspace = true +egui_extras.workspace = true From 0c8a00b942013e6b84e62c4d2f8161270b6f1ab2 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 22:02:31 -0400 Subject: [PATCH 4/7] feat: implement console_log ui and resources needed --- .../src/console_log.rs | 48 +++++++ .../src/console_log/console.rs | 86 +++++++++++ .../src/console_log/logs.rs | 133 ++++++++++++++++++ .../src/lib.rs | 1 + 4 files changed, 268 insertions(+) create mode 100644 crates/bevy_editor_pls_default_windows/src/console_log.rs create mode 100644 crates/bevy_editor_pls_default_windows/src/console_log/console.rs create mode 100644 crates/bevy_editor_pls_default_windows/src/console_log/logs.rs diff --git a/crates/bevy_editor_pls_default_windows/src/console_log.rs b/crates/bevy_editor_pls_default_windows/src/console_log.rs new file mode 100644 index 0000000..74cb18d --- /dev/null +++ b/crates/bevy_editor_pls_default_windows/src/console_log.rs @@ -0,0 +1,48 @@ +mod console; +mod logs; + +use bevy::prelude::{App, World}; +use bevy_editor_pls_core::editor_window::{EditorWindow, EditorWindowContext}; +use bevy_inspector_egui::egui::Ui; +use console::*; + +pub use logs::*; + +pub fn setup(app: &mut App) { + let logger = Logs::default(); + + app.insert_resource(logger.clone()); + + log::set_max_level(log::LevelFilter::Trace); + + if let Err(e) = log::set_boxed_logger(Box::from(logger)) { + println!("Error setting boxed logger: {e:?}"); + } +} + +pub struct ConsoleLogState { + pub(crate) filter: log::Level, +} + +impl Default for ConsoleLogState { + fn default() -> Self { + Self { + filter: log::Level::Trace, + } + } +} + +pub struct ConsoleLogWindow; +impl EditorWindow for ConsoleLogWindow { + type State = ConsoleLogState; + const NAME: &'static str = "Console"; + + fn ui(world: &mut World, mut ctx: EditorWindowContext, ui: &mut Ui) { + let state_mut = ctx.state_mut::(); + let state = state_mut.unwrap(); + + let logs_res = world.resource::(); + + draw_console_logs(ui, &mut state.filter, logs_res); + } +} diff --git a/crates/bevy_editor_pls_default_windows/src/console_log/console.rs b/crates/bevy_editor_pls_default_windows/src/console_log/console.rs new file mode 100644 index 0000000..a9a8252 --- /dev/null +++ b/crates/bevy_editor_pls_default_windows/src/console_log/console.rs @@ -0,0 +1,86 @@ +use bevy_editor_pls_core::widgets::selectable::Selectable; +use bevy_inspector_egui::egui::{Align, Color32, Layout, Ui}; +use egui_extras::{Column, TableBuilder}; + +use super::Logs; + +pub fn draw_console_logs(ui: &mut Ui, filter: &mut log::Level, logs: &Logs) { + let map_logs = logs.get_logs(); + + ui.horizontal(|ui| { + if let Some(s) = Selectable::new( + &[ + ("trace", log::Level::Trace, None), + ("info", log::Level::Info, None), + ("warn", log::Level::Warn, None), + ("error", log::Level::Error, None), + ], + *filter, + 14, + 0, + Color32::DARK_GRAY, + ) + .show(ui) + { + *filter = s; + } + ui.with_layout(Layout::right_to_left(Align::Center), |ui| { + if ui.button("Clear").clicked() { + logs.clear(); + } + }); + }); + + ui.separator(); + + let table = TableBuilder::new(ui) + .striped(true) + .resizable(!map_logs.is_empty()) + .cell_layout(Layout::left_to_right(Align::Center)) + .column(Column::initial(30.0).resizable(false)) + .column(Column::initial(50.0).resizable(false)) + .column(Column::initial(100.0).at_least(40.0).clip(true)) + .column(Column::remainder().clip(true)) + .min_scrolled_height(0.0); + + table + .header(20.0, |mut header| { + header.col(|ui| { + ui.strong(""); + }); + header.col(|ui| { + ui.strong("Level"); + }); + header.col(|ui| { + ui.strong("File"); + }); + header.col(|ui| { + ui.strong("Details"); + }); + }) + .body(|mut body| { + for (log, n) in &map_logs { + if *filter < log.level_log { + continue; + } + body.row(30., |mut row| { + row.col(|ui| { + if n >= &99 { + ui.label("+99"); + } else if n > &0 { + ui.label(n.to_string()); + } + }); + row.col(|ui| { + ui.label(log.level_log.to_string()); + }); + row.col(|ui| { + ui.label(format!("{}:{}", log.file, log.line)); + }); + row.col(|ui| { + ui.label(&log.details); + }); + }); + } + }); +} diff --git a/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs b/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs new file mode 100644 index 0000000..2e435a4 --- /dev/null +++ b/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs @@ -0,0 +1,133 @@ +use std::str::FromStr; +use std::sync::{Arc, OnceLock, RwLock}; + +use bevy::prelude::*; +use bevy::utils::HashMap; + +static FILTER: OnceLock = OnceLock::new(); + +/// Set the modules filters +/// +/// You need set filters separated by `;` +/// +/// Example: +/// ```no_run +/// set_module_filter("wgpu=off;my_super_game=trace"); +/// ``` +/// +/// * `new_filter`: The filter value +pub fn set_module_filter(new_filter: T) { + FILTER.set(new_filter.to_string()).unwrap(); +} + +#[derive(Clone, Debug, Resource)] +pub struct Logs { + logs: Arc>>, + filter_modules: HashMap>, +} + +impl Default for Logs { + fn default() -> Self { + let mut filter_modules = HashMap::new(); + filter_modules.insert(env!("CARGO_PKG_NAME").to_string(), Some(log::Level::Trace)); + + let external_filters = FILTER.get_or_init(String::new); + filter_modules.extend( + external_filters + .split(';') + .flat_map(|s| { + let Some((k, v)) = s.split_once('=') else { + return None; + }; + Some(( + k.trim().to_string(), + log::Level::from_str(v.trim()).map(Some).unwrap_or(None), + )) + }) + .into_iter(), + ); + + Self { + logs: Default::default(), + filter_modules, + } + } +} + +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub struct LogItem { + pub level_log: log::Level, + pub module: String, + pub file: String, + pub line: String, + pub details: String, +} + +impl<'a> From<&log::Record<'a>> for LogItem { + fn from(v: &log::Record) -> Self { + Self { + level_log: v.level(), + module: v.module_path().unwrap_or_default().to_string(), + file: v.file().unwrap_or_default().to_string(), + line: v.line().unwrap_or_default().to_string(), + details: v.args().to_string(), + } + } +} + +impl Logs { + pub fn clear(&self) { + let mut logs = self.logs.write().unwrap(); + logs.clear(); + drop(logs); + } + + pub fn len(&self) -> usize { + let logs = self.logs.read().unwrap(); + let len = logs.len(); + drop(logs); + len + } + + pub fn get_logs(&self) -> HashMap { + let logs = self.logs.read().unwrap(); + let v = logs.clone(); + drop(logs); + v + } +} + +impl log::Log for Logs { + fn enabled(&self, _metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + if !self.enabled(record.metadata()) { + return; + } + + // Check if NameModule of record has in filter modules and have correct level + if !record.module_path().is_some_and(|m| { + self.filter_modules.iter().any(|(name, filter)| { + m.starts_with(name) && filter.is_some_and(|f| record.level() <= f) + }) + }) { + return; + } + + let mut logs = self.logs.write().unwrap(); + let item = LogItem::from(record); + (*logs) + .entry(item) + .and_modify(|c| { + if *c <= 99 { + *c += 1; + } + }) + .or_insert(0); + drop(logs); + } + + fn flush(&self) {} +} diff --git a/crates/bevy_editor_pls_default_windows/src/lib.rs b/crates/bevy_editor_pls_default_windows/src/lib.rs index e3fc2cb..003dbfe 100644 --- a/crates/bevy_editor_pls_default_windows/src/lib.rs +++ b/crates/bevy_editor_pls_default_windows/src/lib.rs @@ -4,6 +4,7 @@ pub mod add; pub mod assets; pub mod cameras; +pub mod console_log; pub mod debug_settings; pub mod diagnostics; pub mod gizmos; From f7b8442ec2df33d777f68bda962bee2855568e06 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 22:03:23 -0400 Subject: [PATCH 5/7] feat: use console_log on default implementation --- crates/bevy_editor_pls/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/bevy_editor_pls/src/lib.rs b/crates/bevy_editor_pls/src/lib.rs index f479693..86a7306 100644 --- a/crates/bevy_editor_pls/src/lib.rs +++ b/crates/bevy_editor_pls/src/lib.rs @@ -112,6 +112,9 @@ impl Plugin for EditorPlugin { use bevy_editor_pls_default_windows::renderer::RendererWindow; use bevy_editor_pls_default_windows::resources::ResourcesWindow; use bevy_editor_pls_default_windows::scenes::SceneWindow; + use bevy_editor_pls_default_windows::console_log; + + console_log::setup(app); app.add_editor_window::(); app.add_editor_window::(); @@ -125,6 +128,7 @@ impl Plugin for EditorPlugin { app.add_editor_window::(); app.add_editor_window::(); app.add_editor_window::(); + app.add_editor_window::(); app.add_plugins(bevy::pbr::wireframe::WireframePlugin); @@ -145,6 +149,7 @@ impl Plugin for EditorPlugin { std::any::TypeId::of::(), std::any::TypeId::of::(), std::any::TypeId::of::(), + std::any::TypeId::of::(), ], ); } From c0b3778b8649b95c22ae4d249bdfa80ed0470351 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Fri, 10 Nov 2023 22:05:52 -0400 Subject: [PATCH 6/7] chore: add usage console_log into examples --- crates/bevy_editor_pls/examples/2d_3d_mixed.rs | 7 ++++++- crates/bevy_editor_pls/examples/basic.rs | 6 +++++- crates/bevy_editor_pls/examples/breakout.rs | 6 +++++- crates/bevy_editor_pls/examples/controls.rs | 6 +++++- crates/bevy_editor_pls/examples/empty.rs | 3 ++- crates/bevy_editor_pls/examples/load_gltf.rs | 6 +++++- crates/bevy_editor_pls/examples/separate_window.rs | 6 +++++- crates/bevy_editor_pls/examples/ui.rs | 6 +++++- 8 files changed, 38 insertions(+), 8 deletions(-) diff --git a/crates/bevy_editor_pls/examples/2d_3d_mixed.rs b/crates/bevy_editor_pls/examples/2d_3d_mixed.rs index 8c6ce22..30843c5 100644 --- a/crates/bevy_editor_pls/examples/2d_3d_mixed.rs +++ b/crates/bevy_editor_pls/examples/2d_3d_mixed.rs @@ -1,14 +1,18 @@ +use bevy::log::LogPlugin; use bevy::prelude::*; fn main() { + console_log::set_module_filter("2d_3d_mixed=trace"); + App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(bevy_editor_pls::EditorPlugin::new()) .add_systems(Startup, (setup_2d, setup_3d)) .run(); } fn setup_2d(mut commands: Commands) { + log::info!("Start Setup Example 2D"); commands.spawn(Camera2dBundle::default()); commands.spawn(SpriteBundle { sprite: Sprite { @@ -25,6 +29,7 @@ fn setup_3d( mut meshes: ResMut>, mut materials: ResMut>, ) { + log::info!("Start Setup Example 3D"); commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane::from_size(0.5))), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), diff --git a/crates/bevy_editor_pls/examples/basic.rs b/crates/bevy_editor_pls/examples/basic.rs index cd95492..c6473a1 100644 --- a/crates/bevy_editor_pls/examples/basic.rs +++ b/crates/bevy_editor_pls/examples/basic.rs @@ -1,3 +1,4 @@ +use bevy::log::LogPlugin; use bevy::{ diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin}, prelude::*, @@ -8,8 +9,10 @@ use bevy::{ }, }; use bevy_editor_pls::prelude::*; +use bevy_editor_pls_default_windows::console_log; fn main() { + console_log::set_module_filter("wgpu_core=off;basic=trace"); // enable wireframe rendering let mut wgpu_settings = WgpuSettings::default(); wgpu_settings.features |= WgpuFeatures::POLYGON_MODE_LINE; @@ -17,7 +20,7 @@ fn main() { App::new() .add_plugins(DefaultPlugins.set(RenderPlugin { render_creation: RenderCreation::Automatic(wgpu_settings), - })) + }).disable::()) .add_plugins(( EditorPlugin::new(), FrameTimeDiagnosticsPlugin, @@ -33,6 +36,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + log::info!("Start Setup Example"); // plane commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))), diff --git a/crates/bevy_editor_pls/examples/breakout.rs b/crates/bevy_editor_pls/examples/breakout.rs index 5252971..5be1623 100644 --- a/crates/bevy_editor_pls/examples/breakout.rs +++ b/crates/bevy_editor_pls/examples/breakout.rs @@ -1,5 +1,6 @@ //! A simplified implementation of the classic game "Breakout". +use bevy::log::LogPlugin; use bevy::{ prelude::*, sprite::collide_aabb::{collide, Collision}, @@ -49,8 +50,10 @@ const TEXT_COLOR: Color = Color::rgb(0.5, 0.5, 1.0); const SCORE_COLOR: Color = Color::rgb(1.0, 0.5, 0.5); fn main() { + console_log::set_module_filter("breakout=trace"); + App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(EditorPlugin::new()) .insert_resource(Scoreboard { score: 0 }) .insert_resource(ClearColor(BACKGROUND_COLOR)) @@ -175,6 +178,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + log::info!("Start Setup Example"); // Camera commands.spawn(Camera2dBundle::default()); diff --git a/crates/bevy_editor_pls/examples/controls.rs b/crates/bevy_editor_pls/examples/controls.rs index aa68e72..7edca7b 100644 --- a/crates/bevy_editor_pls/examples/controls.rs +++ b/crates/bevy_editor_pls/examples/controls.rs @@ -1,9 +1,12 @@ use bevy::prelude::*; +use bevy::log::LogPlugin; use bevy_editor_pls::{controls, EditorPlugin}; fn main() { + console_log::set_module_filter("controls=trace"); + App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(EditorPlugin::new()) .insert_resource(editor_controls()) .add_systems(Startup, (set_cam3d_controls, setup)) @@ -41,6 +44,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + log::info!("Start Setup Example"); // plane commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))), diff --git a/crates/bevy_editor_pls/examples/empty.rs b/crates/bevy_editor_pls/examples/empty.rs index e758ffc..d5f3e4d 100644 --- a/crates/bevy_editor_pls/examples/empty.rs +++ b/crates/bevy_editor_pls/examples/empty.rs @@ -1,9 +1,10 @@ use bevy::prelude::*; +use bevy::log::LogPlugin; use bevy_editor_pls::prelude::*; fn main() { App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(EditorPlugin::new()) .run(); } diff --git a/crates/bevy_editor_pls/examples/load_gltf.rs b/crates/bevy_editor_pls/examples/load_gltf.rs index 763867a..41016af 100644 --- a/crates/bevy_editor_pls/examples/load_gltf.rs +++ b/crates/bevy_editor_pls/examples/load_gltf.rs @@ -2,19 +2,22 @@ use std::f32::consts::*; +use bevy::log::LogPlugin; use bevy::{ pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap}, prelude::*, }; fn main() { + console_log::set_module_filter("load_gltf=trace"); + App::new() .insert_resource(AmbientLight { color: Color::WHITE, brightness: 1.0 / 5.0f32, }) .insert_resource(DirectionalLightShadowMap { size: 4096 }) - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(bevy_editor_pls::EditorPlugin::new()) .add_systems(Startup, setup) .add_systems(Update, animate_light_direction) @@ -22,6 +25,7 @@ fn main() { } fn setup(mut commands: Commands, asset_server: Res) { + log::info!("Start Setup Example"); commands.spawn(( Camera3dBundle { transform: Transform::from_xyz(0.7, 0.7, 1.0) diff --git a/crates/bevy_editor_pls/examples/separate_window.rs b/crates/bevy_editor_pls/examples/separate_window.rs index afeb849..0bc9766 100644 --- a/crates/bevy_editor_pls/examples/separate_window.rs +++ b/crates/bevy_editor_pls/examples/separate_window.rs @@ -1,9 +1,12 @@ +use bevy::log::LogPlugin; use bevy::prelude::*; use bevy_editor_pls::EditorPlugin; fn main() { + console_log::set_module_filter("separate_window=trace"); + App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) // .add_plugin(EditorPlugin::new().in_separate_window()) .add_plugins(EditorPlugin::new().in_new_window(Window::default())) .add_systems(Startup, setup) @@ -16,6 +19,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + log::info!("Start Setup Example"); // plane commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))), diff --git a/crates/bevy_editor_pls/examples/ui.rs b/crates/bevy_editor_pls/examples/ui.rs index 70e9aa0..02e88fc 100644 --- a/crates/bevy_editor_pls/examples/ui.rs +++ b/crates/bevy_editor_pls/examples/ui.rs @@ -1,5 +1,6 @@ //! This example illustrates the various features of Bevy UI. +use bevy::log::LogPlugin; use bevy::{ input::mouse::{MouseScrollUnit, MouseWheel}, prelude::*, @@ -8,8 +9,10 @@ use bevy::{ use bevy_editor_pls::EditorPlugin; fn main() { + console_log::set_module_filter("ui=trace"); + App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.build().disable::()) .add_plugins(EditorPlugin::new()) // Only run the app when there is user input. This will significantly reduce CPU/GPU use. .insert_resource(WinitSettings::desktop_app()) @@ -19,6 +22,7 @@ fn main() { } fn setup(mut commands: Commands, asset_server: Res) { + log::info!("Start Setup Example"); // Camera commands.spawn(Camera2dBundle::default()); From 56d2a48330d46cf0cd0764692973a67d6a2570f7 Mon Sep 17 00:00:00 2001 From: Sergio Ribera <56278796+SergioRibera@users.noreply.github.com> Date: Thu, 7 Dec 2023 16:38:24 -0400 Subject: [PATCH 7/7] wip: replace log by bevy trace --- Cargo.toml | 1 - crates/bevy_editor_pls/Cargo.toml | 1 - .../Cargo.toml | 1 - .../src/console_log/logs.rs | 53 +++++++++++-------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0dd2f3c..69391ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ bevy_editor_pls_default_windows = { version = "0.6.0", path = "crates/bevy_edito bevy-inspector-egui = "0.21.0" egui = "0.23" egui-gizmo = "0.12" -log = "0.4" egui_extras = "0.23" [profile.dev.package."*"] diff --git a/crates/bevy_editor_pls/Cargo.toml b/crates/bevy_editor_pls/Cargo.toml index a44ecb6..2ec7011 100644 --- a/crates/bevy_editor_pls/Cargo.toml +++ b/crates/bevy_editor_pls/Cargo.toml @@ -28,7 +28,6 @@ egui-gizmo.workspace = true # bevy_framepace = { version = "0.12", default-features = false } [dev-dependencies] -log.workspace = true bevy = { version = "0.12", default-features = false, features = [ "bevy_winit", "bevy_core_pipeline", diff --git a/crates/bevy_editor_pls_default_windows/Cargo.toml b/crates/bevy_editor_pls_default_windows/Cargo.toml index ee54e7c..c218601 100644 --- a/crates/bevy_editor_pls_default_windows/Cargo.toml +++ b/crates/bevy_editor_pls_default_windows/Cargo.toml @@ -31,6 +31,5 @@ indexmap = "2" pretty-type-name = "1.0" bevy_mod_debugdump = "0.9" opener = "0.6.0" -log.workspace = true egui-gizmo.workspace = true egui_extras.workspace = true diff --git a/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs b/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs index 2e435a4..937e8c7 100644 --- a/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs +++ b/crates/bevy_editor_pls_default_windows/src/console_log/logs.rs @@ -3,6 +3,8 @@ use std::sync::{Arc, OnceLock, RwLock}; use bevy::prelude::*; use bevy::utils::HashMap; +use bevy::log; +use bevy::utils::tracing::{Subscriber, span}; static FILTER: OnceLock = OnceLock::new(); @@ -23,13 +25,13 @@ pub fn set_module_filter(new_filter: T) { #[derive(Clone, Debug, Resource)] pub struct Logs { logs: Arc>>, - filter_modules: HashMap>, + filter_modules: HashMap>, } impl Default for Logs { fn default() -> Self { let mut filter_modules = HashMap::new(); - filter_modules.insert(env!("CARGO_PKG_NAME").to_string(), Some(log::Level::Trace)); + filter_modules.insert(env!("CARGO_PKG_NAME").to_string(), Some(log::Level::TRACE)); let external_filters = FILTER.get_or_init(String::new); filter_modules.extend( @@ -63,8 +65,8 @@ pub struct LogItem { pub details: String, } -impl<'a> From<&log::Record<'a>> for LogItem { - fn from(v: &log::Record) -> Self { +impl<'a> From<&span::Record<'a>> for LogItem { + fn from(v: &span::Record<'a>) -> Self { Self { level_log: v.level(), module: v.module_path().unwrap_or_default().to_string(), @@ -97,25 +99,16 @@ impl Logs { } } -impl log::Log for Logs { - fn enabled(&self, _metadata: &log::Metadata) -> bool { - true - } - - fn log(&self, record: &log::Record) { - if !self.enabled(record.metadata()) { - return; - } - - // Check if NameModule of record has in filter modules and have correct level - if !record.module_path().is_some_and(|m| { +impl Subscriber for Logs { + fn enabled(&self, metadata: &bevy::utils::tracing::Metadata<'_>) -> bool { + !metadata.module_path().is_some_and(|m| { self.filter_modules.iter().any(|(name, filter)| { - m.starts_with(name) && filter.is_some_and(|f| record.level() <= f) + m.starts_with(name) && filter.is_some_and(|f| metadata.level() <= &f) }) - }) { - return; - } + }) + } + fn record(&self, span: &span::Id, record: &span::Record<'_>) { let mut logs = self.logs.write().unwrap(); let item = LogItem::from(record); (*logs) @@ -129,5 +122,23 @@ impl log::Log for Logs { drop(logs); } - fn flush(&self) {} + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + todo!() + } + + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + todo!() + } + + fn event(&self, event: &bevy::utils::tracing::Event<'_>) { + todo!() + } + + fn enter(&self, span: &span::Id) { + todo!() + } + + fn exit(&self, span: &span::Id) { + todo!() + } }