From 9c82d5e3f31da095f0bf5690a55ebf5e14a4fdb3 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 16 Sep 2019 14:07:06 +0200 Subject: [PATCH 1/2] remove custom log tools in favor of macro crate Signed-off-by: Yoshua Wuyts --- Cargo.toml | 1 + src/task/block_on.rs | 27 ++++++++++++--------------- src/task/log_utils.rs | 32 -------------------------------- src/task/mod.rs | 1 - src/task/pool.rs | 26 +++++++++++--------------- 5 files changed, 24 insertions(+), 63 deletions(-) delete mode 100644 src/task/log_utils.rs diff --git a/Cargo.toml b/Cargo.toml index 2d026fa49..939a98574 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ mio-uds = "0.6.7" num_cpus = "1.10.1" pin-utils = "0.1.0-alpha.4" slab = "0.4.2" +kv-log-macro = "1.0.4" [dev-dependencies] femme = "1.2.0" diff --git a/src/task/block_on.rs b/src/task/block_on.rs index ae0a0120a..0e9a5f485 100644 --- a/src/task/block_on.rs +++ b/src/task/block_on.rs @@ -6,13 +6,14 @@ use std::sync::Arc; use std::task::{RawWaker, RawWakerVTable}; use std::thread::{self, Thread}; -use super::log_utils; use super::pool; use super::task; use crate::future::Future; use crate::task::{Context, Poll, Waker}; use crate::utils::abort_on_panic; +use kv_log_macro::trace; + /// Spawns a task and blocks the current thread on its result. /// /// Calling this function is similar to [spawning] a thread and immediately [joining] it, except an @@ -58,13 +59,11 @@ where // Log this `block_on` operation. let child_id = tag.task_id().as_u64(); let parent_id = pool::get_task(|t| t.id().as_u64()).unwrap_or(0); - log_utils::print( - format_args!("block_on"), - log_utils::LogData { - parent_id, - child_id, - }, - ); + + trace!("block_on", { + parent_id: parent_id, + child_id: child_id, + }); // Wrap the future into one that drops task-local variables on exit. let future = async move { @@ -73,13 +72,11 @@ where // Abort on panic because thread-local variables behave the same way. abort_on_panic(|| pool::get_task(|task| task.metadata().local_map.clear())); - log_utils::print( - format_args!("block_on completed"), - log_utils::LogData { - parent_id, - child_id, - }, - ); + trace!("block_on completed", { + parent_id: parent_id, + child_id: child_id, + }); + res }; diff --git a/src/task/log_utils.rs b/src/task/log_utils.rs deleted file mode 100644 index ad0fe8cd4..000000000 --- a/src/task/log_utils.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::fmt::Arguments; - -/// This struct only exists because kv logging isn't supported from the macros right now. -pub(crate) struct LogData { - pub parent_id: u64, - pub child_id: u64, -} - -impl<'a> log::kv::Source for LogData { - fn visit<'kvs>( - &'kvs self, - visitor: &mut dyn log::kv::Visitor<'kvs>, - ) -> Result<(), log::kv::Error> { - visitor.visit_pair("parent_id".into(), self.parent_id.into())?; - visitor.visit_pair("child_id".into(), self.child_id.into())?; - Ok(()) - } -} - -pub fn print(msg: Arguments<'_>, key_values: impl log::kv::Source) { - log::logger().log( - &log::Record::builder() - .args(msg) - .key_values(&key_values) - .level(log::Level::Trace) - .target(module_path!()) - .module_path(Some(module_path!())) - .file(Some(file!())) - .line(Some(line!())) - .build(), - ); -} diff --git a/src/task/mod.rs b/src/task/mod.rs index 21b053361..eef72846a 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -32,7 +32,6 @@ pub use task::{JoinHandle, Task, TaskId}; mod block_on; mod local; -mod log_utils; mod pool; mod sleep; mod task; diff --git a/src/task/pool.rs b/src/task/pool.rs index c9968489c..90502534b 100644 --- a/src/task/pool.rs +++ b/src/task/pool.rs @@ -4,8 +4,8 @@ use std::thread; use crossbeam_channel::{unbounded, Sender}; use lazy_static::lazy_static; +use kv_log_macro::trace; -use super::log_utils; use super::task; use super::{JoinHandle, Task}; use crate::future::Future; @@ -130,13 +130,11 @@ where // Log this `spawn` operation. let child_id = tag.task_id().as_u64(); let parent_id = get_task(|t| t.id().as_u64()).unwrap_or(0); - log_utils::print( - format_args!("spawn"), - log_utils::LogData { - parent_id, - child_id, - }, - ); + + trace!("spawn", { + parent_id: parent_id, + child_id: child_id, + }); // Wrap the future into one that drops task-local variables on exit. let future = async move { @@ -145,13 +143,11 @@ where // Abort on panic because thread-local variables behave the same way. abort_on_panic(|| get_task(|task| task.metadata().local_map.clear())); - log_utils::print( - format_args!("spawn completed"), - log_utils::LogData { - parent_id, - child_id, - }, - ); + trace!("spawn completed", { + parent_id: parent_id, + child_id: child_id, + }); + res }; From 1341fa7addc59f4ad8ba73bf65f2a8dae2c3caa5 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 16 Sep 2019 15:34:06 +0200 Subject: [PATCH 2/2] cargo fmt Signed-off-by: Yoshua Wuyts --- src/task/pool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task/pool.rs b/src/task/pool.rs index 90502534b..210b862c2 100644 --- a/src/task/pool.rs +++ b/src/task/pool.rs @@ -3,8 +3,8 @@ use std::ptr; use std::thread; use crossbeam_channel::{unbounded, Sender}; -use lazy_static::lazy_static; use kv_log_macro::trace; +use lazy_static::lazy_static; use super::task; use super::{JoinHandle, Task};