From c67d7622fc9666c5484e8057fe3ee875b046d23a Mon Sep 17 00:00:00 2001 From: KodrAus Date: Thu, 27 Jul 2023 21:00:40 +1000 Subject: [PATCH] work on span machinery --- macros/src/emit.rs | 6 ++---- src/lib.rs | 2 -- src/macro_hooks.rs | 46 ++++++++++++++++++++++++++++++++++++---- tests/smoke-test/main.rs | 8 ++++++- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/macros/src/emit.rs b/macros/src/emit.rs index b76abdd..d72e5a1 100644 --- a/macros/src/emit.rs +++ b/macros/src/emit.rs @@ -20,9 +20,7 @@ struct Args { impl Parse for Args { fn parse(input: syn::parse::ParseStream) -> syn::Result { - let mut ts = Arg::token_stream("ts", |expr| { - Ok(quote!(Some(emit::time::Extent::from(#expr)))) - }); + let mut ts = Arg::token_stream("ts", |expr| Ok(quote!(#expr))); let mut to = Arg::token_stream("to", |expr| Ok(quote!(#expr))); let mut when = Arg::token_stream("when", |expr| Ok(quote!(#expr))); @@ -32,7 +30,7 @@ impl Parse for Args { )?; Ok(Args { - ts: ts.take().unwrap_or_else(|| quote!(None)), + ts: ts.take().unwrap_or_else(|| quote!(emit::empty::Empty)), to: to.take().unwrap_or_else(|| quote!(emit::empty::Empty)), when: when.take().unwrap_or_else(|| quote!(emit::empty::Empty)), }) diff --git a/src/lib.rs b/src/lib.rs index 6bd597a..2b0f451 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ extern crate alloc; use core::future::Future; -use std::ops::RangeInclusive; use emit_core::{ctxt::Ctxt, filter::Filter, target::Target}; @@ -24,7 +23,6 @@ pub mod ctxt { use emit_core::{ time::{Clock, Extent}, - value::ToValue, well_known::WellKnown, }; diff --git a/src/macro_hooks.rs b/src/macro_hooks.rs index fba726a..ed46f59 100644 --- a/src/macro_hooks.rs +++ b/src/macro_hooks.rs @@ -1,4 +1,4 @@ -use core::{any::Any, fmt, future::Future}; +use core::{any::Any, fmt, future::Future, ops::RangeInclusive}; use emit_core::{ ambient, @@ -8,7 +8,7 @@ use emit_core::{ props::Props, target::Target, template::Template, - time::{Clock, Extent}, + time::{Clock, Extent, Timestamp}, value::Value, well_known::LEVEL_KEY, }; @@ -303,7 +303,7 @@ impl<'a> __PrivateKeyHook for Key<'a> { pub fn __emit( to: impl Target, when: impl Filter, - ts: Option, + ts: impl IntoExtent, lvl: Level, tpl: Template, props: impl Props, @@ -314,12 +314,40 @@ pub fn __emit( to.and(ambient), when.and(ambient), ambient, - ts.or_else(|| ambient.now().map(Extent::point)), + ts.into_extent().or_else(|| ambient.now().map(Into::into)), tpl, (LEVEL_KEY, lvl).chain(props), ); } +pub trait IntoExtent { + fn into_extent(self) -> Option; +} + +impl IntoExtent for Timestamp { + fn into_extent(self) -> Option { + Some(Extent::from(self)) + } +} + +impl IntoExtent for RangeInclusive { + fn into_extent(self) -> Option { + Some(Extent::from(self)) + } +} + +impl IntoExtent for Option { + fn into_extent(self) -> Option { + self.and_then(IntoExtent::into_extent) + } +} + +impl IntoExtent for emit_core::empty::Empty { + fn into_extent(self) -> Option { + None + } +} + #[track_caller] pub fn __with(props: impl Props) -> LocalFrame { let ambient = ambient::get(); @@ -337,6 +365,16 @@ pub fn __with_future( base_with_future(ambient, props, future) } +pub fn __span_start() -> Option { + ambient::get().now() +} + +pub fn __span_end(start: Option) -> impl IntoExtent { + let end = ambient::get().now(); + + start.and_then(|start| end.map(|end| start..=end)) +} + #[cfg(test)] mod tests { use super::*; diff --git a/tests/smoke-test/main.rs b/tests/smoke-test/main.rs index 2eeb56f..7a14d77 100644 --- a/tests/smoke-test/main.rs +++ b/tests/smoke-test/main.rs @@ -32,6 +32,8 @@ async fn main() { #[emit::with(a, ax: 13)] async fn in_ctxt(a: i32) { + let ts = emit::__private::__span_start(); + in_ctxt2(5).await; let work = Work { @@ -39,7 +41,11 @@ async fn in_ctxt(a: i32) { description: "Some very important business".to_owned(), }; - emit::info!("working on {#[emit::as_serde] work}"); + tokio::time::sleep(Duration::from_secs(1)).await; + + let ts = emit::__private::__span_end(ts); + + emit::info!(ts, "working on {#[emit::as_serde] work}"); } #[emit::with(b, bx: 90)]