Skip to content

Commit

Permalink
Tracing integration (#95)
Browse files Browse the repository at this point in the history
* Setup tracing

* Add logging
  • Loading branch information
matthunz authored Jul 16, 2024
1 parent 23c22de commit c73e0af
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parley = { git = "https://github.com/nicoburns/parley", rev = "a80765f070ab41132
dioxus = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef"}
dioxus-ssr = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef" }
tokio = { version = "1.25.0", features = ["full"] }
tracing = "0.1.40"
vello = { version = "0.1", features = ["wgpu"] }
peniko = { version = "0.1" }
# fello = { git = "https://github.com/linebender/vello" }
Expand Down Expand Up @@ -50,6 +51,7 @@ dioxus = { workspace = true }
euclid = { version = "0.22", features = ["serde"] }
reqwest = "0.11.24"
tokio = { version = "1.36.0", features = ["full"] }
tracing-subscriber = "0.3"
ureq = "2.9"

# [patch.crates-io]
Expand Down
2 changes: 2 additions & 0 deletions examples/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use dioxus::prelude::*;

fn main() {
tracing_subscriber::fmt::init();

dioxus_blitz::launch(app);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "blitz"
version = "0.1.0"
edition = "2021"

[features]
default = ["tracing"]
tracing = ["dep:tracing"]

[dependencies]
slab = "0.4.9"
style = { workspace = true, features = ["servo"] }
Expand All @@ -11,6 +15,7 @@ selectors = { workspace = true }
taffy = { workspace = true }
parley = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true, optional = true }
vello = { workspace = true }
wgpu = { workspace = true }

Expand Down
18 changes: 11 additions & 7 deletions packages/blitz/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,11 @@ where
}
}

dbg!(&node.final_layout);
dbg!(&node.style);
#[cfg(feature = "tracing")]
{
tracing::info!("Layout: {:?}", &node.final_layout);
tracing::info!("Style: {:?}", &node.style);
}

println!("Node {} {}", node.id, node.node_debug_str());
if node.is_inline_root {
Expand Down Expand Up @@ -655,8 +658,7 @@ where
.inline_layout
.as_ref()
.unwrap_or_else(|| {
dbg!(&element);
panic!("Tried to render node marked as inline root that does not have an inline layout");
panic!("Tried to render node marked as inline root that does not have an inline layout: {:?}", element);
});

// Apply padding/border offset to inline root
Expand Down Expand Up @@ -1135,9 +1137,11 @@ impl ElementCx<'_> {
+ multiplier * (color.a as f32 - last_stop.color.a as f32))
as u8,
);
gradient.stops.push(
dbg! {peniko::ColorStop { color: mid_color, offset: mid_offset }},
);
tracing::info!("Gradient stop {:?}", mid_color);
gradient.stops.push(peniko::ColorStop {
color: mid_color,
offset: mid_offset,
});
gradient.stops.push(peniko::ColorStop { color, offset });
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/dioxus-blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"
accessibility = ["dep:accesskit", "dep:accesskit_winit"]
hot-reload = ["dep:dioxus-cli-config", "dep:dioxus-hot-reload"]
menu = ["dep:muda"]
default = ["accessibility", "menu"]
tracing = ["dep:tracing"]
default = ["accessibility", "hot-reload", "menu", "tracing"]

