use crate::forest::{Tag, Tree};
use std::{fmt, time::Duration};
use tracing::Level;
mod field;
pub use field::Field;
pub(crate) use field::FieldSet;
#[derive(Clone, Debug)]
pub struct Event {
pub(crate) shared: Shared,
pub(crate) message: Option<String>,
pub(crate) tag: Option<Tag>,
}
#[derive(Clone, Debug)]
pub struct Span {
pub(crate) shared: Shared,
pub(crate) name: &'static str,
pub(crate) total_duration: Duration,
pub(crate) inner_duration: Duration,
#[cfg(feature = "forest-cpu-time")]
pub(crate) base_cpu_time: Option<CpuTime>,
#[cfg(feature = "forest-cpu-time")]
pub(crate) inner_cpu_time: Option<CpuTime>,
pub(crate) nodes: Vec<Tree>,
}
#[cfg(feature = "forest-cpu-time")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CpuTime {
pub user: Duration,
pub system: Duration,
}
#[cfg(feature = "forest-cpu-time")]
impl CpuTime {
pub(crate) fn checked_add(self, other: Self) -> Option<Self> {
Some(Self {
user: self.user.checked_add(other.user)?,
system: self.system.checked_add(other.system)?,
})
}
pub(crate) fn checked_sub(self, earlier: Self) -> Option<Self> {
Some(Self {
user: self.user.checked_sub(earlier.user)?,
system: self.system.checked_sub(earlier.system)?,
})
}
}
#[derive(Clone, Debug)]
pub(crate) struct Shared {
pub(crate) level: Level,
pub(crate) fields: FieldSet,
}
#[derive(Debug)]
pub struct ExpectedEventError(());
impl fmt::Display for ExpectedEventError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Expected an event, found a span")
}
}
impl std::error::Error for ExpectedEventError {}
#[derive(Debug)]
pub struct ExpectedSpanError(());
impl fmt::Display for ExpectedSpanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Expected a span, found an event")
}
}
impl std::error::Error for ExpectedSpanError {}
impl Tree {
pub fn event(&self) -> Result<&Event, ExpectedEventError> {
match self {
Tree::Event(event) => Ok(event),
Tree::Span(_) => Err(ExpectedEventError(())),
}
}
pub fn span(&self) -> Result<&Span, ExpectedSpanError> {
match self {
Tree::Event(_) => Err(ExpectedSpanError(())),
Tree::Span(span) => Ok(span),
}
}
}
impl Event {
pub fn level(&self) -> Level {
self.shared.level
}
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
pub fn tag(&self) -> Option<Tag> {
self.tag
}
pub fn fields(&self) -> &[Field] {
&self.shared.fields
}
}
impl Span {
pub(crate) fn new(shared: Shared, name: &'static str) -> Self {
Span {
shared,
name,
total_duration: Duration::ZERO,
inner_duration: Duration::ZERO,
#[cfg(feature = "forest-cpu-time")]
base_cpu_time: crate::forest::cpu::initial_time(),
#[cfg(feature = "forest-cpu-time")]
inner_cpu_time: crate::forest::cpu::initial_time(),
nodes: Vec::new(),
}
}
pub fn level(&self) -> Level {
self.shared.level
}
pub fn name(&self) -> &str {
self.name
}
pub fn fields(&self) -> &[Field] {
&self.shared.fields
}
pub fn nodes(&self) -> &[Tree] {
&self.nodes
}
pub fn total_duration(&self) -> Duration {
self.total_duration
}
pub fn inner_duration(&self) -> Duration {
self.inner_duration
}
pub fn base_duration(&self) -> Duration {
self.total_duration
.checked_sub(self.inner_duration)
.expect("the forest layer raises the total duration to at least the sum of its children")
}
#[cfg(feature = "forest-cpu-time")]
pub fn total_cpu_time(&self) -> Option<CpuTime> {
self.base_cpu_time?.checked_add(self.inner_cpu_time?)
}
#[cfg(feature = "forest-cpu-time")]
pub fn base_cpu_time(&self) -> Option<CpuTime> {
self.base_cpu_time
}
#[cfg(feature = "forest-cpu-time")]
pub fn inner_cpu_time(&self) -> Option<CpuTime> {
self.inner_cpu_time
}
}