diff --git a/fiber/Cargo.toml b/fiber/Cargo.toml index aa23e66..6a3d91a 100644 --- a/fiber/Cargo.toml +++ b/fiber/Cargo.toml @@ -10,5 +10,9 @@ description = "A library for implementing distributed systems with commmunicatin corosensei = "0.1" fibril_core = { path = "../core", version = "0" } +[dependencies.tracing] +optional = true +version = "0.1" + [features] serde = ["fibril_core/serde"] diff --git a/fiber/src/fiber.rs b/fiber/src/fiber.rs index 6a5b899..8b9a43b 100644 --- a/fiber/src/fiber.rs +++ b/fiber/src/fiber.rs @@ -4,22 +4,49 @@ use { fibril_core::{Command, Event, Step}, }; +#[cfg(feature = "tracing")] +use tracing::{event, span, Level}; + impl<'a, M> Fiber<'a, M> { pub fn new(behavior: impl FnOnce(Sdk<'_, M>) + 'a) -> Self { - Self(ScopedCoroutine::new(move |yielder, spawn_ok| { - let id = match spawn_ok { - Event::SpawnOk(id) => id, - _ => unreachable!(), - }; - behavior(Sdk(yielder, id)) - })) + #[cfg(feature = "tracing")] + let span = span!(Level::TRACE, "fiber"); + + Self( + ScopedCoroutine::new(move |yielder, spawn_ok| { + let id = match spawn_ok { + Event::SpawnOk(id) => id, + _ => unreachable!(), + }; + + #[cfg(feature = "tracing")] + event!(Level::TRACE, "fiber id: {}", id); + + behavior(Sdk(yielder, id)) + }), + #[cfg(feature = "tracing")] + span, + ) } } impl Step for Fiber<'_, M> { fn step(&mut self, event: Event) -> Command { + #[cfg(feature = "tracing")] + let span = span!(Level::TRACE, "step"); + #[cfg(feature = "tracing")] + let _enter = span.enter(); + match self.0.resume(event) { - CoroutineResult::Yield(command) => command, - CoroutineResult::Return(()) => Command::Exit, + CoroutineResult::Yield(command) => { + #[cfg(feature = "tracing")] + event!(Level::TRACE, "yield"); + command + } + CoroutineResult::Return(()) => { + #[cfg(feature = "tracing")] + event!(Level::TRACE, "return"); + Command::Exit + } } } } diff --git a/fiber/src/lib.rs b/fiber/src/lib.rs index 21063c1..5b2b5e5 100644 --- a/fiber/src/lib.rs +++ b/fiber/src/lib.rs @@ -11,7 +11,13 @@ use { mod fiber; mod sdk; +#[cfg(not(feature = "tracing"))] pub struct Fiber<'a, M>(ScopedCoroutine<'a, Event, Command, (), DefaultStack>); +#[cfg(feature = "tracing")] +pub struct Fiber<'a, M>( + ScopedCoroutine<'a, Event, Command, (), DefaultStack>, + tracing::Span, +); pub use fibril_core::Id;