Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to tracing for logging #1436

Merged
merged 7 commits into from Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -82,7 +82,8 @@ generational-box = { path = "packages/generational-box" }
dioxus-hot-reload = { path = "packages/hot-reload", version = "0.4.0" }
dioxus-fullstack = { path = "packages/fullstack", version = "0.4.1" }
dioxus_server_macro = { path = "packages/server-macro", version = "0.4.1" }
log = "0.4.19"
tracing = "0.1.37"
tracing-futures = "0.2.5"
tokio = "1.28"
slab = "0.4.2"
futures-channel = "0.3.21"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/Cargo.toml
Expand Up @@ -28,7 +28,7 @@ slab = { workspace = true }
futures-channel = { workspace = true }

smallbox = "0.8.1"
log = { workspace = true }
tracing = { workspace = true }

# Serialize the Edits for use in Webview/Liveview instances
serde = { version = "1", features = ["derive"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/any_props.rs
Expand Up @@ -68,7 +68,7 @@ unsafe impl<'a, P> AnyProps<'a> for VProps<'a, P> {
Ok(None) => RenderReturn::default(),
Err(err) => {
let component_name = cx.name();
log::error!("Error while rendering component `{component_name}`: {err:?}");
tracing::error!("Error while rendering component `{component_name}`: {err:?}");
RenderReturn::default()
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/diff.rs
Expand Up @@ -196,14 +196,20 @@ impl<'b> VirtualDom {
right.scope.set(Some(scope_id));

// copy out the box for both
let old = self.scopes[scope_id.0].props.as_ref();
let old_scope = &self.scopes[scope_id.0];
let old = old_scope.props.as_ref();
let new: Box<dyn AnyProps> = right.props.take().unwrap();
let new: Box<dyn AnyProps> = unsafe { std::mem::transmute(new) };

// If the props are static, then we try to memoize by setting the new with the old
// The target scopestate still has the reference to the old props, so there's no need to update anything
// This also implicitly drops the new props since they're not used
if left.static_props && unsafe { old.as_ref().unwrap().memoize(new.as_ref()) } {
tracing::trace!(
"Memoized props for component {:#?} ({})",
scope_id,
old_scope.context().name
);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/events.rs
Expand Up @@ -118,7 +118,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for Event<T> {
///
/// ```rust, ignore
/// rsx!{
/// MyComponent { onclick: move |evt| log::info!("clicked") }
/// MyComponent { onclick: move |evt| tracing::debug!("clicked") }
/// }
///
/// #[derive(Props)]
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/scope_arena.rs
Expand Up @@ -64,6 +64,7 @@ impl VirtualDom {
let props: &dyn AnyProps = scope.props.as_ref().unwrap().as_ref();
let props: &dyn AnyProps = std::mem::transmute(props);

let _span = tracing::trace_span!("render", scope = %scope.context().name);
props.render(scope).extend_lifetime()
};

Expand Down
41 changes: 34 additions & 7 deletions packages/core/src/scope_context.rs
Expand Up @@ -107,27 +107,48 @@ impl ScopeContext {
///
/// Clones the state if it exists.
pub fn consume_context<T: 'static + Clone>(&self) -> Option<T> {
tracing::trace!(
"looking for context {} ({:?}) in {}",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>(),
self.name
);
if let Some(this_ctx) = self.has_context() {
return Some(this_ctx);
}

let mut search_parent = self.parent_id;
with_runtime(|runtime| {
match with_runtime(|runtime: &crate::runtime::Runtime| {
while let Some(parent_id) = search_parent {
let parent = runtime.get_context(parent_id).unwrap();
if let Some(shared) = parent
.shared_contexts
.borrow()
.iter()
.find_map(|any| any.downcast_ref::<T>())
{
tracing::trace!(
"looking for context {} ({:?}) in {}",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>(),
parent.name
);
if let Some(shared) = parent.shared_contexts.borrow().iter().find_map(|any| {
tracing::trace!("found context {:?}", any.type_id());
any.downcast_ref::<T>()
}) {
return Some(shared.clone());
}
search_parent = parent.parent_id;
}
None
})
.flatten()
{
Some(ctx) => Some(ctx),
None => {
tracing::trace!(
"context {} ({:?}) not found",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>()
);
None
}
}
}

/// Expose state to children further down the [`crate::VirtualDom`] Tree. Requires `Clone` on the context to allow getting values down the tree.
Expand All @@ -152,6 +173,12 @@ impl ScopeContext {
/// }
/// ```
pub fn provide_context<T: 'static + Clone>(&self, value: T) -> T {
tracing::trace!(
"providing context {} ({:?}) in {}",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>(),
self.name
);
let mut contexts = self.shared_contexts.borrow_mut();

// If the context exists, swap it out for the new value
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/virtual_dom.rs
Expand Up @@ -306,6 +306,7 @@ impl VirtualDom {
pub fn mark_dirty(&mut self, id: ScopeId) {
if let Some(scope) = self.get_scope(id) {
let height = scope.height();
tracing::trace!("Marking scope {:?} ({}) as dirty", id, scope.context().name);
self.dirty_scopes.insert(DirtyScope { height, id });
}
}
Expand Down Expand Up @@ -558,7 +559,7 @@ impl VirtualDom {
}
// If an error occurs, we should try to render the default error component and context where the error occured
RenderReturn::Aborted(placeholder) => {
log::info!("Ran into suspended or aborted scope during rebuild");
tracing::debug!("Ran into suspended or aborted scope during rebuild");
let id = self.next_null();
placeholder.id.set(Some(id));
self.mutations.push(Mutation::CreatePlaceholder { id });
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/Cargo.toml
Expand Up @@ -18,7 +18,7 @@ dioxus-hot-reload = { workspace = true, optional = true }
serde = "1.0.136"
serde_json = "1.0.79"
thiserror = { workspace = true }
log = { workspace = true }
tracing = { workspace = true }
wry = { version = "0.28.0" }
futures-channel = { workspace = true }
tokio = { workspace = true, features = [
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/src/desktop_context.rs
Expand Up @@ -194,7 +194,7 @@ impl DesktopService {
/// launch print modal
pub fn print(&self) {
if let Err(e) = self.webview.print() {
log::warn!("Open print modal failed: {e}");
tracing::warn!("Open print modal failed: {e}");
}
}

Expand All @@ -209,7 +209,7 @@ impl DesktopService {
self.webview.open_devtools();

#[cfg(not(debug_assertions))]
log::warn!("Devtools are disabled in release builds");
tracing::warn!("Devtools are disabled in release builds");
}

/// Create a wry event handler that listens for wry events.
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/lib.rs
Expand Up @@ -330,7 +330,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
if temp.contains_key("href") {
let open = webbrowser::open(temp["href"].as_str().unwrap());
if let Err(e) = open {
log::error!("Open Browser error: {:?}", e);
tracing::error!("Open Browser error: {:?}", e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/query.rs
Expand Up @@ -123,7 +123,7 @@ impl QueryEngine {
}})
}})();"#
)) {
log::warn!("Query error: {err}");
tracing::warn!("Query error: {err}");
}

Query {
Expand Down
2 changes: 1 addition & 1 deletion packages/dioxus/Cargo.toml
Expand Up @@ -30,7 +30,7 @@ hot-reload = ["dioxus-hot-reload"]

[dev-dependencies]
futures-util = { workspace = true }
log = { workspace = true }
tracing = { workspace = true }
rand = { version = "0.8.4", features = ["small_rng"] }
criterion = "0.3.5"
thiserror = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/fermi/Cargo.toml
Expand Up @@ -14,7 +14,7 @@ keywords = ["dom", "ui", "gui", "react", "state-management"]
[dependencies]
dioxus-core = { workspace = true }
im-rc = { version = "15.0.0", features = ["serde"] }
log = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
closure = "0.3.0"
8 changes: 4 additions & 4 deletions packages/fermi/src/root.rs
Expand Up @@ -66,14 +66,14 @@ impl AtomRoot {

if let Some(slot) = atoms.get_mut(&ptr) {
slot.value = Rc::new(value);
log::trace!("found item with subscribers {:?}", slot.subscribers);
tracing::trace!("found item with subscribers {:?}", slot.subscribers);

for scope in &slot.subscribers {
log::trace!("updating subcsriber");
tracing::trace!("updating subcsriber");
(self.update_any)(*scope);
}
} else {
log::trace!("no atoms found for {:?}", ptr);
tracing::trace!("no atoms found for {:?}", ptr);
atoms.insert(
ptr,
Slot {
Expand All @@ -96,7 +96,7 @@ impl AtomRoot {
pub fn force_update(&self, ptr: AtomId) {
if let Some(slot) = self.atoms.borrow_mut().get(&ptr) {
for scope in slot.subscribers.iter() {
log::trace!("updating subcsriber");
tracing::trace!("updating subcsriber");
(self.update_any)(*scope);
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/fullstack/Cargo.toml
Expand Up @@ -44,7 +44,8 @@ dioxus-desktop = { workspace = true, optional = true }
# Router Integration
dioxus-router = { workspace = true, optional = true }

log = { workspace = true }
tracing = { workspace = true }
tracing-futures = { workspace = true }
once_cell = "1.17.1"
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"], optional = true }
Expand Down
5 changes: 3 additions & 2 deletions packages/fullstack/examples/axum-hello-world/Cargo.toml
Expand Up @@ -12,8 +12,9 @@ dioxus-fullstack = { workspace = true }
axum = { version = "0.6.12", optional = true }
serde = "1.0.159"
simple_logger = "4.2.0"
wasm-logger = "0.2.0"
log.workspace = true
tracing-wasm = "0.2.1"
tracing.workspace = true
tracing-subscriber = "0.3.17"
reqwest = "0.11.18"

[features]
Expand Down
5 changes: 2 additions & 3 deletions packages/fullstack/examples/axum-hello-world/src/main.rs
Expand Up @@ -12,7 +12,6 @@ use dioxus_fullstack::{
prelude::*,
};
use serde::{Deserialize, Serialize};
use wasm_logger::Config;

#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
struct AppProps {
Expand Down Expand Up @@ -66,9 +65,9 @@ async fn get_server_data() -> Result<String, ServerFnError> {

fn main() {
#[cfg(feature = "web")]
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
tracing_wasm::set_as_global_default();
#[cfg(feature = "ssr")]
simple_logger::SimpleLogger::new().init().unwrap();
tracing_subscriber::fmt::init();

LaunchBuilder::new_with_props(app, AppProps { count: 0 }).launch()
}
5 changes: 3 additions & 2 deletions packages/fullstack/examples/salvo-hello-world/Cargo.toml
Expand Up @@ -16,8 +16,9 @@ salvo = { version = "0.37.9", optional = true }
execute = "0.2.12"
reqwest = "0.11.18"
simple_logger = "4.2.0"
log.workspace = true
wasm-logger = "0.2.0"
tracing-wasm = "0.2.1"
tracing.workspace = true
tracing-subscriber = "0.3.17"

[features]
default = []
Expand Down
4 changes: 2 additions & 2 deletions packages/fullstack/examples/salvo-hello-world/src/main.rs
Expand Up @@ -60,9 +60,9 @@ async fn get_server_data() -> Result<String, ServerFnError> {

fn main() {
#[cfg(feature = "web")]
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
tracing_wasm::set_as_global_default();
#[cfg(feature = "ssr")]
simple_logger::SimpleLogger::new().init().unwrap();
tracing_subscriber::fmt::init();

LaunchBuilder::new_with_props(app, AppProps { count: 0 }).launch()
}
5 changes: 3 additions & 2 deletions packages/fullstack/examples/warp-hello-world/Cargo.toml
Expand Up @@ -16,8 +16,9 @@ warp = { version = "0.3.3", optional = true }
execute = "0.2.12"
reqwest = "0.11.18"
simple_logger = "4.2.0"
log.workspace = true
wasm-logger = "0.2.0"
tracing-wasm = "0.2.1"
tracing.workspace = true
tracing-subscriber = "0.3.17"

[features]
default = []
Expand Down
4 changes: 2 additions & 2 deletions packages/fullstack/examples/warp-hello-world/src/main.rs
Expand Up @@ -60,9 +60,9 @@ async fn get_server_data() -> Result<String, ServerFnError> {

fn main() {
#[cfg(feature = "web")]
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
tracing_wasm::set_as_global_default();
#[cfg(feature = "ssr")]
simple_logger::SimpleLogger::new().init().unwrap();
tracing_subscriber::fmt::init();

LaunchBuilder::new_with_props(app, AppProps { count: 0 }).launch()
}
2 changes: 1 addition & 1 deletion packages/fullstack/src/adapters/axum_adapter.rs
Expand Up @@ -389,7 +389,7 @@ pub async fn render_handler<P: Clone + serde::Serialize + Send + Sync + 'static>
response
}
Err(e) => {
log::error!("Failed to render page: {}", e);
tracing::error!("Failed to render page: {}", e);
report_err(e).into_response()
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fullstack/src/adapters/salvo_adapter.rs
Expand Up @@ -423,7 +423,7 @@ impl<P: Clone + serde::Serialize + Send + Sync + 'static> Handler for SSRHandler
freshness.write(res.headers_mut());
}
Err(err) => {
log::error!("Error rendering SSR: {}", err);
tracing::error!("Error rendering SSR: {}", err);
res.write_body("Error rendering SSR").unwrap();
}
};
Expand Down
6 changes: 3 additions & 3 deletions packages/fullstack/src/adapters/warp_adapter.rs
Expand Up @@ -84,7 +84,7 @@ use warp::{
/// async move {
/// let req = warp::hyper::Request::from_parts(parts, bytes.into());
/// service.run(req).await.map_err(|err| {
/// log::error!("Server function error: {}", err);
/// tracing::error!("Server function error: {}", err);
/// warp::reject::reject()
/// })
/// }
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl R
async move {
let req = warp::hyper::Request::from_parts(parts, bytes.into());
service.run(req).await.map_err(|err| {
log::error!("Server function error: {}", err);
tracing::error!("Server function error: {}", err);
warp::reject::reject()
})
}
Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn render_ssr<P: Clone + serde::Serialize + Send + Sync + 'static>(
res
}
Err(err) => {
log::error!("Failed to render ssr: {}", err);
tracing::error!("Failed to render ssr: {}", err);
Response::builder()
.status(500)
.body("Failed to render ssr".into())
Expand Down
2 changes: 1 addition & 1 deletion packages/fullstack/src/hooks/server_cached.rs
Expand Up @@ -23,7 +23,7 @@ pub fn server_cached<O: 'static + Serialize + DeserializeOwned>(server_fn: impl
let data = server_fn();
let sc = crate::prelude::server_context();
if let Err(err) = sc.push_html_data(&data) {
log::error!("Failed to push HTML data: {}", err);
tracing::error!("Failed to push HTML data: {}", err);
}
data
}
Expand Down