[dependencies]
accesskit = { version = "0.15.0", optional = true }
Expand All @@ -22,6 +23,7 @@ futures-util = "0.3.30"
vello = { workspace = true }
wgpu = { workspace = true }
style = { workspace = true }
tracing = { workspace = true, optional = true }
blitz = { path = "../blitz" }
blitz-dom = { path = "../dom" }
url = { version = "2.5.0", features = ["serde"] }
Expand Down
72 changes: 54 additions & 18 deletions packages/dioxus-blitz/src/documents/dioxus_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ impl DocumentLike for DioxusDocument {
// todo: we might need to walk upwards to find the first element with a data-dioxus-id attribute
for node in chain.iter() {
let Some(element) = self.inner.tree()[*node].element_data() else {
println!(
#[cfg(feature = "tracing")]
tracing::info!(
"No element data found for node {}: {:?}",
node,
self.inner.tree()[*node]
);

continue;
};

Expand Down Expand Up @@ -196,11 +198,12 @@ impl DioxusState {
pub fn create(doc: &mut Document) -> Self {
let root = doc.root_node();
let root_id = root.id;
dbg!(Self {

Self {
templates: FxHashMap::default(),
stack: vec![root_id],
node_id_mapping: vec![Some(root_id)],
})
}
}

/// Convert an ElementId to a NodeId
Expand Down Expand Up @@ -254,20 +257,28 @@ impl MutationWriter<'_> {

impl WriteMutations for MutationWriter<'_> {
fn register_template(&mut self, template: Template) {
println!("register_template name:{}", template.name);
let template_root_ids: Vec<NodeId> = template
.roots
.iter()
.map(|root| create_template_node(self.doc, root))
.collect();
dbg!(&template_root_ids);

#[cfg(feature = "tracing")]
tracing::info!(
"Registered template: {:?} with root IDs {:?}",
&template.name,
&template_root_ids
);

self.state
.templates
.insert(template.name.to_string(), template_root_ids);
}

fn append_children(&mut self, id: ElementId, m: usize) {
println!("append_children id:{} m:{}", id.0, m);
#[cfg(feature = "tracing")]
tracing::info!("append_children id:{} m:{}", id.0, m);

let children = self.state.stack.split_off(self.state.stack.len() - m);
let parent = self.state.element_to_node_id(id);
for child in children {
Expand All @@ -277,20 +288,26 @@ impl WriteMutations for MutationWriter<'_> {
}

fn assign_node_id(&mut self, path: &'static [u8], id: ElementId) {
println!("assign_node_id path:{:?} id:{}", path, id.0);
#[cfg(feature = "tracing")]
tracing::info!("assign_node_id path:{:?} id:{}", path, id.0);

let node_id = self.load_child(path);
self.set_id_mapping(node_id, id);
}

fn create_placeholder(&mut self, id: ElementId) {
println!("create_placeholder id:{}", id.0);
#[cfg(feature = "tracing")]
tracing::info!("create_placeholder id:{}", id.0);

let node_id = self.doc.create_node(NodeData::Comment);
self.set_id_mapping(node_id, id);
self.state.stack.push(node_id);
}

fn create_text_node(&mut self, value: &str, id: ElementId) {
println!("create_text_node id:{} text:{}", id.0, value);
#[cfg(feature = "tracing")]
tracing::info!("create_text_node id:{} text:{}", id.0, value);

let node_id = self.doc.create_text_node(value);
self.set_id_mapping(node_id, id);
self.state.stack.push(node_id);
Expand All @@ -302,10 +319,15 @@ impl WriteMutations for MutationWriter<'_> {
} else {
value
};
println!(

#[cfg(feature = "tracing")]
tracing::info!(
"hydrate_text_node id:{} path: {:?} text:{}",
id.0, path, value_trunc
id.0,
path,
value_trunc
);

let node_id = self.load_child(path);
self.set_id_mapping(node_id, id);
let node = self.doc.get_node_mut(node_id).unwrap();
Expand All @@ -329,31 +351,39 @@ impl WriteMutations for MutationWriter<'_> {
}

fn load_template(&mut self, name: &'static str, index: usize, id: ElementId) {
println!("load_template name:{} index: {} id:{}", name, index, id.0);
#[cfg(feature = "tracing")]
tracing::info!("load_template name:{} index: {} id:{}", name, index, id.0);

let template_node_id = self.state.templates[name][index];
let clone_id = self.doc.deep_clone_node(template_node_id);
self.set_id_mapping(clone_id, id);
self.state.stack.push(clone_id);
}

fn replace_node_with(&mut self, id: ElementId, m: usize) {
println!("replace_node_with id:{} m:{}", id.0, m);
#[cfg(feature = "tracing")]
tracing::info!("replace_node_with id:{} m:{}", id.0, m);

let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
let anchor_node_id = self.state.element_to_node_id(id);
self.doc.insert_before(anchor_node_id, &new_nodes);
self.doc.remove_node(anchor_node_id);
}

fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize) {
println!("replace_placeholder_with_nodes path:{:?} m:{}", path, m);
#[cfg(feature = "tracing")]
tracing::info!("replace_placeholder_with_nodes path:{:?} m:{}", path, m);

let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
let anchor_node_id = self.load_child(path);
self.doc.insert_before(anchor_node_id, &new_nodes);
self.doc.remove_node(anchor_node_id);
}

fn insert_nodes_after(&mut self, id: ElementId, m: usize) {
println!("insert_nodes_after id:{} m:{}", id.0, m);
#[cfg(feature = "tracing")]
tracing::info!("insert_nodes_after id:{} m:{}", id.0, m);

let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
let anchor_node_id = self.state.element_to_node_id(id);
let next_sibling_id = self
Expand All @@ -372,7 +402,9 @@ impl WriteMutations for MutationWriter<'_> {
}

fn insert_nodes_before(&mut self, id: ElementId, m: usize) {
println!("insert_nodes_before id:{} m:{}", id.0, m);
#[cfg(feature = "tracing")]
tracing::info!("insert_nodes_before id:{} m:{}", id.0, m);

let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
let anchor_node_id = self.state.element_to_node_id(id);
self.doc.insert_before(anchor_node_id, &new_nodes);
Expand Down Expand Up @@ -479,13 +511,17 @@ impl WriteMutations for MutationWriter<'_> {
}

fn remove_node(&mut self, id: ElementId) {
println!("remove_node id:{}", id.0);
#[cfg(feature = "tracing")]
tracing::info!("remove_node id:{}", id.0);

let node_id = self.state.element_to_node_id(id);
self.doc.remove_node(node_id);
}

fn push_root(&mut self, id: ElementId) {
println!("push_root id:{}", id.0,);
#[cfg(feature = "tracing")]
tracing::info!("push_root id:{}", id.0,);

let node_id = self.state.element_to_node_id(id);
self.state.stack.push(node_id);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/dioxus-blitz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
initial = false;
}

#[cfg(feature = "tracing")]
tracing::trace!("Received event: {:?}", event);

match event {
// Exit the app when close is request
// Not always necessary
Expand Down Expand Up @@ -231,9 +234,8 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
}
}
dioxus_hot_reload::HotReloadMsg::Shutdown => event_loop.exit(),
dioxus_hot_reload::HotReloadMsg::UpdateAsset(asset) => {
dioxus_hot_reload::HotReloadMsg::UpdateAsset(_asset) => {
// TODO dioxus-desktop seems to handle this by forcing a reload of all stylesheets.
dbg!("Update asset {:?}", asset);
}
},
},
Expand Down
2 changes: 0 additions & 2 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {

// todo: if there's an active text input, we want to direct input towards it and translate system emi text
WindowEvent::KeyboardInput { event, .. } => {
dbg!(&event);

match event.physical_key {
PhysicalKey::Code(key_code) => {
match key_code {
Expand Down
5 changes: 5 additions & 0 deletions packages/dom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "blitz-dom"
version = "0.0.0"
edition = "2021"

[features]
default = ["tracing"]
tracing = ["dep:tracing"]

[dependencies]
style = { workspace = true, features = ["servo"] }
selectors = { workspace = true }
Expand All @@ -11,6 +15,7 @@ style_traits = { workspace = true }
taffy = { workspace = true }
parley = { workspace = true }
peniko = { workspace = true }
tracing = { workspace = true, optional = true }
slab = "0.4.9"
app_units = "0.7.5"
euclid = { version = "0.22", features = ["serde"] }
Expand Down

0 comments on commit c73e0af

Please sign in to comment.