Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
work on span machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jul 27, 2023
1 parent d217e5b commit c67d762
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
6 changes: 2 additions & 4 deletions macros/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ struct Args {

impl Parse for Args {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
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)));

Expand All @@ -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)),
})
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
extern crate alloc;

use core::future::Future;
use std::ops::RangeInclusive;

use emit_core::{ctxt::Ctxt, filter::Filter, target::Target};

Expand All @@ -24,7 +23,6 @@ pub mod ctxt {

use emit_core::{
time::{Clock, Extent},
value::ToValue,
well_known::WellKnown,
};

Expand Down
46 changes: 42 additions & 4 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{any::Any, fmt, future::Future};
use core::{any::Any, fmt, future::Future, ops::RangeInclusive};

use emit_core::{
ambient,
Expand All @@ -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,
};
Expand Down Expand Up @@ -303,7 +303,7 @@ impl<'a> __PrivateKeyHook for Key<'a> {
pub fn __emit(
to: impl Target,
when: impl Filter,
ts: Option<Extent>,
ts: impl IntoExtent,
lvl: Level,
tpl: Template,
props: impl Props,
Expand All @@ -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<Extent>;
}

impl IntoExtent for Timestamp {
fn into_extent(self) -> Option<Extent> {
Some(Extent::from(self))
}
}

impl IntoExtent for RangeInclusive<Timestamp> {
fn into_extent(self) -> Option<Extent> {
Some(Extent::from(self))
}
}

impl<T: IntoExtent> IntoExtent for Option<T> {
fn into_extent(self) -> Option<Extent> {
self.and_then(IntoExtent::into_extent)
}
}

impl IntoExtent for emit_core::empty::Empty {
fn into_extent(self) -> Option<Extent> {
None
}
}

#[track_caller]
pub fn __with(props: impl Props) -> LocalFrame<impl Ctxt> {
let ambient = ambient::get();
Expand All @@ -337,6 +365,16 @@ pub fn __with_future<F: Future>(
base_with_future(ambient, props, future)
}

pub fn __span_start() -> Option<Timestamp> {
ambient::get().now()
}

pub fn __span_end(start: Option<Timestamp>) -> impl IntoExtent {
let end = ambient::get().now();

start.and_then(|start| end.map(|end| start..=end))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 7 additions & 1 deletion tests/smoke-test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ 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 {
id: 42,
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)]
Expand Down

0 comments on commit c67d762

Please sign in to comment.