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

Commit

Permalink
make module path first class
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 13, 2024
1 parent 309c814 commit f888c07
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 26 deletions.
26 changes: 23 additions & 3 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@ use core::{fmt, ops::ControlFlow};

use crate::{
extent::{Extent, ToExtent},
path::Path,
props::{ByRef, ErasedProps, Props},
template::{Render, Template},
};

#[derive(Clone)]
pub struct Event<'a, P> {
// "where"
source: Path<'a>,
// "when"
extent: Option<Extent>,
// "what"
tpl: Template<'a>,
// "why"
props: P,
// "how" is your problem
}

impl<'a, P> Event<'a, P> {
pub fn new(extent: impl ToExtent, tpl: Template<'a>, props: P) -> Self {
pub fn new(
source: impl Into<Path<'a>>,
extent: impl ToExtent,
tpl: Template<'a>,
props: P,
) -> Self {
Event {
source: source.into(),
extent: extent.to_extent(),
tpl,
props,
}
}

pub fn source(&self) -> &Path<'a> {
&self.source
}

pub fn extent(&self) -> Option<&Extent> {
self.extent.as_ref()
}

pub fn tpl(&self) -> Template {
self.tpl.by_ref()
pub fn tpl(&self) -> &Template<'a> {
&self.tpl
}

pub fn props(&self) -> &P {
Expand All @@ -42,6 +59,7 @@ impl<'a, P: Props> Event<'a, P> {

pub fn by_ref<'b>(&'b self) -> Event<'b, ByRef<'b, P>> {
Event {
source: self.source.by_ref(),
extent: self.extent.clone(),
tpl: self.tpl.by_ref(),
props: self.props.by_ref(),
Expand All @@ -50,6 +68,7 @@ impl<'a, P: Props> Event<'a, P> {

pub fn erase<'b>(&'b self) -> Event<'b, &'b dyn ErasedProps> {
Event {
source: self.source.by_ref(),
extent: self.extent.clone(),
tpl: self.tpl.by_ref(),
props: &self.props,
Expand Down Expand Up @@ -77,6 +96,7 @@ impl<'a, P: Props> fmt::Debug for Event<'a, P> {

let mut f = f.debug_struct("Event");

f.field("source", &self.source);
f.field("extent", &self.extent);
f.field("msg", &self.msg());
f.field("tpl", &self.tpl);
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod empty;
pub mod event;
pub mod extent;
pub mod filter;
pub mod path;
pub mod props;
pub mod rng;
pub mod runtime;
Expand Down
98 changes: 98 additions & 0 deletions core/src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use core::fmt;

use crate::{
str::Str,
value::{FromValue, ToValue, Value},
};

#[derive(Clone)]
pub struct Path<'a>(Str<'a>);

impl<'a> From<&'a str> for Path<'a> {
fn from(value: &'a str) -> Self {
Path(Str::from(value))
}
}

impl<'a> From<Str<'a>> for Path<'a> {
fn from(value: Str<'a>) -> Self {
Path(value)
}
}

impl<'a> ToValue for Path<'a> {
fn to_value(&self) -> Value {
self.0.to_value()
}
}

impl<'a> FromValue<'a> for Path<'a> {
fn from_value(value: Value<'a>) -> Option<Self> {
Some(value.cast()?)
}
}

impl Path<'static> {
pub const fn new(source: &'static str) -> Self {
Path(Str::new(source))
}
}

impl<'a> Path<'a> {
pub const fn new_ref(source: &'a str) -> Self {
Path(Str::new_ref(source))
}

pub fn by_ref<'b>(&'b self) -> Path<'b> {
Path(self.0.by_ref())
}

pub fn segments(&self) -> impl Iterator<Item = &str> {
self.0.as_str().split("::")
}

pub fn is_child_of<'b>(&self, other: &Path<'b>) -> bool {
let child = self.0.as_str();
let parent = other.0.as_str();

if child.len() >= parent.len() && child.is_char_boundary(parent.len()) {
let (child_prefix, child_suffix) = child.split_at(parent.len());

child_prefix == parent && (child_suffix.is_empty() || child_suffix.starts_with("::"))
} else {
false
}
}
}

impl<'a> fmt::Debug for Path<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

impl<'a> fmt::Display for Path<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn is_child_of() {
let a = Path::new("a");
let aa = Path::new("aa");
let b = Path::new("b");
let a_b = Path::new("a::b");

assert!(!aa.is_child_of(&a));
assert!(!b.is_child_of(&a));
assert!(!a.is_child_of(&a_b));

