Skip to content

Commit

Permalink
Replace custom logging by tracing (#2814)
Browse files Browse the repository at this point in the history
* use tracing for logging

* embed spans in the scheduler for tracing

* fix feature soundness

* remove spans from scheduler for now

* feature soundness take 2

* use tracing::* throughout lib code

not yet in testing, and for some errors!
  • Loading branch information
WorldSEnder committed Aug 7, 2022
1 parent f0b0df3 commit 4206da7
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 333 deletions.
1 change: 1 addition & 0 deletions packages/yew-router/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ gloo = { version = "0.8", features = ["futures"] }
route-recognizer = "0.3"
serde = "1"
serde_urlencoded = "0.7.1"
tracing = "0.1.36"

[dependencies.web-sys]
version = "0.3"
Expand Down
3 changes: 1 addition & 2 deletions packages/yew-router/src/switch.rs
@@ -1,6 +1,5 @@
//! The [`Switch`] Component.

use gloo::console;
use yew::prelude::*;

use crate::prelude::*;
Expand Down Expand Up @@ -41,7 +40,7 @@ where
match route {
Some(route) => props.render.emit(route),
None => {
console::warn!("no route matched");
tracing::warn!("no route matched");
Html::default()
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/yew/Cargo.toml
Expand Up @@ -33,6 +33,7 @@ bincode = { version = "1.3.3", optional = true }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1.19", features = ["sync"] }
tokio-stream = { version = "0.1.9", features = ["sync"] }
tracing = "0.1.36"

[dependencies.web-sys]
version = "0.3"
Expand Down
14 changes: 14 additions & 0 deletions packages/yew/src/app_handle.rs
Expand Up @@ -24,6 +24,11 @@ where
/// similarly to the `program` function in Elm. You should provide an initial model, `update`
/// function which will update the state of the model and a `view` function which
/// will render the model to a virtual DOM tree.
#[tracing::instrument(
level = tracing::Level::DEBUG,
name = "mount",
skip(props),
)]
pub(crate) fn mount_with_props(host: Element, props: Rc<COMP::Properties>) -> Self {
clear_element(&host);
let app = Self {
Expand All @@ -42,6 +47,10 @@ where
}

/// Schedule the app for destruction
#[tracing::instrument(
level = tracing::Level::DEBUG,
skip_all,
)]
pub fn destroy(self) {
self.scope.destroy(false)
}
Expand Down Expand Up @@ -74,6 +83,11 @@ mod feat_hydration {
where
COMP: BaseComponent,
{
#[tracing::instrument(
level = tracing::Level::DEBUG,
name = "hydrate",
skip(props),
)]
pub(crate) fn hydrate_with_props(host: Element, props: Rc<COMP::Properties>) -> Self {
let app = Self {
scope: Scope::new(None),
Expand Down
3 changes: 1 addition & 2 deletions packages/yew/src/dom_bundle/bnode.rs
Expand Up @@ -2,7 +2,6 @@

use std::fmt;

use gloo::console;
use web_sys::{Element, Node};

use super::{BComp, BList, BPortal, BSubtree, BSuspense, BTag, BText};
Expand Down Expand Up @@ -54,7 +53,7 @@ impl ReconcileTarget for BNode {
Self::Ref(ref node) => {
// Always remove user-defined nodes to clear possible parent references of them
if parent.remove_child(node).is_err() {
console::warn!("Node not found to remove VRef");
tracing::warn!("Node not found to remove VRef");
}
}
Self::Portal(bportal) => bportal.detach(root, parent, parent_to_detach),
Expand Down
3 changes: 1 addition & 2 deletions packages/yew/src/dom_bundle/btag/mod.rs
Expand Up @@ -7,7 +7,6 @@ use std::borrow::Cow;
use std::hint::unreachable_unchecked;
use std::ops::DerefMut;

use gloo::console;
use gloo::utils::document;
use listeners::ListenerRegistration;
pub use listeners::Registry;
Expand Down Expand Up @@ -84,7 +83,7 @@ impl ReconcileTarget for BTag {
let result = parent.remove_child(&node);

if result.is_err() {
console::warn!("Node not found to remove VTag");
tracing::warn!("Node not found to remove VTag");
}
}
// It could be that the ref was already reused when rendering another element.
Expand Down
3 changes: 1 addition & 2 deletions packages/yew/src/dom_bundle/btext.rs
@@ -1,6 +1,5 @@
//! This module contains the bundle implementation of text [BText].

use gloo::console;
use gloo::utils::document;
use web_sys::{Element, Text as TextNode};

Expand All @@ -21,7 +20,7 @@ impl ReconcileTarget for BText {
let result = parent.remove_child(&self.text_node);

if result.is_err() {
console::warn!("Node not found to remove VText");
tracing::warn!("Node not found to remove VText");
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions packages/yew/src/dom_bundle/utils.rs
Expand Up @@ -6,8 +6,18 @@ pub(super) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&N
Some(next_sibling) => parent
.insert_before(node, Some(next_sibling))
.unwrap_or_else(|err| {
gloo::console::error!("failed to insert node", err, parent, next_sibling, node);
panic!("failed to insert tag before next sibling")
// Log normally, so we can inspect the nodes in console
gloo::console::error!(
"failed to insert node before next sibling",
err,
parent,
next_sibling,
node
);
// Log via tracing for consistency
tracing::error!("failed to insert node before next sibling");
// Panic to short-curcuit and fail
panic!("failed to insert node before next sibling")
}),
None => parent.append_child(node).expect("failed to append child"),
};
Expand Down

0 comments on commit 4206da7

Please sign in to comment.