mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
trace-core: API polish (#962)
This branch makes a handful of `tokio-trace-core` API improvements, mostly around naming. In particular: * Rename `dispatcher::with` to `dispatcher::get_default` * Rename `Event::observe` to `Event::dispatch` * Make `field::ValidLen` trait private Closes #948 Closes #960 Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
extern crate tokio_trace;
|
extern crate tokio_trace;
|
||||||
|
|
||||||
use tokio_trace::{
|
use tokio_trace::{
|
||||||
field::{self, Field, Visit},
|
field::{Field, Visit},
|
||||||
span,
|
span,
|
||||||
subscriber::{self, Subscriber},
|
subscriber::{self, Subscriber},
|
||||||
Event, Id, Metadata,
|
Event, Id, Metadata,
|
||||||
|
|||||||
@@ -740,7 +740,7 @@ macro_rules! event {
|
|||||||
};
|
};
|
||||||
if is_enabled!(callsite) {
|
if is_enabled!(callsite) {
|
||||||
let meta = callsite.metadata();
|
let meta = callsite.metadata();
|
||||||
Event::observe(meta, &valueset!(meta.fields(), $( $k = $val),* ));
|
Event::dispatch(meta, &valueset!(meta.fields(), $( $k = $val),* ));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -1091,7 +1091,7 @@ macro_rules! is_enabled {
|
|||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
let meta = $callsite.metadata();
|
let meta = $callsite.metadata();
|
||||||
$crate::dispatcher::with(|current| current.enabled(meta))
|
$crate::dispatcher::get_default(|current| current.enabled(meta))
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,7 +272,7 @@ impl<'a> Span<'a> {
|
|||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn make(meta: &'a Metadata<'a>, new_span: Attributes) -> Span<'a> {
|
fn make(meta: &'a Metadata<'a>, new_span: Attributes) -> Span<'a> {
|
||||||
let inner = dispatcher::with(move |dispatch| {
|
let inner = dispatcher::get_default(move |dispatch| {
|
||||||
let id = dispatch.new_span(&new_span);
|
let id = dispatch.new_span(&new_span);
|
||||||
Some(Inner::new(id, dispatch, meta))
|
Some(Inner::new(id, dispatch, meta))
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -185,7 +185,6 @@ where
|
|||||||
fn new_span(&self, span: &Attributes) -> Id {
|
fn new_span(&self, span: &Attributes) -> Id {
|
||||||
use span::Parent;
|
use span::Parent;
|
||||||
let meta = span.metadata();
|
let meta = span.metadata();
|
||||||
let values = span.values();
|
|
||||||
let id = self.ids.fetch_add(1, Ordering::SeqCst);
|
let id = self.ids.fetch_add(1, Ordering::SeqCst);
|
||||||
let id = Id::from_u64(id as u64);
|
let id = Id::from_u64(id as u64);
|
||||||
println!(
|
println!(
|
||||||
|
|||||||
@@ -52,11 +52,10 @@ pub fn with_default<T>(dispatcher: Dispatch, f: impl FnOnce() -> T) -> T {
|
|||||||
let _guard = ResetGuard(prior.ok());
|
let _guard = ResetGuard(prior.ok());
|
||||||
f()
|
f()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes a closure with a reference to this thread's current [dispatcher].
|
/// Executes a closure with a reference to this thread's current [dispatcher].
|
||||||
///
|
///
|
||||||
/// [dispatcher]: ../dispatcher/struct.Dispatch.html
|
/// [dispatcher]: ../dispatcher/struct.Dispatch.html
|
||||||
pub fn with<T, F>(mut f: F) -> T
|
pub fn get_default<T, F>(mut f: F) -> T
|
||||||
where
|
where
|
||||||
F: FnMut(&Dispatch) -> T,
|
F: FnMut(&Dispatch) -> T,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ impl<'a> Event<'a> {
|
|||||||
/// Constructs a new `Event` with the specified metadata and set of values,
|
/// Constructs a new `Event` with the specified metadata and set of values,
|
||||||
/// and observes it with the current subscriber.
|
/// and observes it with the current subscriber.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn observe(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet) {
|
pub fn dispatch(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet) {
|
||||||
let event = Event { metadata, fields };
|
let event = Event { metadata, fields };
|
||||||
::dispatcher::with(|current| {
|
::dispatcher::get_default(|current| {
|
||||||
current.event(&event);
|
current.event(&event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ use std::{
|
|||||||
ops::Range,
|
ops::Range,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use self::private::ValidLen;
|
||||||
|
|
||||||
/// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value
|
/// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value
|
||||||
/// data.
|
/// data.
|
||||||
///
|
///
|
||||||
@@ -227,15 +229,6 @@ pub struct DisplayValue<T: fmt::Display>(T);
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DebugValue<T: fmt::Debug>(T);
|
pub struct DebugValue<T: fmt::Debug>(T);
|
||||||
|
|
||||||
/// Marker trait implemented by arrays which are of valid length to
|
|
||||||
/// construct a `ValueSet`.
|
|
||||||
///
|
|
||||||
/// `ValueSet`s may only be constructed from arrays containing 32 or fewer
|
|
||||||
/// elements, to ensure the array is small enough to always be allocated on the
|
|
||||||
/// stack. This trait is only implemented by arrays of an appropriate length,
|
|
||||||
/// ensuring that the correct size arrays are used at compile-time.
|
|
||||||
pub trait ValidLen<'a>: ::sealed::Sealed + Borrow<[(&'a Field, Option<&'a (Value + 'a)>)]> {}
|
|
||||||
|
|
||||||
/// Wraps a type implementing `fmt::Display` as a `Value` that can be
|
/// Wraps a type implementing `fmt::Display` as a `Value` that can be
|
||||||
/// recorded using its `Display` implementation.
|
/// recorded using its `Display` implementation.
|
||||||
pub fn display<T>(t: T) -> DisplayValue<T>
|
pub fn display<T>(t: T) -> DisplayValue<T>
|
||||||
@@ -615,12 +608,23 @@ impl<'a> fmt::Debug for ValueSet<'a> {
|
|||||||
|
|
||||||
// ===== impl ValidLen =====
|
// ===== impl ValidLen =====
|
||||||
|
|
||||||
|
mod private {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// Marker trait implemented by arrays which are of valid length to
|
||||||
|
/// construct a `ValueSet`.
|
||||||
|
///
|
||||||
|
/// `ValueSet`s may only be constructed from arrays containing 32 or fewer
|
||||||
|
/// elements, to ensure the array is small enough to always be allocated on the
|
||||||
|
/// stack. This trait is only implemented by arrays of an appropriate length,
|
||||||
|
/// ensuring that the correct size arrays are used at compile-time.
|
||||||
|
pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (Value + 'a)>)]> {}
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! impl_valid_len {
|
macro_rules! impl_valid_len {
|
||||||
( $( $len:tt ),+ ) => {
|
( $( $len:tt ),+ ) => {
|
||||||
$(
|
$(
|
||||||
impl<'a> ::sealed::Sealed for
|
impl<'a> private::ValidLen<'a> for
|
||||||
[(&'a Field, Option<&'a (Value + 'a)>); $len] {}
|
|
||||||
impl<'a> ValidLen<'a> for
|
|
||||||
[(&'a Field, Option<&'a (Value + 'a)>); $len] {}
|
[(&'a Field, Option<&'a (Value + 'a)>); $len] {}
|
||||||
)+
|
)+
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user