From 6abff2e7a7f7c4887350f6198ebb8c4d84025481 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 9 Oct 2019 15:50:48 +0200 Subject: [PATCH] async stdify Signed-off-by: Yoshua Wuyts --- Cargo.toml | 1 + examples/trace.rs | 17 ++++++++++------- src/lib.rs | 21 +++++++++++---------- src/logger.rs | 39 +++++++++++---------------------------- 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd1c32f..106d383 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ edition = "2018" log = { version = "0.4.8", features = ["std", "kv_unstable"] } backtrace = "0.3.34" async-log-attributes = { path = "async-log-attributes", version = "1.0.1" } +async-std = "0.99.9" [dev-dependencies] femme = "1.2.0" diff --git a/examples/trace.rs b/examples/trace.rs index e829ba3..05ee93f 100644 --- a/examples/trace.rs +++ b/examples/trace.rs @@ -1,22 +1,25 @@ use async_log::{instrument, span}; +use async_std::task; use log::info; fn setup_logger() { let logger = femme::pretty::Logger::new(); - async_log::Logger::wrap(logger, || /* get the task id here */ 0) + async_log::Logger::wrap(logger) .start(log::LevelFilter::Trace) .unwrap(); } fn main() { - setup_logger(); + task::block_on(async { + setup_logger(); - span!("level {}", 1, { - let x = "beep"; - info!("look at this value: {}", x); + span!("level {}", 1, { + let x = "beep"; + info!("look at this value: {}", x); - span!("level {}", 2, { - inner("boop"); + span!("level {}", 2, { + inner("boop"); + }) }) }) } diff --git a/src/lib.rs b/src/lib.rs index 4c473ba..0600a23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,26 +78,27 @@ //! ## Example //! //! ```rust -//! use async_log::span; -//! use log::info; +//! use async_std::task; //! //! fn setup_logger() { //! let logger = femme::pretty::Logger::new(); -//! async_log::Logger::wrap(logger, || 12) +//! async_log::Logger::wrap(logger) //! .start(log::LevelFilter::Trace) //! .unwrap(); //! } //! //! fn main() { -//! setup_logger(); +//! task::block_on(async { +//! setup_logger(); //! -//! span!("new level, depth={}", 1, { -//! let x = "beep"; -//! info!("look at this value, x={}", x); +//! async_log::span!("new level, depth={}", 1, { +//! let x = "beep"; +//! log::info!("look at this value, x={}", x); //! -//! span!("new level, depth={}", 2, { -//! let y = "boop"; -//! info!("another nice value, y={}", y); +//! async_log::span!("new level, depth={}", 2, { +//! let y = "boop"; +//! log::info!("another nice value, y={}", y); +//! }) //! }) //! }) //! } diff --git a/src/logger.rs b/src/logger.rs index 66962b9..4104e5f 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,33 +1,23 @@ use crate::backtrace::async_log_capture_caller; -use log::{set_boxed_logger, LevelFilter, Log, Metadata, Record}; +use log::{kv, set_boxed_logger, LevelFilter, Log, Metadata, Record}; +use async_std::task; use std::thread; /// A Logger that wraps other loggers to extend it with async functionality. #[derive(Debug)] -pub struct Logger -where - F: Fn() -> u64 + Send + Sync + 'static, -{ +pub struct Logger { backtrace: bool, logger: L, - with: F, } -impl Logger -where - F: Fn() -> u64 + Send + Sync + 'static, -{ +impl Logger { /// Wrap an existing logger, extending it with async functionality. - pub fn wrap(logger: L, with: F) -> Self { + pub fn wrap(logger: L) -> Self { let backtrace = std::env::var_os("RUST_BACKTRACE") .map(|x| &x != "0") .unwrap_or(false); - Self { - logger, - backtrace, - with, - } + Self { logger, backtrace } } /// Start logging. @@ -39,11 +29,6 @@ where res } - /// Call the `self.with` closure, and return its results. - fn with(&self) -> u64 { - (self.with)() - } - /// Compute which stack frame to log based on an offset defined inside the log message. /// This message is then stripped from the resulting record. fn compute_stack_depth(&self, _record: &Record<'_>) -> u8 { @@ -59,10 +44,7 @@ fn thread_id() -> u64 { string.parse().unwrap() } -impl log::Log for Logger -where - F: Fn() -> u64 + Send + Sync + 'static, -{ +impl log::Log for Logger { fn enabled(&self, metadata: &Metadata<'_>) -> bool { self.logger.enabled(metadata) } @@ -74,7 +56,7 @@ where let key_values = KeyValues { thread_id: thread_id(), - task_id: self.with(), + task_id: task::current().id(), kvs: record.key_values(), }; @@ -131,7 +113,7 @@ where struct KeyValues<'a> { thread_id: u64, - task_id: u64, + task_id: task::TaskId, kvs: &'a dyn log::kv::Source, } impl<'a> log::kv::Source for KeyValues<'a> { @@ -140,8 +122,9 @@ impl<'a> log::kv::Source for KeyValues<'a> { visitor: &mut dyn log::kv::Visitor<'kvs>, ) -> Result<(), log::kv::Error> { self.kvs.visit(visitor)?; + let task_id = kv::Value::from_display(&self.task_id); visitor.visit_pair("thread_id".into(), self.thread_id.into())?; - visitor.visit_pair("task_id".into(), self.task_id.into())?; + visitor.visit_pair("task_id".into(), task_id)?; Ok(()) } }