assert!(a.is_child_of(&a));
assert!(a_b.is_child_of(&a));
}
}
3 changes: 2 additions & 1 deletion core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ impl<TEmitter: Emitter, TFilter: Filter, TCtxt: Ctxt, TClock: Clock, TRng: Rng>
pub fn emit<P: Props>(&self, evt: &Event<P>) {
self.ctxt.with_current(|ctxt| {
let evt = Event::new(
evt.source().by_ref(),
evt.extent()
.cloned()
.or_else(|| self.clock.now().to_extent()),
evt.tpl(),
evt.tpl().by_ref(),
ctxt.chain(evt.props()),
);

Expand Down
3 changes: 3 additions & 0 deletions macros/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::{parse::Parse, FieldValue};
use crate::{
args::{self, Arg},
event::push_event_props,
source::source_tokens,
template,
};

Expand Down Expand Up @@ -69,6 +70,7 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
let extent_tokens = args.extent;
let rt_tokens = args.rt;
let when_tokens = args.when;
let source_tokens = source_tokens();

let template_tokens = template.template_tokens();

Expand All @@ -77,6 +79,7 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
(#(#props_match_binding_tokens),*) => {
emit::__private::__private_emit(
#rt_tokens,
#source_tokens,
#when_tokens,
#extent_tokens,
#template_tokens,
Expand Down
12 changes: 3 additions & 9 deletions macros/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::{parse::Parse, FieldValue, Ident};
use crate::{
args::{self, Arg},
props::Props,
source::source_tokens,
template,
};

Expand Down Expand Up @@ -52,9 +53,10 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
let base_props_tokens = args.props;
let template_tokens = template.template_tokens();
let props_tokens = props.props_tokens();
let source_tokens = source_tokens();

Ok(
quote!(emit::Event::new(#extent_tokens, #template_tokens, emit::Props::chain(&#base_props_tokens, #props_tokens))),
quote!(emit::Event::new(#source_tokens, #extent_tokens, #template_tokens, emit::Props::chain(&#base_props_tokens, #props_tokens))),
)
}

Expand All @@ -70,13 +72,5 @@ pub fn push_event_props(props: &mut Props, level: Option<TokenStream>) -> Result
)?;
}

// Add the location as a property
let loc_ident = Ident::new(emit_core::well_known::MODULE_KEY, Span::call_site());
props.push(
&syn::parse2::<FieldValue>(quote!(#loc_ident: emit::__private::__private_module!()))?,
false,
true,
)?;

Ok(())
}
1 change: 1 addition & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod hook;
mod key;
mod optional;
mod props;
mod source;
mod span;
mod template;
mod util;
Expand Down
5 changes: 5 additions & 0 deletions macros/src/source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use proc_macro2::TokenStream;

pub(crate) fn source_tokens() -> TokenStream {
quote!(emit::__private::__private_module!())
}
9 changes: 7 additions & 2 deletions macros/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
args::{self, Arg},
event::push_event_props,
props::Props,
source::source_tokens,
template::{self, Template},
};

Expand Down Expand Up @@ -140,14 +141,16 @@ fn inject_sync(
let ctxt_props_tokens = ctxt_props.props_tokens();
let evt_props_tokens = evt_props.props_tokens();
let template_tokens = template.template_tokens();
let source_tokens = source_tokens();

quote!({
let (mut __ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let (mut __ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #source_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let __ctxt_guard = __ctxt.enter();

let #span_arg = emit::__private::__private_begin_span(__timer, |extent| {
emit::__private::__private_emit(
#rt_tokens,
#source_tokens,
Some(emit::always()),
extent,
#template_tokens,
Expand All @@ -171,14 +174,16 @@ fn inject_async(
let ctxt_props_tokens = ctxt_props.props_tokens();
let evt_props_tokens = evt_props.props_tokens();
let template_tokens = template.template_tokens();
let source_tokens = source_tokens();

quote!({
let (__ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let (__ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #source_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);

__ctxt.with_future(async {
let #span_arg = emit::__private::__private_begin_span(__timer, |extent| {
emit::__private::__private_emit(
#rt_tokens,
#source_tokens,
Some(emit::always()),
extent,
#template_tokens,
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(feature = "alloc")]
extern crate alloc;

use emit_core::extent::ToExtent;
use emit_core::{extent::ToExtent, path::Path};

#[doc(inline)]
pub use emit_macros::*;
Expand Down Expand Up @@ -50,14 +50,15 @@ pub use setup::*;
#[track_caller]
fn base_emit(
to: impl Emitter,
source: Path,
when: impl Filter,
ctxt: impl Ctxt,
ts: impl ToExtent,
tpl: Template,
props: impl Props,
) {
ctxt.with_current(|ctxt| {
let evt = Event::new(ts, tpl, props.chain(ctxt));
let evt = Event::new(source, ts, tpl, props.chain(ctxt));

if when.matches(&evt) {
to.emit(&evt);
Expand Down
11 changes: 8 additions & 3 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use emit_core::{
emitter::Emitter,
extent::{Extent, ToExtent},
filter::Filter,
path::Path,
props::Props,
rng::Rng,
runtime::Runtime,
Expand Down Expand Up @@ -502,13 +503,15 @@ impl<A: Filter, B: Filter> Filter for FirstDefined<A, B> {
#[track_caller]
pub fn __private_emit<'a, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(
rt: &'a Runtime<E, F, C, T, R>,
source: impl Into<Path<'a>>,
when: Option<impl Filter>,
extent: impl ToExtent,
tpl: Template,
props: impl Props,
) {
base_emit(
rt.emitter(),
source.into(),
FirstDefined(when, rt.filter()),
rt.ctxt(),
extent.to_extent().or_else(|| rt.now().to_extent()),
Expand All @@ -519,10 +522,11 @@ pub fn __private_emit<'a, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(

#[track_caller]
#[cfg(feature = "alloc")]
pub fn __private_push_span_ctxt<'a, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(
pub fn __private_push_span_ctxt<'a, 'b, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(
rt: &'a Runtime<E, F, C, T, R>,
source: impl Into<Path<'b>>,
when: Option<impl Filter>,
tpl: Template,
tpl: Template<'b>,
ctxt_props: impl Props,
evt_props: impl Props,
) -> (Frame<Option<&'a C>>, Option<Timer<&'a T>>) {
Expand Down Expand Up @@ -573,8 +577,9 @@ pub fn __private_push_span_ctxt<'a, E: Emitter, F: Filter, C: Ctxt, T: Clock, R:
let timer = Timer::start(rt.clock());

if FirstDefined(when, rt.filter()).matches(&Event::new(
source,
timer.extent().map(|extent| *extent.as_point()),
tpl.by_ref(),
tpl,
ctxt_props.by_ref().chain(&trace_ctxt).chain(&evt_props),
)) {
(
Expand Down
Loading

0 comments on commit f888c07

Please sign in to comment.